Skip to content

Commit 320c980

Browse files
chin123zsparal
authored andcommitted
Add Go language to bubble sort (#157)
* Add Go language to bubble sort * add line range for Go Bubble Sort import * Update output format of Go Bubble Sort
1 parent 64a9d0f commit 320c980

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

book.json

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282
{
8383
"lang": "d",
8484
"name": "D"
85+
},
86+
{
87+
"lang": "go",
88+
"name": "Go"
8589
}
8690

8791
],

chapters/sorting_searching/bubble/bubble_sort.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
2626
[import:6-19, lang:"rust"](code/rust/bubble_sort.rs)
2727
{% sample lang="d" %}
2828
[import:3-18, lang:"d"](code/d/bubble_sort.d)
29+
{% sample lang="go" %}
30+
[import:7-21, lang:"go"](code/go/bubbleSort.go)
2931
{% endmethod %}
3032

3133
... And that's it for the simplest bubble sort method.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Submitted by Chinmaya Mahesh (chin123)
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func bubbleSort(array []int) {
8+
n := len(array)
9+
for i := 0; i < n-1; i++ {
10+
swapped := false
11+
for j := 0; j < n-i-1; j++ {
12+
if array[j] > array[j+1] {
13+
array[j], array[j+1] = array[j+1], array[j]
14+
swapped = true
15+
}
16+
}
17+
if !swapped {
18+
break
19+
}
20+
}
21+
}
22+
23+
func main() {
24+
array := [10]int{1, 45, 756, 4569, 56, 3, 8, 5, -10, -4}
25+
fmt.Println("Unsorted array:", array)
26+
27+
bubbleSort(array[:])
28+
29+
fmt.Println("Sorted array:", array)
30+
}

0 commit comments

Comments
 (0)