sort

package module
v0.0.0-...-dd1ef72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2017 License: MIT Imports: 3 Imported by: 3

README

MIT Licence GoDoc Build Status Go Report Card

Sort

sort whatever you want

Index

Merge Sort

MergeSort()

MergeSort() is a custom Merge Sort function. You can define the comparion regulations all by yourself.

You should write your own compare function with definition below. func(i,j interface{})bool (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

here is a sample code:

package main
    
import (
  "github.com/goSTL/sort"
  "fmt"
)

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:

original: 	 [{5 sam} {3 lily} {7 jacky} {1 willy} {2 steve}]
[]interface{}: 	 [{1 willy} {2 steve} {3 lily} {5 sam} {7 jacky}]
[]student: 	 [{1 willy} {2 steve} {3 lily} {5 sam} {7 jacky}]

Quick Sort

QuickSort()

QuickSort() is a custom quick sort.

You should write your own compare function with definition below. func(i,j interface{})bool (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

here is a sample code:

package main
    
import (
  "github.com/goSTL/sort"
  "fmt"
)

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.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:

original: 	 [{5 sam} {3 lily} {7 jacky} {1 willy} {2 steve}]
[]interface{}: 	 [{1 willy} {2 steve} {3 lily} {5 sam} {7 jacky}]
[]student: 	 [{1 willy} {2 steve} {3 lily} {5 sam} {7 jacky}]

Documentation

Index

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 HeapSortFloat32

func HeapSortFloat32(dataS []float32)

FLOAT32

func HeapSortFloat64

func HeapSortFloat64(dataS []float64)

FLOAT64

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 HeapSortInt32

func HeapSortInt32(dataS []int32)

INT32

func HeapSortInt64

func HeapSortInt64(dataS []int64)

INT64

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL