From da9a00e9ae8f599cdabb01f253d354af070d4d0e Mon Sep 17 00:00:00 2001 From: Chinmaya Krishnan Mahesh Date: Fri, 29 Jun 2018 01:07:31 +0530 Subject: [PATCH 1/3] Add Go language to bubble sort --- book.json | 4 ++ .../sorting_searching/bubble/bubble_sort.md | 2 + .../bubble/code/go/bubbleSort.go | 38 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 chapters/sorting_searching/bubble/code/go/bubbleSort.go 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 e4e806821..48c513658 100644 --- a/chapters/sorting_searching/bubble/bubble_sort.md +++ b/chapters/sorting_searching/bubble/bubble_sort.md @@ -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, 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..694f41e55 --- /dev/null +++ b/chapters/sorting_searching/bubble/code/go/bubbleSort.go @@ -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:") + for i := 0; i < len(array); i++ { + fmt.Print(array[i], " ") + } + fmt.Println() + + bubbleSort(array[:]) + + fmt.Println("Sorted array:") + for i := 0; i < len(array); i++ { + fmt.Print(array[i], " ") + } + fmt.Println() +} From 6e60716b624f11e89fa08de064c760920b69ca07 Mon Sep 17 00:00:00 2001 From: Chinmaya Krishnan Mahesh Date: Fri, 29 Jun 2018 01:23:37 +0530 Subject: [PATCH 2/3] add line range for Go Bubble Sort import --- chapters/sorting_searching/bubble/bubble_sort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/sorting_searching/bubble/bubble_sort.md b/chapters/sorting_searching/bubble/bubble_sort.md index 48c513658..ae303b00e 100644 --- a/chapters/sorting_searching/bubble/bubble_sort.md +++ b/chapters/sorting_searching/bubble/bubble_sort.md @@ -23,7 +23,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with {% sample lang="d" %} [import, lang:"d"](code/d/bubble_sort.d) {% sample lang="go" %} -[import, lang:"go"](code/go/bubbleSort.go) +[import:7-21, lang:"go"](code/go/bubbleSort.go) {% endmethod %} ... And that's it for the simplest bubble sort method. From 06f63aa00a61303c232a839438db098782d68e5a Mon Sep 17 00:00:00 2001 From: Chinmaya Krishnan Mahesh Date: Fri, 29 Jun 2018 12:39:51 +0530 Subject: [PATCH 3/3] Update output format of Go Bubble Sort --- .../sorting_searching/bubble/code/go/bubbleSort.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/chapters/sorting_searching/bubble/code/go/bubbleSort.go b/chapters/sorting_searching/bubble/code/go/bubbleSort.go index 694f41e55..df620fe5c 100644 --- a/chapters/sorting_searching/bubble/code/go/bubbleSort.go +++ b/chapters/sorting_searching/bubble/code/go/bubbleSort.go @@ -22,17 +22,9 @@ func bubbleSort(array []int) { func main() { array := [10]int{1, 45, 756, 4569, 56, 3, 8, 5, -10, -4} - fmt.Println("Unsorted array:") - for i := 0; i < len(array); i++ { - fmt.Print(array[i], " ") - } - fmt.Println() + fmt.Println("Unsorted array:", array) bubbleSort(array[:]) - fmt.Println("Sorted array:") - for i := 0; i < len(array); i++ { - fmt.Print(array[i], " ") - } - fmt.Println() + fmt.Println("Sorted array:", array) }