diff --git a/book.json b/book.json index dca995968..d32aedc1f 100644 --- a/book.json +++ b/book.json @@ -82,6 +82,10 @@ { "lang": "d", "name": "D" + }, + { + "lang": "go", + "name": "Go" } ], diff --git a/chapters/sorting_searching/bubble/bubble_sort.md b/chapters/sorting_searching/bubble/bubble_sort.md index b71e50ed3..4e9220b6f 100644 --- a/chapters/sorting_searching/bubble/bubble_sort.md +++ b/chapters/sorting_searching/bubble/bubble_sort.md @@ -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. diff --git a/chapters/sorting_searching/bubble/code/go/bubbleSort.go b/chapters/sorting_searching/bubble/code/go/bubbleSort.go new file mode 100644 index 000000000..df620fe5c --- /dev/null +++ b/chapters/sorting_searching/bubble/code/go/bubbleSort.go @@ -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) +}