2
0
mirror of https://github.com/0xAX/go-algorithms synced 2024-11-16 00:12:53 +00:00

Make insertion_sort.go a bit easier

This commit is contained in:
Georgy Komarov 2018-01-06 14:29:55 +03:00
parent 916c027ac9
commit bc53a37b35

View File

@ -12,18 +12,16 @@ func main() {
fmt.Println("Initial array is:", arr) fmt.Println("Initial array is:", arr)
fmt.Println("") fmt.Println("")
if (len(arr) <= 1) { if len(arr) <= 1 {
fmt.Println("Sorted array is: ", arr) fmt.Println("Sorted array is: ", arr)
return return
} }
for curr := 1; curr < len(arr); curr++ { var i, j int
curr_val := arr[curr] for i = 1; i < len(arr); i++ {
prev := curr - 1 for j = i; j > 0 && arr[j] < arr[j-1]; j-- {
for ; prev >= 0 && arr[prev] > curr_val; prev-- { arr[j], arr[j-1] = arr[j-1], arr[j]
arr[prev + 1] = arr[prev]
} }
arr[prev + 1] = curr_val
} }
fmt.Println("Sorted array is: ", arr) fmt.Println("Sorted array is: ", arr)