-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqsort.go
71 lines (65 loc) · 1.52 KB
/
qsort.go
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
package main
import (
"fmt"
)
var arr []int = []int{
1350, 1446, 1021, 1328, 1170, 1566, 1588, 1444, 1341, 1311,
1610, 1506, 1530, 1637, 1620, 1390, 1013, 1489, 1058, 1310,
1266, 1150, 1232, 1309, 1179, 953, 1553, 1240, 1319, 1029,
1479, 1226, 1053, 1221, 1249, 1108, 1506, 1368, 1126, 1338,
1045, 1058, 1249, 1535, 1497, 1248, 1292, 1554, 1026, 1510,
1303, 1596, 1065, 995, 1434, 1311, 1659, 1501, 1502, 1296,
1135, 1653, 1525, 1131, 1021, 1202, 1486, 1587, 1559, 1420,
1560, 1246, 1113, 1573, 1002, 1031, 1370, 1545, 1383, 1430,
1630, 950, 1451, 1373, 1468, 1545, 1502, 1145, 1123, 1566,
1403, 1192, 1186, 970, 1286, 1518, 1303, 1652, 1016, 1265,
976, 1203, 1580, 1596, 1256, 1037, 991, 1026, 1163, 1225,
1202, 1193, 1075, 1339, 1338, 1469, 1100, 1360, 960, 1400,
1413, 1276, 1601, 1559, 1154, 1212, 1124, 1363, 1508, 1573,
1647, 1081, 1225, 1190, 1370, 986, 1341, 1067, 1460, 1010,
1534, 1535, 1090,
}
func swap(i, j int) {
c := arr[i]
arr[i] = arr[j]
arr[j] = c
}
func less(i, j int) bool {
if arr[i] < arr[j] {
return true
} else {
return false
}
}
func qsort(n int,
less func(i, j int) bool,
swap func(i, j int), optional_m ...int) {
m := 0
if len(optional_m) > 0 {
m = optional_m[0]
}
if m > n-1 {
return
}
f := m
p := n - 1
cp := p
for j := f; j < p; j++ {
if less(j, cp) {
if j == cp {
cp = f
} else if f == cp {
cp = j
}
swap(j, f)
f++
}
}
swap(f, p)
qsort(f, less, swap, m)
qsort(n, less, swap, f+1)
}
func main() {
qsort(len(arr), less, swap)
fmt.Println(arr)
}