3
3
4
4
package algorithm
5
5
6
- import "github.com/duke-git/lancet/v2/lancetconstraints "
6
+ import "github.com/duke-git/lancet/v2/constraints "
7
7
8
8
// BubbleSort applys the bubble sort algorithm to sort the collection, will change the original collection data.
9
9
// Play: https://go.dev/play/p/GNdv7Jg2Taj
10
- func BubbleSort [T any ](slice []T , comparator lancetconstraints .Comparator ) {
10
+ func BubbleSort [T any ](slice []T , comparator constraints .Comparator ) {
11
11
for i := 0 ; i < len (slice ); i ++ {
12
12
for j := 0 ; j < len (slice )- 1 - i ; j ++ {
13
13
isCurrGreatThanNext := comparator .Compare (slice [j ], slice [j + 1 ]) == 1
@@ -20,7 +20,7 @@ func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) {
20
20
21
21
// InsertionSort applys the insertion sort algorithm to sort the collection, will change the original collection data.
22
22
// Play: https://go.dev/play/p/G5LJiWgJJW6
23
- func InsertionSort [T any ](slice []T , comparator lancetconstraints .Comparator ) {
23
+ func InsertionSort [T any ](slice []T , comparator constraints .Comparator ) {
24
24
for i := 0 ; i < len (slice ); i ++ {
25
25
for j := i ; j > 0 ; j -- {
26
26
isPreLessThanCurrent := comparator .Compare (slice [j ], slice [j - 1 ]) == - 1
@@ -35,7 +35,7 @@ func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
35
35
36
36
// SelectionSort applys the selection sort algorithm to sort the collection, will change the original collection data.
37
37
// Play: https://go.dev/play/p/oXovbkekayS
38
- func SelectionSort [T any ](slice []T , comparator lancetconstraints .Comparator ) {
38
+ func SelectionSort [T any ](slice []T , comparator constraints .Comparator ) {
39
39
for i := 0 ; i < len (slice ); i ++ {
40
40
min := i
41
41
for j := i + 1 ; j < len (slice ); j ++ {
@@ -49,7 +49,7 @@ func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
49
49
50
50
// ShellSort applys the shell sort algorithm to sort the collection, will change the original collection data.
51
51
// Play: https://go.dev/play/p/3ibkszpJEu3
52
- func ShellSort [T any ](slice []T , comparator lancetconstraints .Comparator ) {
52
+ func ShellSort [T any ](slice []T , comparator constraints .Comparator ) {
53
53
size := len (slice )
54
54
55
55
gap := 1
@@ -69,11 +69,11 @@ func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
69
69
70
70
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1.
71
71
// Play: https://go.dev/play/p/7Y7c1Elk3ax
72
- func QuickSort [T any ](slice []T , comparator lancetconstraints .Comparator ) {
72
+ func QuickSort [T any ](slice []T , comparator constraints .Comparator ) {
73
73
quickSort (slice , 0 , len (slice )- 1 , comparator )
74
74
}
75
75
76
- func quickSort [T any ](slice []T , lowIndex , highIndex int , comparator lancetconstraints .Comparator ) {
76
+ func quickSort [T any ](slice []T , lowIndex , highIndex int , comparator constraints .Comparator ) {
77
77
if lowIndex < highIndex {
78
78
p := partition (slice , lowIndex , highIndex , comparator )
79
79
quickSort (slice , lowIndex , p - 1 , comparator )
@@ -82,7 +82,7 @@ func quickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconst
82
82
}
83
83
84
84
// partition split slice into two parts
85
- func partition [T any ](slice []T , lowIndex , highIndex int , comparator lancetconstraints .Comparator ) int {
85
+ func partition [T any ](slice []T , lowIndex , highIndex int , comparator constraints .Comparator ) int {
86
86
p := slice [highIndex ]
87
87
i := lowIndex
88
88
for j := lowIndex ; j < highIndex ; j ++ {
@@ -99,7 +99,7 @@ func partition[T any](slice []T, lowIndex, highIndex int, comparator lancetconst
99
99
100
100
// HeapSort applys the heap sort algorithm to sort the collection, will change the original collection data.
101
101
// Play: https://go.dev/play/p/u6Iwa1VZS_f
102
- func HeapSort [T any ](slice []T , comparator lancetconstraints .Comparator ) {
102
+ func HeapSort [T any ](slice []T , comparator constraints .Comparator ) {
103
103
size := len (slice )
104
104
105
105
for i := size / 2 - 1 ; i >= 0 ; i -- {
@@ -111,7 +111,7 @@ func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) {
111
111
}
112
112
}
113
113
114
- func sift [T any ](slice []T , lowIndex , highIndex int , comparator lancetconstraints .Comparator ) {
114
+ func sift [T any ](slice []T , lowIndex , highIndex int , comparator constraints .Comparator ) {
115
115
i := lowIndex
116
116
j := 2 * i + 1
117
117
@@ -133,11 +133,11 @@ func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraint
133
133
134
134
// MergeSort applys the merge sort algorithm to sort the collection, will change the original collection data.
135
135
// Play: https://go.dev/play/p/ydinn9YzUJn
136
- func MergeSort [T any ](slice []T , comparator lancetconstraints .Comparator ) {
136
+ func MergeSort [T any ](slice []T , comparator constraints .Comparator ) {
137
137
mergeSort (slice , 0 , len (slice )- 1 , comparator )
138
138
}
139
139
140
- func mergeSort [T any ](slice []T , lowIndex , highIndex int , comparator lancetconstraints .Comparator ) {
140
+ func mergeSort [T any ](slice []T , lowIndex , highIndex int , comparator constraints .Comparator ) {
141
141
if lowIndex < highIndex {
142
142
mid := (lowIndex + highIndex ) / 2
143
143
mergeSort (slice , lowIndex , mid , comparator )
@@ -146,7 +146,7 @@ func mergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconst
146
146
}
147
147
}
148
148
149
- func merge [T any ](slice []T , lowIndex , midIndex , highIndex int , comparator lancetconstraints .Comparator ) {
149
+ func merge [T any ](slice []T , lowIndex , midIndex , highIndex int , comparator constraints .Comparator ) {
150
150
i := lowIndex
151
151
j := midIndex + 1
152
152
temp := []T {}
@@ -175,7 +175,7 @@ func merge[T any](slice []T, lowIndex, midIndex, highIndex int, comparator lance
175
175
176
176
// CountSort applys the count sort algorithm to sort the collection, don't change the original collection data.
177
177
// Play: https://go.dev/play/p/tB-Umgm0DrP
178
- func CountSort [T any ](slice []T , comparator lancetconstraints .Comparator ) []T {
178
+ func CountSort [T any ](slice []T , comparator constraints .Comparator ) []T {
179
179
size := len (slice )
180
180
out := make ([]T , size )
181
181
0 commit comments