Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
{
"lang": "d",
"name": "D"
},
{
"lang": "go",
"name": "Go"
}

],
Expand Down
2 changes: 2 additions & 0 deletions chapters/sorting_searching/bubble/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:6-19, lang:"rust"](code/rust/bubble_sort.rs)
{% sample lang="d" %}
[import:3-18, lang:"d"](code/d/bubble_sort.d)
{% sample lang="go" %}
[import:7-21, lang:"go"](code/go/bubbleSort.go)
{% endmethod %}

... And that's it for the simplest bubble sort method.
Expand Down
30 changes: 30 additions & 0 deletions chapters/sorting_searching/bubble/code/go/bubbleSort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Submitted by Chinmaya Mahesh (chin123)

package main

import "fmt"

func bubbleSort(array []int) {
n := len(array)
for i := 0; i < n-1; i++ {
swapped := false
for j := 0; j < n-i-1; j++ {
if array[j] > array[j+1] {
array[j], array[j+1] = array[j+1], array[j]
swapped = true
}
}
if !swapped {
break
}
}
}

func main() {
array := [10]int{1, 45, 756, 4569, 56, 3, 8, 5, -10, -4}
fmt.Println("Unsorted array:", array)

bubbleSort(array[:])

fmt.Println("Sorted array:", array)
}