From 921b040b547dcdfb20a8e4a06a4a3462805e6dcc Mon Sep 17 00:00:00 2001 From: Suradid Chao Date: Sat, 30 Oct 2021 22:22:55 +0700 Subject: [PATCH 1/3] add bubble sort --- Golang Programs/bubble_sort.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Golang Programs/bubble_sort.go diff --git a/Golang Programs/bubble_sort.go b/Golang Programs/bubble_sort.go new file mode 100644 index 0000000..0109e31 --- /dev/null +++ b/Golang Programs/bubble_sort.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +func bubbleSort(arr []int) []int { + for range arr { + for j, _ := range arr { + if j >= len(arr) - 1 { + break + } + if arr[j] > arr[j+1] { + temp := arr[j+1] + arr[j+1] = arr[j] + arr[j] = temp + } + } + } + + return arr +} + +func main() { + usArr := []int{5, 2, 6, 3, 1, 4} + sArr := bubbleSort(usArr) + fmt.Println(sArr) +} \ No newline at end of file From 100bf5082a6a341fc29e4c07ad533d0f9d1bc12f Mon Sep 17 00:00:00 2001 From: Suradid Chao Date: Sat, 30 Oct 2021 22:28:40 +0700 Subject: [PATCH 2/3] refactor bubble sort --- Golang Programs/bubble_sort.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Golang Programs/bubble_sort.go b/Golang Programs/bubble_sort.go index 0109e31..aa14b64 100644 --- a/Golang Programs/bubble_sort.go +++ b/Golang Programs/bubble_sort.go @@ -9,9 +9,7 @@ func bubbleSort(arr []int) []int { break } if arr[j] > arr[j+1] { - temp := arr[j+1] - arr[j+1] = arr[j] - arr[j] = temp + arr[j+1], arr[j] = arr[j], arr[j+1] } } } From fa6f2b537fda16b5ee057415a4cefc51c59859c7 Mon Sep 17 00:00:00 2001 From: Suradid Chao Date: Sat, 30 Oct 2021 22:32:12 +0700 Subject: [PATCH 3/3] add logging --- Golang Programs/bubble_sort.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Golang Programs/bubble_sort.go b/Golang Programs/bubble_sort.go index aa14b64..0ef3bd8 100644 --- a/Golang Programs/bubble_sort.go +++ b/Golang Programs/bubble_sort.go @@ -19,6 +19,7 @@ func bubbleSort(arr []int) []int { func main() { usArr := []int{5, 2, 6, 3, 1, 4} + fmt.Println("Before sort: ", usArr) sArr := bubbleSort(usArr) - fmt.Println(sArr) + fmt.Println("After sort: ", sArr) } \ No newline at end of file