Skip to content

Commit faff377

Browse files
committed
modify insertion sort.
Signed-off-by: Jobin John <[email protected]>
1 parent 43ceb7e commit faff377

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

sorts/insertion_sort.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
package sorts
33

44
func InsertionSort(arr []int) []int {
5-
for out := 1; out < len(arr); out++ {
6-
temp := arr[out]
7-
in := out
8-
9-
for ; in > 0 && arr[in-1] >= temp; in-- {
10-
arr[in] = arr[in-1]
5+
for currentIndex := 1; currentIndex < len(arr); currentIndex++ {
6+
temporary := arr[currentIndex]
7+
iterator := currentIndex
8+
for ; iterator > 0 && arr[iterator-1] >= temporary; iterator-- {
9+
arr[iterator] = arr[iterator-1]
1110
}
12-
arr[in] = temp
11+
arr[iterator] = temporary
1312
}
1413
return arr
1514
}

0 commit comments

Comments
 (0)