Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -22,6 +22,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import, lang:"c_cpp"](code/c++/bubblesort.cpp)
{% sample lang="d" %}
[import, 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
38 changes: 38 additions & 0 deletions chapters/sorting_searching/bubble/code/go/bubbleSort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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:")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just fmt.Println("Unsorted array: ", array)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to this format in both the cases, thanks!

for i := 0; i < len(array); i++ {
fmt.Print(array[i], " ")
}
fmt.Println()

bubbleSort(array[:])

fmt.Println("Sorted array:")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, why not just: fmt.Println("Sorted array: ", array)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, its simpler this way, I changed it. Initially it was like that because I wanted it to be like the output format of the C bubble sort.

for i := 0; i < len(array); i++ {
fmt.Print(array[i], " ")
}
fmt.Println()
}