10 examples of 'bubble sort algorithm' in Go

Every line of 'bubble sort algorithm' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
5func BubbleSort(arr []int) []int {
6 if len(arr) == 0 {
7 return arr
8 }
9 for j := len(arr); j > 0; j-- {
10 for i := 1; i < j; i++ {
11 if arr[i] > arr[i+1] {
12 swap(arr, i, i+1)
13 }
14 }
15 }
16 return arr
17}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

22func BubbleSort(arr []int) []int {
23 tmp := 0
24 for i := 0; i < len(arr); i++ {
25 for j := 0; j < len(arr)-i-1; j++ {
26 if arr[j] > arr[j+1] {
27 tmp = arr[j]
28 arr[j] = arr[j+1]
29 arr[j+1] = tmp
30 }
31 }
32 }
33 return arr
34}
14func bubbleSort(arrayzor []int) {
15
16 swapped := true;
17 for swapped {
18 swapped = false
19 for i := 0; i < len(arrayzor) - 1; i++ {
20 if arrayzor[i + 1] < arrayzor[i] {
21 algoutils.Swap(arrayzor, i, i + 1)
22 swapped = true
23 }
24 }
25 }
26}
20func BubbleBestSort(a []dataframe.Grade) []dataframe.Grade {
21 lastSwap := len(a) - 1
22 lastSwapTemp := len(a) - 1
23 for j := 0; j < len(a)-1; j++ {
24 lastSwap = lastSwapTemp
25 for i := 0; i < lastSwap; i++ {
26 if a[i].Score > a[i+1].Score {
27 a[i], a[i+1] = a[i+1], a[i]
28 lastSwapTemp = i
29 }
30 }
31 if lastSwap == lastSwapTemp {
32 break
33 }
34 }
35 return a
36}
22func BubbleSort(arr []int) {
23 n := len(arr)
24 for i := 0; i < n; i++ {
25 flag := false
26 for j := 0; j < n - i - 1; j++ {
27 if arr[j] > arr[j + 1] {
28 tmp := arr[j]
29 arr[j] = arr[j + 1];
30 arr[j + 1] = tmp
31 flag = true
32 }
33 }
34 if (!flag) {
35 break;
36 }
37 }
38}
33func heapSort(s1 []int, i int, s1Len int) []int {
34 done := false
35
36 tmp := 0
37 maxChild := 0
38
39 for (i*2+1 < s1Len) && (!done) {
40 if i*2+1 == s1Len-1 {
41 maxChild = i*2 + 1
42 } else if s1[i*2+1] > s1[i*2+2] {
43 maxChild = i*2 + 1
44 } else {
45 maxChild = i*2 + 2
46 }
47
48 if s1[i] < s1[maxChild] {
49 tmp = s1[i]
50 s1[i] = s1[maxChild]
51 s1[maxChild] = tmp
52 i = maxChild
53 } else {
54 done = true
55 }
56 fmt.Printf("Sorting ...:\t%v\n", s1)
57 }
58
59 return s1
60}
13func heapsort(array []int) {
14 ep := (len(array) - 1) >> 1
15 fmt.Println(ep)
16 for i := ep; i >= 0; i-- {
17 heapt(array, i, len(array)-1)
18 }
19
20 for i := len(array) - 1; i > 0; i-- {
21 array[0], array[i] = array[i], array[0]
22 heapt(array, 0, i-1)
23 }
24}
11func bubblesort(n []int) {
12 for i := 0; i < len(n) - 1; i++ {
13 for j := i + 1; j < len(n); j++ {
14 if n[j] < n[i] {
15 n[i], n[j] = n[j], n[i]
16 }
17 }
18 }
19}
34func bubbleSort(numbers []int) {
35 n := len(numbers)
36 for i := 0; i < n; i++ {
37 if !sweep(numbers, i) {
38 return
39 }
40 }
41}
18func MergeSort(arr []int) []int {
19 var alen = len(arr)
20 var ns = make([]int, alen, alen)
21
22 var tmp = &arr
23 var stmp = &ns
24 for add := 1; add < alen; add *= 2 {
25 fmt.Println(add)
26 var start1 = 0
27 var start2 = 0
28 ik := 0
29 for start1 = 0; start1 < alen; start1 = start1 + add {
30 start2 = start1 + add
31 var end1 = start1 + add
32 var end2 = start2 + add
33 for start1 < end1 && start2 < end2 && start1 < alen && start2 < alen {
34 if (*tmp)[start1] < (*tmp)[start2] {
35 (*stmp)[ik] = (*tmp)[start1]
36 start1++
37 } else {
38 (*stmp)[ik] = (*tmp)[start2]
39 start2++
40 }
41 ik++
42 fmt.Println(*stmp)
43 }
44
45 for start1 < end1 && start1 < alen {
46 (*stmp)[ik] = (*tmp)[start1]
47 start1++
48 ik++
49 fmt.Println(*stmp)
50 }
51
52 for start2 < end2 && start2 < alen {
53 (*stmp)[ik] = (*tmp)[start2]
54 start2++
55 ik++
56 fmt.Println(*stmp)
57 }
58 }
59 tmp, stmp = stmp, tmp
60 for index, _ := range *stmp {
61 (*stmp)[index] = 0
62 }
63 }
64 return (*tmp)
65}

Related snippets