-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbleSort.go
35 lines (31 loc) · 849 Bytes
/
bubbleSort.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
package sort
import (
"github.com/syhv-git/cookbook/cmd"
types "github.com/syhv-git/cookbook/file"
)
// BubbleSort performs a bubble sort on t with b defining the vertical sort order and s defining the sort value
func BubbleSort(v, b bool, s string, t types.Tree) {
cmd.Log(v, "*** Starting bubble sort")
defer cmd.Log(v, "*** Ending bubble sort")
switch s {
case "dir":
bubbleSorter(v, b, t, dir)
case "mod":
bubbleSorter(v, b, t, mod)
case "name":
bubbleSorter(v, b, t, name)
case "size":
bubbleSorter(v, b, t, size)
default:
cmd.Log(v, "* Unknown sort type: %s", s)
}
}
func bubbleSorter[T constraint](v, b bool, t types.Tree, data T) {
for i := 0; i < len(t); i++ {
for j := 0; j < len(t)-i-1; j++ {
if handleDesc(v, b, data.handle(t[j+1]), data.handle(t[j]), data) {
t[j], t[j+1] = t[j+1], t[j]
}
}
}
}