From da18b553ef541a2b69cc3d3fe428c4ce0ec96d1b Mon Sep 17 00:00:00 2001
From: Gavin Lee <sz110010@gmail.com>
Date: Fri, 17 Mar 2017 00:30:31 +0800
Subject: [PATCH 1/3] Add Insertion sort

---
 HeapSort.go                      |  37 +++++++++-
 InsertionSort.go                 | 119 +++++++++++++++++++++++++++++++
 IntroSort.go                     |   1 +
 example_InsertionSortInt_test.go |  18 +++++
 example_InsertionSort_test.go    |  26 +++++++
 5 files changed, 198 insertions(+), 3 deletions(-)
 create mode 100644 InsertionSort.go
 create mode 100644 IntroSort.go
 create mode 100644 example_InsertionSortInt_test.go
 create mode 100644 example_InsertionSort_test.go

diff --git a/HeapSort.go b/HeapSort.go
index 8f848c6..1fc96cf 100644
--- a/HeapSort.go
+++ b/HeapSort.go
@@ -1,9 +1,15 @@
 package sort
 
-import (
-	"reflect"
-)
+import "reflect"
 
+// This is a custom Heap Sort
+// You MUST define the compare function by yourself
+// compare function type: func(i,j interface{}) bool
+//
+// input: func([]interface{}, func(i,j interface{}) bool) []interface{}
+//
+// output slice and won't change the origin one
+// data = HeapSort(data, cmp), if you want to change origin slice
 func HeapSort(data interface{}, cmp func(i, j interface{}) bool) []interface{} {
 	value := reflect.ValueOf(data)
 	dataS := make([]interface{}, value.Len())
@@ -56,6 +62,11 @@ func maxHeap(data []interface{}, len int, cmp func(i, j interface{}) bool) {
 }
 
 // INT
+// Heap Sort in Int type, sorting from small to big
+//
+// Input slice func(Int[])
+//
+// No output, just change origin slice
 func HeapSortInt(dataS []int) {
 	heapifyInt(dataS, len(dataS)/2-1, len(dataS))
 	maxHeapInt(dataS, len(dataS))
@@ -102,6 +113,11 @@ func maxHeapInt(data []int, len int) {
 }
 
 // INT32
+// Heap Sort in Int32 type, sorting from small to big
+//
+// Input slice func(Int32[])
+//
+// No output, just change origin slice
 func HeapSortInt32(dataS []int32) {
 	heapifyInt32(dataS, len(dataS)/2-1, len(dataS))
 	maxHeapInt32(dataS, len(dataS))
@@ -148,6 +164,11 @@ func maxHeapInt32(data []int32, len int) {
 }
 
 // INT64
+// Heap Sort in Int64 type, sorting from small to big
+//
+// Input slice func(Int64[])
+//
+// No output, just change origin slice
 func HeapSortInt64(dataS []int64) {
 	heapifyInt64(dataS, len(dataS)/2-1, len(dataS))
 	maxHeapInt64(dataS, len(dataS))
@@ -194,6 +215,11 @@ func maxHeapInt64(data []int64, len int) {
 }
 
 // FLOAT32
+// Heap Sort in Float32 type, sorting from small to big
+//
+// Input slice func(Float32[])
+//
+// No output, just change origin slice
 func HeapSortFloat32(dataS []float32) {
 	heapifyFloat32(dataS, len(dataS)/2-1, len(dataS))
 	maxHeapFloat32(dataS, len(dataS))
@@ -240,6 +266,11 @@ func maxHeapFloat32(data []float32, len int) {
 }
 
 // FLOAT64
+// Heap Sort in Float64 type, sorting from small to big
+//
+// Input slice func(Float64[])
+//
+// No output, just change origin slice
 func HeapSortFloat64(dataS []float64) {
 	heapifyFloat64(dataS, len(dataS)/2-1, len(dataS))
 	maxHeapFloat64(dataS, len(dataS))
diff --git a/InsertionSort.go b/InsertionSort.go
new file mode 100644
index 0000000..390c2be
--- /dev/null
+++ b/InsertionSort.go
@@ -0,0 +1,119 @@
+package sort
+
+import "reflect"
+
+// This is a custom Merge Sort
+// You MUST define the compare function by yourself
+// compare function type: func(i,j interface{}) bool
+//
+// input: func([]interface{}, func(i,j interface{}) bool) []interface{}
+//
+// output slice and won't change the origin one
+// data = InsertionSort(data, cmp), if you want to change origin slice
+func InsertionSort(data interface{}, cmp func(i, j interface{}) bool) []interface{} {
+	value := reflect.ValueOf(data)
+	dataS := make([]interface{}, value.Len())
+	len := value.Len()
+	for a := 0; a < len; a++ {
+		dataS[a] = value.Index(a).Interface()
+	}
+	for i := 1; i < len; i++ {
+		tmp := dataS[i]
+		j := i - 1
+		for ; j >= 0 && cmp(dataS[j], tmp); j-- {
+			dataS[j+1] = dataS[j]
+		}
+		dataS[j+1] = tmp
+	}
+	return dataS
+}
+
+// INT
+// Insertion Sort in Int type, sorting from small to big
+//
+// Input slice func([]int)
+//
+// No output, just change origin slice
+func InsertionSortInt(dataS []int) {
+	len := len(dataS)
+	for i := 1; i < len; i++ {
+		tmp := dataS[i]
+		j := i - 1
+		for ; j >= 0 && dataS[j] > tmp; j-- {
+			dataS[j+1] = dataS[j]
+		}
+		dataS[j+1] = tmp
+	}
+}
+
+// INT32
+// Insertion Sort in Int32 type, sorting from small to big
+//
+// Input slice func([]int32)
+//
+// No output, just change origin slice
+func InsertionSortInt32(dataS []int32) {
+	len := len(dataS)
+	for i := 1; i < len; i++ {
+		tmp := dataS[i]
+		j := i - 1
+		for ; j >= 0 && dataS[j] > tmp; j-- {
+			dataS[j+1] = dataS[j]
+		}
+		dataS[j+1] = tmp
+	}
+}
+
+// INT64
+// Insertion Sort in Int64 type, sorting from small to big
+//
+// Input slice func([]int64)
+//
+// No output, just change origin slice
+func InsertionSortInt64(dataS []int64) {
+	len := len(dataS)
+	for i := 1; i < len; i++ {
+		tmp := dataS[i]
+		j := i - 1
+		for ; j >= 0 && dataS[j] > tmp; j-- {
+			dataS[j+1] = dataS[j]
+		}
+		dataS[j+1] = tmp
+	}
+}
+
+// FLOAT32
+// Insertion Sort in Float32 type, sorting from small to big
+//
+// Input slice func([]float32)
+//
+// No output, just change origin slice
+func InsertionSortFloat32(dataS []float32) {
+	len := len(dataS)
+	for i := 1; i < len; i++ {
+		tmp := dataS[i]
+		j := i - 1
+		for ; j >= 0 && dataS[j] > tmp; j-- {
+			dataS[j+1] = dataS[j]
+		}
+		dataS[j+1] = tmp
+	}
+}
+
+// FLOAT64
+// Insertion Sort in Float64 type, sorting from small to big
+//
+// Input slice func([]float64)
+//
+// No output, just change origin slice
+func InsertionSortFloat64(dataS []float64) {
+	len := len(dataS)
+	for i := 1; i < len; i++ {
+		tmp := dataS[i]
+		j := i - 1
+		for ; j >= 0 && dataS[j] > tmp; j-- {
+			dataS[j+1] = dataS[j]
+		}
+		dataS[j+1] = tmp
+	}
+}
diff --git a/IntroSort.go b/IntroSort.go
new file mode 100644
index 0000000..24478dd
--- /dev/null
+++ b/IntroSort.go
@@ -0,0 +1 @@
+package sort
diff --git a/example_InsertionSortInt_test.go b/example_InsertionSortInt_test.go
new file mode 100644
index 0000000..bcc2b38
--- /dev/null
+++ b/example_InsertionSortInt_test.go
@@ -0,0 +1,18 @@
+package sort_test
+
+import (
+	"fmt"
+
+	"github.com/goSTL/sort"
+)
+
+func ExampleInsertionSortInt() {
+	sample := []int{3, 138, 1, 674, 213, 23, 5, 2}
+	fmt.Println(sample)
+
+	sort.InsertionSortInt(sample)
+	fmt.Println(sample)
+	//Output:
+	//[3 138 1 674 213 23 5 2]
+	//[1 2 3 5 23 138 213 674]
+}
diff --git a/example_InsertionSort_test.go b/example_InsertionSort_test.go
new file mode 100644
index 0000000..571b248
--- /dev/null
+++ b/example_InsertionSort_test.go
@@ -0,0 +1,26 @@
+package sort_test
+
+import (
+	"fmt"
+
+	"github.com/goSTL/sort"
+)
+
+type student struct {
+	num  int
+	name string
+}
+
+func ExampleInsertionSort() {
+	stu := []student{{5, "sam"}, {3, "lily"}, {7, "jacky"}, {1, "willy"}, {2, "steve"}, {3, "kally"}, {1, "gay"}, {-1, "10"}}
+	fmt.Println("original: \t", stu)
+
+	out := sort.InsertionSort(stu, cmp)
+	fmt.Println("[]interface{}: \t", out)
+
+	stu2 := make([]student, len(out))
+	for a := 0; a < len(out); a++ {
+		stu2[a] = out[a].(student)
+	}
+	fmt.Println("[]student: \t", stu2)
+}

From 298e575030524bc1c4b05bf8ec840167b9e00368 Mon Sep 17 00:00:00 2001
From: Gavin Lee <sz110010@gmail.com>
Date: Fri, 17 Mar 2017 00:35:26 +0800
Subject: [PATCH 2/3] Fix bug of Insertion test file, InsertionSort()

forget to rm the student type of Insertion test file
---
 example_InsertionSort_test.go | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/example_InsertionSort_test.go b/example_InsertionSort_test.go
index 571b248..bb5488e 100644
--- a/example_InsertionSort_test.go
+++ b/example_InsertionSort_test.go
@@ -6,11 +6,6 @@ import (
 	"github.com/goSTL/sort"
 )
 
-type student struct {
-	num  int
-	name string
-}
-
 func ExampleInsertionSort() {
 	stu := []student{{5, "sam"}, {3, "lily"}, {7, "jacky"}, {1, "willy"}, {2, "steve"}, {3, "kally"}, {1, "gay"}, {-1, "10"}}
 	fmt.Println("original: \t", stu)

From eaed3969e7bf535d39d8442299edda7fa5c9d78d Mon Sep 17 00:00:00 2001
From: Gavin Lee <sz110010@gmail.com>
Date: Fri, 17 Mar 2017 11:20:16 +0800
Subject: [PATCH 3/3] Fix comment of func name and Insertion sort

---
 HeapSort.go      | 11 ++++++-----
 InsertionSort.go | 13 +++++++------
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/HeapSort.go b/HeapSort.go
index 1fc96cf..0173ea8 100644
--- a/HeapSort.go
+++ b/HeapSort.go
@@ -2,6 +2,7 @@ package sort
 
 import "reflect"
 
+// HeapSort
 // This is a custom Heap Sort
 // You MUST define the compare function by yourself
 // compare function type: func(i,j interface{}) bool
@@ -61,7 +62,7 @@ func maxHeap(data []interface{}, len int, cmp func(i, j interface{}) bool) {
 	return
 }
 
-// INT
+// HeapSortInt
 // Heap Sort in Int type, sorting from small to big
 //
 // Input slice func(Int[])
@@ -112,7 +113,7 @@ func maxHeapInt(data []int, len int) {
 	return
 }
 
-// INT32
+// HeapSortInt32
 // Heap Sort in Int32 type, sorting from small to big
 //
 // Input slice func(Int32[])
@@ -163,7 +164,7 @@ func maxHeapInt32(data []int32, len int) {
 	return
 }
 
-// INT64
+// HeapSortInt64
 // Heap Sort in Int64 type, sorting from small to big
 //
 // Input slice func(Int64[])
@@ -214,7 +215,7 @@ func maxHeapInt64(data []int64, len int) {
 	return
 }
 
-// FLOAT32
+// HeapSortFloat32
 // Heap Sort in Float32 type, sorting from small to big
 //
 // Input slice func(Float32[])
@@ -265,7 +266,7 @@ func maxHeapFloat32(data []float32, len int) {
 	return
 }
 
-// FLOAT64
+// HeapSortFloat64
 // Heap Sort in Float64 type, sorting from small to big
 //
 // Input slice func(Float64[])
diff --git a/InsertionSort.go b/InsertionSort.go
index 390c2be..f00c68a 100644
--- a/InsertionSort.go
+++ b/InsertionSort.go
@@ -2,7 +2,8 @@ package sort
 
 import "reflect"
 
-// This is a custom Merge Sort
+// InsertionSort
+// This is a custom Insertion Sort
 // You MUST define the compare function by yourself
 // compare function type: func(i,j interface{}) bool
 //
@@ -28,7 +29,7 @@ func InsertionSort(data interface{}, cmp func(i, j interface{}) bool) []interfac
 	return dataS
 }
 
-// INT
+// InsertionSortInt
 // Insertion Sort in Int type, sorting from small to big
 //
 // Input slice func([]int)
@@ -46,7 +47,7 @@ func InsertionSortInt(dataS []int) {
 	}
 }
 
-// INT32
+// InsertionSortInt32
 // Insertion Sort in Int32 type, sorting from small to big
 //
 // Input slice func([]int32)
@@ -64,7 +65,7 @@ func InsertionSortInt32(dataS []int32) {
 	}
 }
 
-// INT64
+// InsertionSortInt64
 // Insertion Sort in Int64 type, sorting from small to big
 //
 // Input slice func([]int64)
@@ -82,7 +83,7 @@ func InsertionSortInt64(dataS []int64) {
 	}
 }
 
-// FLOAT32
+// InsertionSortFloat32
 // Insertion Sort in Float32 type, sorting from small to big
 //
 // Input slice func([]float32)
@@ -100,7 +101,7 @@ func InsertionSortFloat32(dataS []float32) {
 	}
 }
 
-// FLOAT64
+// InsertionSortFloat64
 // Insertion Sort in Float64 type, sorting from small to big
 //
 // Input slice func([]float64)