// 希尔排序, 时间复杂度O(n^1.3), 空间复杂度O(1)funcShellSort(a []int){// 初始步长
h :=1for h <len(a)/3{
h = h*3+1}for h >=1{for i := h; i <len(a); i++{for j := i; j >= h && a[j]< a[j-h]; j -= h {
a[j], a[j-h]= a[j-h], a[j]}}
h /=3}}
归并排序
// 归并排序, 时间复杂度O(nlogn), 空间复杂度O(n)funcMergeSort(a []int){mergeSort(a,0,len(a)-1)}funcmergeSort(a []int, l, r int){if l >= r {return}
mid :=(l + r)/2mergeSort(a, l, mid)mergeSort(a, mid+1, r)merge(a, l, mid, r)}funcmerge(a []int, l, mid, r int){// 创建临时数组
tmp :=make([]int, r-l+1)
i, j, k := l, mid+1,0for i <= mid && j <= r {if a[i]< a[j]{
tmp[k]= a[i]
i++}else{
tmp[k]= a[j]
j++}
k++}// 剩余部分拷贝到临时数组for i <= mid {
tmp[k]= a[i]
i++
k++}for j <= r {
tmp[k]= a[j]
j++
k++}// 拷贝到原数组for i :=0; i <len(tmp); i++{
a[l+i]= tmp[i]}}
选择排序
// 选择排序, 时间复杂度O(n^2), 空间复杂度O(1)funcSelectSort(a []int){for i :=0; i <len(a); i++{
min := i
for j := i +1; j <len(a); j++{if a[j]< a[min]{
min = j
}}
a[i], a[min]= a[min], a[i]}}
// 堆排序, 时间复杂度O(nlogn), 空间复杂度O(1)funcHeapSort(a []int){// 创建大顶堆for i :=len(a)/2; i >=0; i--{heapAdjust(a, i,len(a))}// 堆排序for i :=len(a)-1; i >0; i--{
a[0], a[i]= a[i], a[0]heapAdjust(a,0, i)}}funcheapAdjust(a []int, i, length int){
tmp := a[i]for k :=2*i +1; k < length; k =2*k +1{if k+1< length && a[k]< a[k+1]{
k++}if a[k]> tmp {
a[i]= a[k]
i = k
}else{break}}
a[i]= tmp
}
桶排序
// 桶排序, 时间复杂度O(n), 空间复杂度O(n)funcBucketSort(a []int){if a ==nil{return}// 桶数组
min := a[0]
max := min
for_, i :=range a {if i < min {
min = i
}if i > max {
max = i
}}
total :=(max-min)/len(a)+1
buckets :=make([][]int, total)// 初始化桶数组for i :=0; i <len(a); i++{
idx :=(a[i]- min)/len(a)
buckets[idx]=append(buckets[idx], a[i])}// 每个桶进行排序for i :=0; i <len(buckets); i++{InsertSort(buckets[i])// 使用插入排序}// 拷贝桶数组
k :=0for i :=0; i <len(buckets); i++{for j :=0; j <len(buckets[i]); j++{
a[k]= buckets[i][j]
k++}}}
代码验证
测试代码
package sort
import("math/rand""testing")funcTestSort(t *testing.T){var a []intfor i :=10; i >0; i--{
a =append(a, rand.Intn(65536)%1000)}
assert :=func(a, b []int)bool{iflen(a)!=len(b){returnfalse}for i :=0; i <len(a); i++{if a[i]!= b[i]{returnfalse}}returntrue}
expect :=make([]int,len(a))copy(expect, a)InsertSort(expect)
t.Logf("before sort: %v\n", a)
t.Logf("expect: %v\n", expect)
t.Run("FastSort",func(t *testing.T){
b :=make([]int,len(a))copy(b, a)FastSort(b)if!assert(b, expect){
t.Errorf("FastSort: %v\n", b)}})
t.Run("ShellSort",func(t *testing.T){
b :=make([]int,len(a))copy(b, a)ShellSort(b)if!assert(b, expect){
t.Errorf("ShellSort: %v\n", b)}})
t.Run("MergeSort",func(t *testing.T){
b :=make([]int,len(a))copy(b, a)MergeSort(b)if!assert(b, expect){
t.Errorf("MergeSort: %v\n", b)}})
t.Run("SelectSort",func(t *testing.T){
b :=make([]int,len(a))copy(b, a)SelectSort(b)if!assert(b, expect){
t.Errorf("SelectSort: %v\n", b)}})
t.Run("BubbleSort",func(t *testing.T){
b :=make([]int,len(a))copy(b, a)BubbleSort(b)if!assert(b, expect){
t.Errorf("BubbleSort: %v\n", b)}})
t.Run("HeapSort",func(t *testing.T){
b :=make([]int,len(a))copy(b, a)HeapSort(b)if!assert(b, expect){
t.Errorf("HeapSort: %v\n", b)}})
t.Run("BucketSort",func(t *testing.T){
b :=make([]int,len(a))copy(b, a)BucketSort(b)if!assert(b, expect){
t.Errorf("BucketSort: %v\n", b)}})
t.Run("InsertSort",func(t *testing.T){
b :=make([]int,len(a))copy(b, a)InsertSort(b)if!assert(b, expect){
t.Errorf("InsertSort: %v\n", b)}})}funcBenchmarkSort(b *testing.B){var a []intfor i :=10000; i >0; i--{
a =append(a, rand.Intn(1000000))}
b.Run("InsertSort",func(b *testing.B){
c :=make([]int,len(a))copy(c, a)
b.ResetTimer()for i :=0; i < b.N; i++{InsertSort(c)}})
b.Run("ShellSort",func(b *testing.B){
c :=make([]int,len(a))copy(c, a)
b.ResetTimer()for i :=0; i < b.N; i++{ShellSort(c)}})
b.Run("MergeSort",func(b *testing.B){
c :=make([]int,len(a))copy(c, a)
b.ResetTimer()for i :=0; i < b.N; i++{MergeSort(c)}})
b.Run("SelectSort",func(b *testing.B){
c :=make([]int,len(a))copy(c, a)
b.ResetTimer()for i :=0; i < b.N; i++{SelectSort(c)}})
b.Run("BubbleSort",func(b *testing.B){
c :=make([]int,len(a))copy(c, a)
b.ResetTimer()for i :=0; i < b.N; i++{BubbleSort(c)}})
b.Run("HeapSort",func(b *testing.B){
c :=make([]int,len(a))copy(c, a)
b.ResetTimer()for i :=0; i < b.N; i++{HeapSort(c)}})
b.Run("BucketSort",func(b *testing.B){
c :=make([]int,len(a))copy(c, a)
b.ResetTimer()for i :=0; i < b.N; i++{BucketSort(c)}})
b.Run("FastSort",func(b *testing.B){
c :=make([]int,len(a))copy(c, a)
b.ResetTimer()for i :=0; i < b.N; i++{FastSort(c)}})}