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:
parent
916c027ac9
commit
bc53a37b35
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user