-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathsort.go
38 lines (34 loc) · 875 Bytes
/
sort.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
package mysort
import "sort"
// BubbleSort takes a slice if int and sorts the elements
func BubbleSort(elements []int) {
keepWorking := true
for keepWorking {
//Comment out the following to test infinit loop
keepWorking = false
for i := 0; i < len(elements)-1; i++ {
if elements[i] < elements[i+1] {
keepWorking = true
elements[i], elements[i+1] = elements[i+1], elements[i]
}
}
}
}
// BubbleSortDesc changes to Decending order
func BubbleSortDesc(elements []int) {
keepWorking := true
for keepWorking {
//keepWorking = false
for i := 0; i < len(elements)-1; i++ {
keepWorking = false
if elements[i] < elements[i+1] {
keepWorking = true
elements[i], elements[i+1] = elements[i+1], elements[i]
}
}
}
}
// Sort in increasing order takes a var elements as a slice of ints
func Sort(elements []int) {
sort.Ints(elements)
}