mirror of
https://github.com/0xAX/go-algorithms
synced 2024-11-13 13:10:30 +00:00
Merge pull request #12 from srksumanth/sumanth
Improve loop syntax and format code
This commit is contained in:
commit
741916f31c
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user