From 5c41ed1d14bc5c6b272dbf536e4f137e2d805bcd Mon Sep 17 00:00:00 2001 From: srksumanth Date: Thu, 1 Mar 2018 13:37:08 +0530 Subject: [PATCH] Improve loop syntax and format code --- sorting/cocktail_sort.go | 60 ++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/sorting/cocktail_sort.go b/sorting/cocktail_sort.go index f09750e..f9814c3 100644 --- a/sorting/cocktail_sort.go +++ b/sorting/cocktail_sort.go @@ -9,35 +9,35 @@ import "fmt" import "github.com/0xAX/go-algorithms" func main() { - arr := utils.RandArray(10) - fmt.Println("Initial array is:", arr) - fmt.Println("") - - tmp := 0 - - for i := 0; i < len(arr) / 2; i++ { - left := 0 - right := len(arr) - 1 - - for ; left <= right ; { - - if arr[left] > arr[left + 1] { - tmp = arr[left] - arr[left] = arr[left + 1] - arr[left + 1] = tmp - } - - left++ - - if arr[right - 1] > arr[right] { - tmp = arr[right - 1] - arr[right - 1] = arr[right] - arr[right] = tmp - } + arr := utils.RandArray(10) + fmt.Println("Initial array is:", arr) + fmt.Println("") - right-- - } - } - - fmt.Println("Sorted array is: ", arr) + tmp := 0 + + for i := 0; i < len(arr)/2; i++ { + left := 0 + right := len(arr) - 1 + + for left <= right { + + if arr[left] > arr[left+1] { + tmp = arr[left] + arr[left] = arr[left+1] + arr[left+1] = tmp + } + + left++ + + if arr[right-1] > arr[right] { + tmp = arr[right-1] + arr[right-1] = arr[right] + arr[right] = tmp + } + + right-- + } + } + + fmt.Println("Sorted array is: ", arr) }