1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
define LOGPTR 32
define swap {temp=$1;$1=$2;$2=temp}
# AT_SSQUICK -- Quicksort for text data. NOTE -- This algorithm is quadratic in
# the worst case, i.e., when the data is already sorted. A random method of
# selecting the pivot should be used to improve the behaviour on sorted arrays.
procedure at_ssquick (linbuf, linptr, index, nlines)
char linbuf[ARB] #I the input string buffer
int linptr[ARB] #U the indices of strings in buffer
int index[ARB] #O the output sort index
int nlines #I the number of strings
int i, j, k, temp, lv[LOGPTR], p, pivlin, uv[LOGPTR]
int strncmp()
begin
lv[1] = 1
uv[1] = nlines
p = 1
do i = 1, nlines
index[i] = i
while (p > 0) {
if (lv[p] >= uv[p]) # only one elem in this subset
p = p - 1 # pop stack
else {
# Dummy loop to trigger optimizer.
do p = p, ARB {
i = lv[p] - 1
j = uv[p]
# Select pivot element at midpoint of interval to avoid
# quadratic behavior on a sorted list.
k = (lv[p] + uv[p]) / 2
swap (linptr[j], linptr[k])
swap (index[j], index[k])
pivlin = linptr[j]
while (i < j) {
for (i=i+1; strncmp (linbuf, linptr[i], pivlin) < 0;
i=i+1)
;
for (j=j-1; j > i; j=j-1)
if (strncmp (linbuf, linptr[j], pivlin) <= 0)
break
if (i < j) { # out of order pair
swap (linptr[i], linptr[j])
swap (index[i], index[j])
}
}
j = uv[p] # move pivot to position i
swap (linptr[i], linptr[j])
swap (index[i], index[j])
if (i-lv[p] < uv[p] - i) { # stack so shorter done first
lv[p+1] = lv[p]
uv[p+1] = i - 1
lv[p] = i + 1
} else {
lv[p+1] = i + 1
uv[p+1] = uv[p]
uv[p] = i - 1
}
break
}
p = p + 1 # push onto stack
}
}
end
|