10 examples of 'merging sort' in Go

Every line of 'merging sort' 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
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}
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

17func Sort(arr []int) []int {
18 for i := 0; i < len(arr); i++ {
19 min := i
20 for j := i + 1; j < len(arr); j++ {
21 if arr[j] < arr[min] {
22 min = j
23 }
24 }
25 if min != i {
26 arr[i], arr[min] = arr[min], arr[i]
27 }
28 }
29 return arr
30}
7func Sort(arr []int) []int{
8 if len(arr) <= 1 {
9 return arr
10 }
11
12 n := len(arr) - 1
13
14 for {
15 if n == 0 {
16 break
17 }
18
19 for i := 0; i < len(arr)-1; i++ {
20 if arr[i] > arr[i+1] {
21 arr[i], arr[i+1] = arr[i+1], arr[i]
22 }
23 }
24 n -= 1
25 }
26
27 return arr
28}
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}
7func mergeSort(arr []int, left, right int) {
8 if left >= right {
9 return
10 }
11
12 mid := left + (right-left)/2
13 mergeSort(arr, left, mid)
14 mergeSort(arr, mid+1, right)
15 merge(arr, left, mid, right)
16}
6func mergeSort(array []int) []int {
7 n := len(array)
8 if n < 2 {
9 return array
10 }
11
12 key := n / 2
13 left := mergeSort(array[0:key])
14 right := mergeSort(array[key:])
15 return merge(left, right)
16}
5func mergeSort(a []int) []int {
6
7 if len(a) <= 1 {
8 return a
9 }
10
11 left := make([]int, 0)
12 right := make([]int, 0)
13 m := len(a) / 2
14
15 for i, x := range a {
16 switch {
17 case i < m:
18 left = append(left, x)
19 case i >= m:
20 right = append(right, x)
21 }
22 }
23
24 left = mergeSort(left)
25 right = mergeSort(right)
26
27 return merge(left, right)
28}
22func sort(a, b, c int) (int, int, int) {
23 if c < a {
24 a, c = c, a
25 }
26 if b < a {
27 a, b = b, a
28 }
29 if c < b {
30 b, c = c, b
31 }
32 return a, b, c
33}
54func sort(a, b int) (int, int) {
55 if a < b {
56 return a, b
57 }
58 return b, a
59}
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}

Related snippets