diff --git a/HeapSort.go b/HeapSort.go index 8f848c6..0173ea8 100644 --- a/HeapSort.go +++ b/HeapSort.go @@ -1,9 +1,16 @@ package sort -import ( - "reflect" -) +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 +// +// 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()) @@ -55,7 +62,12 @@ 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[]) +// +// No output, just change origin slice func HeapSortInt(dataS []int) { heapifyInt(dataS, len(dataS)/2-1, len(dataS)) maxHeapInt(dataS, len(dataS)) @@ -101,7 +113,12 @@ func maxHeapInt(data []int, len int) { return } -// INT32 +// HeapSortInt32 +// 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)) @@ -147,7 +164,12 @@ func maxHeapInt32(data []int32, len int) { return } -// INT64 +// HeapSortInt64 +// 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)) @@ -193,7 +215,12 @@ func maxHeapInt64(data []int64, len int) { return } -// FLOAT32 +// HeapSortFloat32 +// 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)) @@ -239,7 +266,12 @@ func maxHeapFloat32(data []float32, len int) { return } -// FLOAT64 +// HeapSortFloat64 +// 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..f00c68a --- /dev/null +++ b/InsertionSort.go @@ -0,0 +1,120 @@ +package sort + +import "reflect" + +// InsertionSort +// This is a custom Insertion 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 +} + +// InsertionSortInt +// 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 + } +} + +// InsertionSortInt32 +// 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 + } +} + +// InsertionSortInt64 +// 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 + } +} + +// InsertionSortFloat32 +// 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 + } +} + +// InsertionSortFloat64 +// 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..bb5488e --- /dev/null +++ b/example_InsertionSort_test.go @@ -0,0 +1,21 @@ +package sort_test + +import ( + "fmt" + + "github.com/goSTL/sort" +) + +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) +}