-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsort.awk
executable file
·82 lines (73 loc) · 1.62 KB
/
qsort.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
#! /usr/bin/awk -f
# NOTE: array 'A' must be indexed by ordinal integer numbers, starting at
# left, ending at right (warning: take care with indexing!)
function qsort(A, left, right,
i, last) # internal operands
{
if (left >= right) { # do nothing if array contains
return; # less than two elements
}
swap(A, left, left + int(rand() * (right - left + 1)));
last = left; # A[left] is now partition element
for (i = left + 1; i <= right; i++) {
if (A[i] < A[left]) { # using "<" as a compare "function"
swap(A, ++last, i);
}
}
swap(A, left, last);
# sort left half...
qsort(A, left, last - 1);
# sort right half...
qsort(A, last + 1, right);
}
function swap(A, i, j,
t) # internal operand
{
t = A[i];
A[i] = A[j];
A[j] = t;
}
# in Gawk 3.2 and newer length(A) does this too, but not nawk
#
function alen(A, # array name
i, n) # local variables
{
n = 0;
for (i in A) {
n++;
}
return n;
}
BEGIN {
if (DEBUG == 0 && DEBUG == "") {
NDEBUG = 1;
}
# inplist[0] = "bbb"; # 22;
# inplist[1] = "ddd"; # 44;
# inplist[2] = "aaa"; # 11;
# inplist[3] = "eee"; # 55;
# inplist[4] = "ccc"; # 33;
# NR = 5;
}
{
# slurp.... (note: NR starts at 1)
inplist[NR-1] = $0;
}
END {
if (NDEBUG == 0) {
printf("read %d lines into array of %d items\n", NR, alen(inplist));
for (i = 0; i < NR; i++) {
printf("intput item # %d has value = %s\n", i, inplist[i]);
}
}
qsort(inplist, 0, NR - 1)
printf("\n");
for (i = 0; i < NR; i++) {
if (NDEBUG == 0) {
printf("result item # %d has value = %s\n", i, inplist[i]);
} else {
printf("%s\n", inplist[i]);
}
}
delete inplist;
}