-
Notifications
You must be signed in to change notification settings - Fork 24
/
msort.awk
358 lines (306 loc) · 9.73 KB
/
msort.awk
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/awk -f
# comparison function
# usage: __compare(a, b, how)
# compares "a" and "b" based on "how", returning 0 for false and 1 for true.
# required for all of the msort() functions below
function __compare(a, b, how) {
# standard comparisons
if (how == "std asc") {
return a < b;
} else if (how == "std desc") {
return a > b;
# force string comps
} else if (how == "str asc") {
return "a" a < "a" b;
} else if (how == "str desc") {
return "a" a > "a" b;
# force numeric
} else if (how == "num asc") {
return +a < +b;
} else if (how == "num desc") {
return +a > +b;
}
}
# actual sorting function
# usage: __mergesort(array, len, how)
# sorts the values in "array" in-place, from indices 1 to "len", based
# on the comparison mode "how" (see the msort() description).
# required for all of the msort() functions below
function __mergesort(array, len, how,
tmpa, alen, a, tmpb, blen, b, half, cur, pos, tmp) {
# if there are 10 elements or less, use an insertion sort and return
if (len <= 10) {
# loop over each item, starting with the second
for (cur=2; cur<=len; cur++) {
pos = cur;
# shift the item down the list into position
while (pos > 1 && __compare(array[pos], array[pos-1], how)) {
tmp = array[pos];
array[pos] = array[pos-1];
array[pos-1] = tmp;
pos--;
}
}
# return
return len;
}
# determine the halfway point of the indices
half = int(len / 2);
# create temp arrays of the two halves
a = 0;
for (i=1; i<=half; i++) {
tmpa[++a] = array[i];
# remove the index from the original array
delete array[i];
}
b = 0;
for (i=half+1; i<=len; i++) {
tmpb[++b] = array[i];
# remove the index from the original array
delete array[i];
}
# sort the two halves with recursive calls
alen = __mergesort(tmpa, a, how);
blen = __mergesort(tmpb, b, how);
# merge the two halves
len = 0;
a = b = 1;
# loop while there is still an element in either array
while (a <= alen || b <= blen) {
# a sorts first
if (a <= alen && (b > blen || __compare(tmpa[a], tmpb[b], how))) {
array[++len] = tmpa[a];
delete tmpa[a++]; # remove the index from the temp array
# b sorts first
} else {
array[++len] = tmpb[b];
delete tmpb[b++]; # remove the index from the temp array
}
}
# return the length
return len;
}
# actual sorting function for the msortv() function
# usage: __mergesortv(array, values, len, how)
# sorts the values in "array" on the original values in "values", from indices
# 1 through "len", based on the comparison mode "how" (see the msortv()
# description). required for all of the msortv() functions below
function __mergesortv(array, values, len, how,
tmpa, tmpva, alen, a, tmpb, tmpvb, blen, b,
half, cur, pos, tmp) {
# if there are 10 elements or less, use an insertion sort and return
if (len <= 10) {
# loop over each item, starting with the second
for (cur=2; cur<=len; cur++) {
pos = cur;
# shift the item down the list into position
while (pos > 1 && __compare(values[pos], values[pos-1], how)) {
tmp = array[pos];
array[pos] = array[pos-1];
array[pos-1] = tmp;
tmp = values[pos];
values[pos] = values[pos-1];
values[pos-1] = tmp;
pos--;
}
}
# return
return len;
}
# determine the halfway point of the indices
half = int(len / 2);
# create temp arrays of the two halves
a = 0;
for (i=1; i<=half; i++) {
tmpa[++a] = array[i];
tmpva[a] = values[i];
# remove the index from the original array
delete array[i];
}
b = 0;
for (i=half+1; i<=len; i++) {
tmpb[++b] = array[i];
tmpvb[b] = values[i];
# remove the index from the original array
delete array[i];
}
# sort the two halves with recursive calls
alen = __mergesortv(tmpa, tmpva, a, how);
blen = __mergesortv(tmpb, tmpvb, b, how);
# merge the two halves
len = 0;
a = b = 1;
# loop while there is still an element in either array
while (a <= alen || b <= blen) {
# a sorts first
if (a <= alen && (b > blen || __compare(tmpva[a], tmpvb[b], how))) {
array[++len] = tmpa[a];
values[len] = tmpva[a];
delete tmpva[a];
delete tmpa[a++]; # remove the index from the temp array
# b sorts first
} else {
array[++len] = tmpb[b];
values[len] = tmpvb[b];
delete tmpvb[b];
delete tmpb[b++]; # remove the index from the temp array
}
}
# return the length
return len;
}
## usage: msort(s, d [, how])
## sorts the elements in the array "s" using awk's normal rules for comparing
## values, creating a new sorted array "d" indexed with sequential integers
## starting with 1. returns the length, or -1 if an error occurs.. leaves the
## indices of the source array "s" unchanged. the optional string "how" controls
## the direction and the comparison mode. uses the merge sort algorithm, with an
## insertion sort when the list size gets small enough. this is not a stable
## sort. requires the __compare() and __mergesort() functions.
## valid values for "how" are:
## "std asc"
## use awk's standard rules for comparison, ascending. this is the default
## "std desc"
## use awk's standard rules for comparison, descending.
## "str asc"
## force comparison as strings, ascending.
## "str desc"
## force comparison as strings, descending.
## "num asc"
## force a numeric comparison, ascending.
## "num desc"
## force a numeric comparison, descending.
function msort(array, out, how, count, i) {
# make sure how is correct
if (length(how)) {
if (how !~ /^(st[rd]|num) (a|de)sc$/) {
return -1;
}
# how was not passed, use the default
} else {
how = "std asc";
}
# loop over each index, and generate a new array with the same values and
# sequential indices
count = 0;
for (i in array) {
out[++count] = array[i];
}
# actually sort
return __mergesort(out, count, how);
}
## usage: imsort(s [, how])
## the bevavior is the same as that of msort(), except that the array "s" is
## sorted in-place. the original indices are destroyed and replaced with
## sequential integers. everything else is described in msort() above.
function imsort(array, how, tmp, count, i) {
# make sure how is correct
if (length(how)) {
if (how !~ /^(st[rd]|num) (a|de)sc$/) {
return -1;
}
# how was not passed, use the default
} else {
how = "std asc";
}
# loop over each index, and generate a new array with the same values and
# sequential indices
count = 0;
for (i in array) {
tmp[++count] = array[i];
delete array[i];
}
# copy tmp back over array
for (i=1; i<=count; i++) {
array[i] = tmp[i];
delete tmp[i];
}
# actually sort
return __mergesort(array, count, how);
}
## usage: msorti(s, d [, how])
## the behavior is the same as that of msort(), except that the array indices
## are used for sorting, not the array values. when done, the new array is
## indexed numerically, and the values are those of the original indices.
## everything else is described in msort() above.
function msorti(array, out, how, count, i) {
# make sure how is correct
if (length(how)) {
if (how !~ /^(st[rd]|num) (a|de)sc$/) {
return -1;
}
# how was not passed, use the default
} else {
how = "std asc";
}
# loop over each index, and generate a new array with the original indices
# mapped to new numeric ones
count = 0;
for (i in array) {
out[++count] = i;
}
# actually sort
return __mergesort(out, count, how);
}
## usage: imsorti(s [, how])
## the bevavior is the same as that of msorti(), except that the array "s" is
## sorted in-place. the original indices are destroyed and replaced with
## sequential integers. everything else is described in msort() and msorti()
## above.
function imsorti(array, how, tmp, count, i) {
# make sure how is correct
if (length(how)) {
if (how !~ /^(st[rd]|num) (a|de)sc$/) {
return -1;
}
# how was not passed, use the default
} else {
how = "std asc";
}
# loop over each index, and generate a new array with the original indices
# mapped to new numeric ones
count = 0;
for (i in array) {
tmp[++count] = i;
delete array[i];
}
# copy tmp back over the original array
for (i=1; i<=count; i++) {
array[i] = tmp[i];
delete tmp[i];
}
# actually sort
return __mergesort(array, count, how);
}
## usage: msortv(s, d [, how])
## sorts the indices in the array "s" based on the values, creating a new
## sorted array "d" indexed with sequential integers starting with 1, and the
## values the indices of "s". returns the length, or -1 if an error occurs.
## leaves the source array "s" unchanged. the optional string "how" controls
## the direction and the comparison mode. uses the merge sort algorithm, with
## an insertion sort when the list size gets small enough. this is not a stable
## sort. requires the __compare() and __mergesortv() functions. valid values for
## "how" are explained in the msort() function above.
function msortv(array, out, how, values, count, i) {
# make sure how is correct
if (length(how)) {
if (how !~ /^(st[rd]|num) (a|de)sc$/) {
return -1;
}
# how was not passed, use the default
} else {
how = "std asc";
}
# loop over each index, and generate two new arrays: the original indices
# mapped to numeric ones, and the values mapped to the same indices
count = 0;
for (i in array) {
count++;
out[count] = i;
values[count] = array[i];
}
# actually sort
return __mergesortv(out, values, count, how);
}
# You can do whatever you want with this stuff, but a thanks is always
# appreciated