Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Insertion sort #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions HeapSort.go
Original file line number Diff line number Diff line change
@@ -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())
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
120 changes: 120 additions & 0 deletions InsertionSort.go
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions IntroSort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package sort
18 changes: 18 additions & 0 deletions example_InsertionSortInt_test.go
Original file line number Diff line number Diff line change
@@ -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]
}
21 changes: 21 additions & 0 deletions example_InsertionSort_test.go
Original file line number Diff line number Diff line change
@@ -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)
}