Skip to content

Commit cc2521b

Browse files
committed
Refactor insertion sort
1 parent 543fa8d commit cc2521b

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

sorts/insertion_sort.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

sorts/insertionsort.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Reference: https://en.wikipedia.org/wiki/Insertion_sort
2+
3+
package sorts
4+
5+
func insertionSort(arr []int) []int {
6+
for currentIndex := 1; currentIndex < len(arr); currentIndex++ {
7+
temporary := arr[currentIndex]
8+
iterator := currentIndex
9+
for ; iterator > 0 && arr[iterator-1] >= temporary; iterator-- {
10+
arr[iterator] = arr[iterator-1]
11+
}
12+
arr[iterator] = temporary
13+
}
14+
return arr
15+
}

sorts/sorts_test.go

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

33
import "testing"
44

5-
func TestBubble(t *testing.T) {
5+
func TestBubbleSort(t *testing.T) {
66
for _, test := range sortTests {
77
actual := bubbleSort(test.input)
88
pos, sorted := compareSlices(actual, test.expected)
@@ -28,7 +28,7 @@ func TestCountingSort(t *testing.T) {
2828
}
2929
}
3030

31-
func TestSelection(t *testing.T) {
31+
func TestSelectionSort(t *testing.T) {
3232
for _, test := range sortTests {
3333
actual := selectionSort(test.input)
3434
pos, sorted := compareSlices(actual, test.expected)
@@ -41,7 +41,7 @@ func TestSelection(t *testing.T) {
4141
}
4242
}
4343

44-
func TestInsertion(t *testing.T) {
44+
func TestInsertionSort(t *testing.T) {
4545
for _, test := range sortTests {
4646
actual := insertionSort(test.input)
4747
pos, sorted := compareSlices(actual, test.expected)
@@ -54,7 +54,7 @@ func TestInsertion(t *testing.T) {
5454
}
5555
}
5656

57-
func TestMerge(t *testing.T) {
57+
func TestMergeSort(t *testing.T) {
5858
for _, test := range sortTests {
5959
actual := Mergesort(test.input)
6060
pos, sorted := compareSlices(actual, test.expected)
@@ -67,7 +67,7 @@ func TestMerge(t *testing.T) {
6767
}
6868
}
6969

70-
func TestHeap(t *testing.T) {
70+
func TestHeapSort(t *testing.T) {
7171
for _, test := range sortTests {
7272
actual := heapSort(test.input)
7373
pos, sorted := compareSlices(actual, test.expected)
@@ -80,7 +80,7 @@ func TestHeap(t *testing.T) {
8080
}
8181
}
8282

83-
func TestQuick(t *testing.T) {
83+
func TestQuickSort(t *testing.T) {
8484
for _, test := range sortTests {
8585
actual := quickSort(test.input)
8686
pos, sorted := compareSlices(actual, test.expected)
@@ -93,7 +93,7 @@ func TestQuick(t *testing.T) {
9393
}
9494
}
9595

96-
func TestShell(t *testing.T) {
96+
func TestShellSort(t *testing.T) {
9797
for _, test := range sortTests {
9898
actual := shellSort(test.input)
9999
pos, sorted := compareSlices(actual, test.expected)

0 commit comments

Comments
 (0)