Skip to content

Commit 1e1d47e

Browse files
AmrDevelopertstreamDOTh
authored andcommittedOct 26, 2018
Implement BubbleSort in Go (#360)
* Implement BubbleSort in Go Implement Bubble Sort Algorithm in Go Programming Language * Update BubbleSort.go
1 parent fbe380e commit 1e1d47e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎Sorting/Bubble Sort/Go/BubbleSort.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
//Test Code
7+
array := []int{1, 6, 2, 4, 9, 0, 5, 3, 7, 8}
8+
fmt.Println("Unsorted array: ", array)
9+
sort(array)
10+
fmt.Println("Sorted array: ", array)
11+
}
12+
13+
func sort(arr []int) {
14+
for i := 0; i < len(arr); i++ {
15+
for j := 0; j < i; j++ {
16+
if arr[j] > arr[i] {
17+
swap(arr, i, j)
18+
}
19+
}
20+
}
21+
}
22+
23+
func swap(arr []int, x, y int) {
24+
temp := arr[x]
25+
arr[x] = arr[y]
26+
arr[y] = temp
27+
}

0 commit comments

Comments
 (0)
Please sign in to comment.