-
Notifications
You must be signed in to change notification settings - Fork 0
/
shufsort.v
52 lines (42 loc) · 946 Bytes
/
shufsort.v
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
/* This program prints an array containing the numbers 1-10, shuffles it, prints
it again, sorts it using quicksort, then prints it once more. */
import rand
fn shuffle(mut arr []int, low int, high int) {
for i in low .. high {
j := rand.intn(high - low) + low
tmp := arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
}
fn partition(mut arr []int, low int, high int) int {
pivot := arr[high]
mut i := (low - 1)
for j in low .. high {
if arr[j] < pivot {
i++
tmp := arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
}
tmp := arr[i + 1]
arr[i + 1] = arr[high]
arr[high] = tmp
return i + 1
}
fn qsort(mut arr []int, low int, high int) {
if low < high {
pi := partition(mut arr, low, high)
qsort(mut arr, low, pi - 1)
qsort(mut arr, pi + 1, high)
}
}
fn main() {
mut arr := [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
println(arr)
shuffle(mut arr, 0, arr.len - 1)
println(arr)
qsort(mut arr, 0, arr.len - 1)
println(arr)
}