This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package query | |
func SortQuery(objPtr interface{}) (res []string, err error) { | |
reflectObjType := reflect.TypeOf(objPtr) | |
if reflectObjType.Kind() != reflect.Ptr { | |
return nil, errors.New("objPtr can only be pointers") | |
} | |
if reflectObjType.Elem().Kind() != reflect.Struct { | |
return nil, errors.New("objPtr can only be struct") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package query | |
func Sort(nums []int) []int { | |
numberBit := howManyBit(maximum(nums)) | |
// 循环的次数 | |
// 定义一个rec 二维切片 rec[i][x] 用来接受尾数是 i的数字 | |
for i := 0; i < numberBit; i++ { | |
rec := make([][]int, 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package compare | |
type signed interface { | |
~int | ~int8 | ~int16 | ~int32 | ~int64 | |
} | |
type unsigned interface { | |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package query | |
func Sort[T constraints.Ordered](buf []T) { | |
for i := 0; i < len(buf)-1; i++ { | |
flag := false | |
for j := 1; j < len(buf)-i; j++ { | |
if buf[j-1] > buf[j] { | |
tmp := buf[j-1] | |
buf[j-1] = buf[j] | |
buf[j] = tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package query | |
func Sort[T constraints.Ordered](buf []T) { | |
for i := 0; i < len(buf)-1; i++ { | |
min := i | |
for j := i; j < len(buf); j++ { | |
if buf[min] > buf[j] { | |
min = j | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Sort[T constraints.Ordered](buf []T) { | |
for i := 1; i < len(buf); i++ { | |
for j := i; j > 0; j-- { | |
if buf[j] < buf[j-1] { | |
tmp := buf[j-1] | |
buf[j-1] = buf[j] | |
buf[j] = tmp | |
} else { | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Sort[T constraints.Ordered](buf []T) { | |
tmp := make([]T, len(buf)) | |
merge_sort[T](buf, 0, len(buf)-1, tmp) | |
} | |
func merge_sort[T constraints.Ordered](a []T, first, last int, tmp []T) { | |
if first < last { | |
middle := (first + last) / 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package quicksort | |
func Sort[T constraints.Ordered](buf []T) { | |
quick(buf, 0, len(buf)-1) | |
} | |
func quick[T constraints.Ordered](a []T, l, r int) { | |
if l >= r { | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package shellsort | |
func Sort[T constraints.Ordered](buf []T) { | |
length := len(buf) | |
incre := length | |
for { | |
incre /= 2 | |
for k := 0; k < incre; k++ { | |
for i := k + incre; i < length; i += incre { | |
for j := i; j > k; j -= incre { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pachage heapsort | |
func Sort[T constraints.Ordered](buf []T) { | |
n := len(buf) | |
for i := (n - 1) / 2; i >= 0; i-- { | |
minHeapFixdown[T](buf, i, n) | |
} | |
for i := n - 1; i > 0; i-- { | |
temp := buf[0] |
OlderNewer