-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.go
63 lines (56 loc) · 1.28 KB
/
QuickSort.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
package main
import (
"fmt"
"math/rand"
"time"
)
func QuickSort(list []int) []int {
n := len(list)
if n == 1 {
return list
}
var c_small, c_large, c_sorted []int
for i, v := range list {
if v < list[0] {
c_small = append(c_small, list[i])
}
if v > list[0] {
c_large = append(c_large, list[i])
}
}
if len(c_small) != 0 {
c_sorted = append(c_sorted, QuickSort(c_small)...)
}
c_sorted = append(c_sorted, list[0])
if len(c_large) != 0 {
c_sorted = append(c_sorted, QuickSort(c_large)...)
}
return c_sorted
}
func RandomizedQuickSort(list []int) []int {
n := len(list)
if n == 1 {
return list
}
// Инициализация генератора случайных чисел с использованием текущего времени в качестве источника
rand.NewSource(time.Now().UnixNano())
m := rand.Intn(n - 1)
fmt.Println(m)
var c_small, c_large, c_sorted []int
for i, v := range list {
if v < list[m] {
c_small = append(c_small, list[i])
}
if v > list[m] {
c_large = append(c_large, list[i])
}
}
if len(c_small) != 0 {
c_sorted = append(c_sorted, QuickSort(c_small)...)
}
c_sorted = append(c_sorted, list[m])
if len(c_large) != 0 {
c_sorted = append(c_sorted, QuickSort(c_large)...)
}
return c_sorted
}