Documentation ¶
Index ¶
- func HeapSort(data interface{}, cmp func(i, j interface{}) bool) []interface{}
- func HeapSortFloat32(dataS []float32)
- func HeapSortFloat64(dataS []float64)
- func HeapSortInt(dataS []int)
- func HeapSortInt32(dataS []int32)
- func HeapSortInt64(dataS []int64)
- func MergeSort(data interface{}, cmp func(i, j interface{}) bool) []interface{}
- func MergeSortFloat32(data []float32)
- func MergeSortFloat64(data []float64)
- func MergeSortInt(data []int)
- func MergeSortInt32(data []int32)
- func MergeSortInt64(data []int64)
- func MergeSortPara(data interface{}, cmp func(i, j interface{}) bool) []interface{}
- func QuickSort(data interface{}, cmp func(i, j interface{}) bool) []interface{}
- func QuickSortFloat32(data []float32)
- func QuickSortFloat64(data []float64)
- func QuickSortInt(data []int)
- func QuickSortInt32(data []int32)
- func QuickSortInt64(data []int64)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HeapSort ¶
func HeapSort(data interface{}, cmp func(i, j interface{}) bool) []interface{}
Example ¶
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.HeapSort(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)
Output:
func HeapSortInt ¶
func HeapSortInt(dataS []int)
INT
Example ¶
package main import ( "fmt" "github.com/goSTL/sort" ) func main() { sample := []int{3, 138, 1, 674, 213, 23, 5, 2} fmt.Println(sample) sort.HeapSortInt(sample) fmt.Println(sample) }
Output: [3 138 1 674 213 23 5 2] [1 2 3 5 23 138 213 674]
func MergeSort ¶
func MergeSort(data interface{}, cmp func(i, j interface{}) bool) []interface{}
MergeSort is a custom Merge Sort. You can define the comparion regulations all by yourself.
You should write your own compare function with definition " func(i,j interface{})bool " then MergeSort() will launch Merge Sort with the regulation you define. (*you can use i.(YourInputType) to convert i to original type)
The output will be a slice of interface{},you should use o.(YourInputType) to convert o to origibal type.
Example ¶
package main import ( "fmt" "github.com/goSTL/sort" ) type student struct { id int name string } func cmp(i, j interface{}) bool { ii := i.(student) jj := j.(student) return ii.id < jj.id } func main() { stu := []student{{5, "sam"}, {3, "lily"}, {7, "jacky"}, {1, "willy"}, {2, "steve"}} fmt.Println("original: \t", stu) out := sort.MergeSort(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) }
Output:
func MergeSortFloat32 ¶
func MergeSortFloat32(data []float32)
MergeSortFloat32 is a MergeSort function for float32, sorting from smallest to biggest.
func MergeSortFloat64 ¶
func MergeSortFloat64(data []float64)
MergeSortFloat64 is a MergeSort function for float64, sorting from smallest to biggest.
func MergeSortInt ¶
func MergeSortInt(data []int)
MergeSortInt is a MergeSort function for int, sorting from smallest to biggest.
Example ¶
package main import ( "fmt" "github.com/goSTL/sort" ) func main() { sample := []int{5, 7, 4, 10, 99, 2, 72, 37} fmt.Println(sample) sort.MergeSortInt(sample) fmt.Println(sample) }
Output: [5 7 4 10 99 2 72 37] [2 4 5 7 10 37 72 99]
func MergeSortInt32 ¶
func MergeSortInt32(data []int32)
MergeSortInt32 is a MergeSort function for int32, sorting from smallest to biggest.
func MergeSortInt64 ¶
func MergeSortInt64(data []int64)
MergeSortInt64 is a MergeSort function for int64, sorting from smallest to biggest.
func MergeSortPara ¶
func MergeSortPara(data interface{}, cmp func(i, j interface{}) bool) []interface{}
MergeSortPara is a custom Merge Sort with parallel compution in 8 threads. You can define the comparion regulations all by yourself.
You should use runtime.GOMAXPROS(int) to set the maximum number of CPUs that can be executing simultaneously.
func QuickSort ¶
func QuickSort(data interface{}, cmp func(i, j interface{}) bool) []interface{}
QuickSort is a custom quick sort. You should build your own compare function to sort in your own regulations.
Example ¶
stu := []student{{5, "sam"}, {3, "lily"}, {7, "jacky"}, {1, "willy"}, {2, "steve"}} fmt.Println("original: \t", stu) out := sort.QuickSort(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)
Output:
func QuickSortFloat32 ¶
func QuickSortFloat32(data []float32)
QuickSortFloat32 is a QuickSort function for float32, sorting from smallest to biggest.
func QuickSortFloat64 ¶
func QuickSortFloat64(data []float64)
QuickSortFloat64 is a QuickSort function for float64, sorting from smallest to biggest.
func QuickSortInt ¶
func QuickSortInt(data []int)
QuickSortInt is a QuickSort function for int, sorting from smallest to biggest.
Example ¶
package main import ( "fmt" "github.com/goSTL/sort" ) func main() { sample := []int{5, 7, 4, 10, 99, 2, 72, 37} fmt.Println(sample) sort.QuickSortInt(sample) fmt.Println(sample) }
Output: [5 7 4 10 99 2 72 37] [2 4 5 7 10 37 72 99]
func QuickSortInt32 ¶
func QuickSortInt32(data []int32)
QuickSortInt32 is a QuickSort function for int32, sorting from smallest to biggest.
func QuickSortInt64 ¶
func QuickSortInt64(data []int64)
QuickSortInt64 is a QuickSort function for int64, sorting from smallest to biggest.
Types ¶
This section is empty.