Last active
August 12, 2016 14:40
-
-
Save keenan-v1/e33f897ee5873bb342e41cfa217523c9 to your computer and use it in GitHub Desktop.
Sorting (Merge Sort vs Insertion Sort)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sort | |
func mergeSort(arr []int) (c []int) { | |
sz := len(arr) | |
if sz <= 1 { | |
return arr | |
} | |
s := int(sz / 2) | |
a, b := arr[:s], arr[s:] | |
la, lb := len(a), len(b) | |
if la > 1 || lb > 1 { | |
a = mergeSort(a) | |
b = mergeSort(b) | |
} else { | |
c = make([]int, la+lb) | |
for i, j, k := 0, 0, 0; k < la+lb; k++ { | |
if i < la && j < lb { | |
if a[i] < b[j] { | |
c[k] = a[i] | |
i++ | |
} else if b[j] < a[i] { | |
c[k] = b[j] | |
j++ | |
} else { | |
c[k] = b[j] | |
c[k+1] = a[i] | |
k++ | |
i++ | |
j++ | |
} | |
} else if i < la && j >= lb { | |
c[k] = a[i] | |
i++ | |
} else { | |
c[k] = b[j] | |
j++ | |
} | |
} | |
} | |
return | |
} | |
func insertionSort(arr []int) []int { | |
for i := 1; i < len(arr); i++ { | |
x := arr[i] | |
j := i - 1 | |
for j >= 0 && arr[j] > x { | |
arr[j+1] = arr[j] | |
j-- | |
} | |
arr[j+1] = x | |
} | |
return arr | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sort | |
import ( | |
"flag" | |
"math/rand" | |
"os" | |
"testing" | |
) | |
var ( | |
smallArr = []int{} | |
mediumArr = []int{} | |
largeArr = []int{} | |
) | |
func TestInsertionSortEven(t *testing.T) { | |
arr := []int{5, 8, 2, 4, 1, 7, 6, 3} | |
expected := []int{1, 2, 3, 4, 5, 6, 7, 8} | |
arr = insertionSort(arr) | |
t.Logf("arr = %v", arr) | |
for i := 0; i < len(arr); i++ { | |
if arr[i] != expected[i] { | |
t.Errorf("Expected %v, actual %v", expected, arr) | |
} | |
} | |
} | |
func TestMergeSortEven(t *testing.T) { | |
arr := []int{5, 8, 2, 4, 1, 7, 6, 3} | |
expected := []int{1, 2, 3, 4, 5, 6, 7, 8} | |
arr = mergeSort(arr) | |
t.Logf("arr = %v", arr) | |
for i := 0; i < len(arr); i++ { | |
if arr[i] != expected[i] { | |
t.Errorf("Expected %v, actual %v", expected, arr) | |
} | |
} | |
} | |
func TestInsertionSortOdd(t *testing.T) { | |
arr := []int{5, 8, 2, 4, 1, 9, 7, 6, 3} | |
expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} | |
arr = insertionSort(arr) | |
t.Logf("arr = %v", arr) | |
for i := 0; i < len(arr); i++ { | |
if arr[i] != expected[i] { | |
t.Errorf("Expected %v, actual %v", expected, arr) | |
} | |
} | |
} | |
func TestMergeSortOdd(t *testing.T) { | |
arr := []int{5, 8, 2, 4, 1, 9, 7, 6, 3} | |
expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} | |
arr = mergeSort(arr) | |
t.Logf("arr = %v", arr) | |
for i := 0; i < len(arr); i++ { | |
if arr[i] != expected[i] { | |
t.Errorf("Expected %v, actual %v", expected, arr) | |
} | |
} | |
} | |
func TestMain(m *testing.M) { | |
flag.Parse() | |
if !testing.Short() { | |
for i := 0; i < 10000; i++ { | |
smallArr = append(smallArr, rand.Intn(9999)) | |
} | |
for i := 0; i < 100000; i++ { | |
mediumArr = append(mediumArr, rand.Intn(9999)) | |
} | |
for i := 0; i < 500000; i++ { | |
largeArr = append(largeArr, rand.Intn(9999)) | |
} | |
} | |
os.Exit(m.Run()) | |
} | |
func BenchmarkSmallMS(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
mergeSort(smallArr) | |
} | |
} | |
func BenchmarkSmallIS(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
insertionSort(smallArr) | |
} | |
} | |
func BenchmarkMediumMS(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
mergeSort(mediumArr) | |
} | |
} | |
func BenchmarkMediumIS(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
insertionSort(mediumArr) | |
} | |
} | |
func BenchmarkLargeMS(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
mergeSort(largeArr) | |
} | |
} | |
func BenchmarkLargeIS(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
insertionSort(largeArr) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick update to exit early if there's no work to be done.
And another benchmark result:
As you can clearly see, the NS/OP for MS (merge sort) has dropped nicely. :)