-
Notifications
You must be signed in to change notification settings - Fork 1
/
go_005.go
50 lines (45 loc) · 971 Bytes
/
go_005.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
// First project First.go
package main
import (
"fmt"
)
const SIZE = 100
var tmp [SIZE]int
/* quick sort algorithm*/
func quick_sort(num_list *[SIZE]int, left, right int) {
if right-left <= 1 {
return
}
var left_inx int = left
var right_inx int = right - 1
for i := left + 1; i < right; i++ {
if num_list[i] < num_list[left] {
tmp[left_inx] = num_list[i]
left_inx++
} else {
tmp[right_inx] = num_list[i]
right_inx--
}
}
tmp[left_inx] = num_list[left]
for i := left; i < right; i++ {
num_list[i] = tmp[i]
}
//fmt.Println(left, right, num_list)
quick_sort(num_list, left, left_inx)
quick_sort(num_list, left_inx+1, right)
}
/* please don't use 'enter',because fmt.scanf can not ignore it! */
func main() {
var n int
var num_list [SIZE]int
fmt.Scanf("%d", &n)
for i := 0; i < n; i++ {
fmt.Scanf("%d", &num_list[i])
}
quick_sort(&num_list, 0, n)
for i := 0; i < n; i++ {
fmt.Printf("%d ", num_list[i])
}
fmt.Println("")
}