背景
参考:https://www.godoc.org/sort
最近使用到了golang中的sort包,于是好奇包内使用了什么排序算法便进去仔细阅读了一下
不得不说官方包内对排序的优化的确非常精妙。
这里对里面用到的一些算法和逻辑进行一些简单的介绍和备忘
源码阅读
sort包的使用
import(
"sort"
"fmt"
)
func main(){
data:=[]int{6,4,2,1,4,3}
sort.Ints(data)
fmt.Println(data) //输出[1 2 3 4 4 6]
}
上一段代码中使用了sort包对int的排序
我们可以看他的源码
虽然支持各种类型的排序 它内部是使用了Sort这个函数
// Ints sorts a slice of ints in increasing order.
func Ints(a []int) { Sort(IntSlice(a)) }
// Float64s sorts a slice of float64s in increasing order
// (not-a-number values are treated as less than other values).
func Float64s(a []float64) { Sort(Float64Slice(a)) }
// Strings sorts a slice of strings in increasing order.
func Strings(a []string) { Sort(StringSlice(a)) }
// Sort sorts data.
// It makes one call to data.Len to determine n, and O(n*log(n)) calls to
// data.Less and data.Swap. The sort is not guaranteed to be stable.
func Sort(data Interface) {
n := data.Len()
quickSort(data, 0, n, maxDepth(n))
}
// maxDepth returns a threshold at which quicksort should switch
// to heapsort. It returns 2*ceil(lg(n+1)).
func maxDepth(n int) int {
var depth int
for i := n; i > 0; i >>= 1 {
depth++
}
return depth * 2
}
Sort这个函数中取了数据的长度n,并使用了quicksort(快速排序)来对数据进行排序
这个maxDepth是快速排序次数的一个阈值,超过这个阈值则将进行heapsort(堆排序)
maxDepth=2*log(n+1)
备注中说明排序算法不是稳定的,对于相同的元素,不能保证排序后的顺序和排序是前一样的
接下来我们一次来看一下使用到的各种排序算法
首先 是第一个出现的快速排序
1. 快速排序
/**
data:需要排序处理的数据
a:数据起始位置
b:数据结束位置
maxDepth:剩余快排阈值
**/
func quickSort(data Interface, a, b, maxDepth int) {
for b-a > 12 { // Use ShellSort for slices <= 12 elements
if maxDepth == 0 {
heapSort(data, a, b)
return
}
maxDepth--
mlo, mhi := doPivot(data, a, b)
// Avoiding recursion on the larger subproblem guarantees
// a stack depth of at most lg(b-a).
if mlo-a < b-mhi {
quickSort(data, a, mlo, maxDepth)
a = mhi // i.e., quickSort(data, mhi, b)
} else {
quickSort(data, mhi, b, maxDepth)
b = mlo // i.e., quickSort(data, a, mlo)
}
}
if b-a > 1 { //使用希尔排序
// Do ShellSort pass with gap 6
// It could be written in this simplified form cause b-a <= 12
for i := a + 6; i < b; i++ {
if data.Less(i, i-6) {
data.Swap(i, i-6)
}
}
insertionSort(data, a, b)
}
}
可以看到 这里根据多种不同情况对是用什么算法进行了选择
- 当长度小于12时 对数据进行希尔排序(后续说明)
- 当长度大于12时
2.1 若maxDepth为0 则进行堆排序(后续说明)
2.2 若maxDepth大于0 则进行快速排序
我们先来看快速排序
maxDepth--
mlo, mhi := doPivot(data, a, b)
// Avoiding recursion on the larger subproblem guarantees
// a stack depth of at most lg(b-a).
if mlo-a < b-mhi {
quickSort(data, a, mlo, maxDepth)
a = mhi // i.e., quickSort(data, mhi, b)
} else {
quickSort(data, mhi, b, maxDepth)
b = mlo // i.e., quickSort(data, a, mlo)
}
首先进行一次快排递归需要对现有阈值maxDepth减1
其次使用doPivot函数找到快排中关键的分界值 当然最好的是找到中位数
func doPivot(data Interface, lo, hi int) (midlo, midhi int) {
m := int(uint(lo+hi) >> 1) // Written like this to avoid integer overflow.
if hi-lo > 40 {
// Tukey's ``Ninther,'' median of three medians of three.
s := (hi - lo) / 8
medianOfThree(data, lo, lo+s, lo+2*s)
medianOfThree(data, m, m-s, m+s)
medianOfThree(data, hi-1, hi-1-s, hi-1-2*s)
}
medianOfThree(data, lo, m, hi-1)
// Invariants are:
// data[lo] = pivot (set up by ChoosePivot)
// data[lo < i < a] < pivot
// data[a <= i < b] <= pivot
// data[b <= i < c] unexamined
// data[c <= i < hi-1] > pivot
// data[hi-1] >= pivot
pivot := lo
a, c := lo+1, hi-1
for ; a < c && data.Less(a, pivot); a++ {
}
b := a
for {
for ; b < c && !data.Less(pivot, b); b++ { // data[b] <= pivot
}
for ; b < c && data.Less(pivot, c-1); c-- { // data[c-1] > pivot
}
if b >= c {
break
}
// data[b] > pivot; data[c-1] <= pivot
data.Swap(b, c-1)
b++
c--
}
// If hi-c<3 then there are duplicates (by property of median of nine).
// Let's be a bit more conservative, and set border to 5.
protect := hi-c < 5
if !protect && hi-c < (hi-lo)/4 {
// Lets test some points for equality to pivot
dups := 0
if !data.Less(pivot, hi-1) { // data[hi-1] = pivot
data.Swap(c, hi-1)
c++
dups++
}
if !data.Less(b-1, pivot) { // data[b-1] = pivot
b--
dups++
}
// m-lo = (hi-lo)/2 > 6
// b-lo > (hi-lo)*3/4-1 > 8
// ==> m < b ==> data[m] <= pivot
if !data.Less(m, pivot) { // data[m] = pivot
data.Swap(m, b-1)
b--
dups++
}
// if at least 2 points are equal to pivot, assume skewed distribution
protect = dups > 1
}
if protect {
// Protect against a lot of duplicates
// Add invariant:
// data[a <= i < b] unexamined
// data[b <= i < c] = pivot
for {
for ; a < b && !data.Less(b-1, pivot); b-- { // data[b] == pivot
}
for ; a < b && data.Less(a, pivot); a++ { // data[a] < pivot
}
if a >= b {
break
}
// data[a] == pivot; data[b-1] < pivot
data.Swap(a, b-1)
a++
b--
}
}
// Swap pivot into middle
data.Swap(pivot, b-1)
return b - 1, c
}
doPivot有点复杂 我们一点点看
m := int(uint(lo+hi) >> 1) // m取中间位置
if hi-lo > 40 {
s := (hi - lo) / 8
medianOfThree(data, lo, lo+s, lo+2*s)
medianOfThree(data, m, m-s, m+s)
medianOfThree(data, hi-1, hi-1-s, hi-1-2*s)
}
medianOfThree(data, lo, m, hi-1)
首先m取当前的中间位置
函数medianOfThree即取三个点的中间值pivot
m0,m1,m2三个位置的中间值会放到m1处
// medianOfThree moves the median of the three values data[m0], data[m1], data[m2] into data[m1].
func medianOfThree(data Interface, m1, m0, m2 int) {
// sort 3 elements
if data.Less(m1, m0) {
data.Swap(m1, m0)
}
// data[m0] <= data[m1]
if data.Less(m2, m1) {
data.Swap(m2, m1)
// data[m0] <= data[m2] && data[m1] < data[m2]
if data.Less(m1, m0) {
data.Swap(m1, m0)
}
}
// now data[m0] <= data[m1] <= data[m2]
}
如果当前长度大于40 则使用Tukey’s Ninther - John Tukey’s median of median
想象你有九个点y1,y2,y3,y4,y5,y6,y7,y8,y9,我们将yA设置为前三个样本的中间节点,yB设置为中间三个样本的中间节点,yC设置为后三个样本的中间节点,所谓的“第九层”(ninther)数据集就是yA,yB,yC的中间节点。参考:https://blog.csdn.net/mianshui1105/article/details/52691711
这个方法并非是为了取到精确的中位数 而只是取近似
进行完了上面的步骤 我们已经将取出来的分界值放在了lo
// Invariants are:
// data[lo] = pivot (set up by ChoosePivot)
// data[lo < i < a] < pivot
// data[a <= i < b] <= pivot
// data[b <= i < c] unexamined
// data[c <= i < hi-1] > pivot
// data[hi-1] >= pivot
pivot := lo
a, c := lo+1, hi-1
for ; a < c && data.Less(a, pivot); a++ {
}
b := a
for {
for ; b < c && !data.Less(pivot, b); b++ { // data[b] <= pivot
}
for ; b < c && data.Less(pivot, c-1); c-- { // data[c-1] > pivot
}
if b >= c {
break
}
// data[b] > pivot; data[c-1] <= pivot
data.Swap(b, c-1)
b++
c--
}
进行完上述步骤 我们对数据进行了初步的划分
data[lo] = pivot
data[lo < i < a] < pivot
data[a <= i < b] <= pivot
data[c <= i < hi-1] > pivot
data[b <= i < c] 未筛选
那么接下来是将所有等于pivot的移到[b,c-1]区间来
// If hi-c<3 then there are duplicates (by property of median of nine).
// Let's be a bit more conservative, and set border to 5.
//如果大于pivot的个数3 那么根据median of nine必定有pivot重复项 这里增加到了5
protect := hi-c < 5
if !protect && hi-c < (hi-lo)/4 {
// Lets test some points for equality to pivot
// 与pivot相等的个数
dups := 0
// 根据之前medianOfThree pivot<=data[hi-1]
// 如果此时pivot>=data[hi-1]
// 那么可以得到data[hi-1] = pivot
if !data.Less(pivot, hi-1) {
data.Swap(c, hi-1)
c++
dups++
}
// 根据之前medianOfThree pivot>=data[b-1]
// 如果此时pivot<=data[b-1]
// 那么可以得到data[b-1] = pivot
if !data.Less(b-1, pivot) {
b--
dups++
}
// m-lo = (hi-lo)/2 > 6
// b-lo > (hi-lo)*3/4-1 > 8
// ==> m < b ==> data[m] <= pivot
// 如果此时data[m]>=pivot
if !data.Less(m, pivot) { // data[m] = pivot
data.Swap(m, b-1)
b--
dups++
}
// if at least 2 points are equal to pivot, assume skewed distribution
//大于1则说明存在和pivot相等的数
protect = dups > 1
}
if protect {
// 存在pivot重复的话要移动这些数据,也就是进一步细分[a,b)这个区间
// Protect against a lot of duplicates
// Add invariant:
// data[a <= i < b] unexamined
// data[b <= i < c] = pivot
for {
for ; a < b && !data.Less(b-1, pivot); b-- { // data[b] == pivot
}
for ; a < b && data.Less(a, pivot); a++ { // data[a] < pivot
}
if a >= b {
break
}
// data[a] == pivot; data[b-1] < pivot
data.Swap(a, b-1)
a++
b--
}
}
// Swap pivot into middle
data.Swap(pivot, b-1)
return b - 1, c
根据上述的方法得到了b-1,c这两个分界点
mlo, mhi := doPivot(data, a, b)
// Avoiding recursion on the larger subproblem guarantees
// a stack depth of at most lg(b-a).
if mlo-a < b-mhi {
quickSort(data, a, mlo, maxDepth)
a = mhi // i.e., quickSort(data, mhi, b)
} else {
quickSort(data, mhi, b, maxDepth)
b = mlo // i.e., quickSort(data, a, mlo)
}
根据分界点进行快速排序 这里先对长度小的进行了排序
这里就讲完了快速排序
接下来看长度小于12时进行的希尔排序
2. 希尔排序
if b-a > 1 { //使用希尔排序
// Do ShellSort pass with gap 6
// It could be written in this simplified form cause b-a <= 12
for i := a + 6; i < b; i++ {
if data.Less(i, i-6) {
data.Swap(i, i-6)
}
}
insertionSort(data, a, b) //插入排序
}
希尔排序是对插入排序进行了改进,通过一定的间隔gap将元素划分成几个区域来先进行排序,然后逐步缩小间隔进行排序,最后采用插入排序,此时基本已经排好了,所以插入排序的效率就很高。
这里希尔排序使用的gap值是6,也就是间隔6位的为一组,先进行排序,不同于平时的希尔排序将gap值减半,而是直接使用了插入排序。
接下来看插入排序
3. 插入排序
// Insertion sort
func insertionSort(data Interface, a, b int) {
for i := a + 1; i < b; i++ {
for j := i; j > a && data.Less(j, j-1); j-- {
data.Swap(j, j-1)
}
}
}
这个代码十分简洁明了
将未排序的元素i和已排序元素[a,i)从后往前依次比较,不停前移,直到比前项要大
此时为相应的位置
最后我们看一下堆排序
4. 堆排序
func heapSort(data Interface, a, b int) {
first := a
lo := 0
hi := b - a
// Build heap with greatest element at top.
// 建立大顶堆 将所有非叶节点进行下沉操作
// hi为所有的个数 在二叉树中最多能有(hi-1)/2个非叶节点
for i := (hi - 1) / 2; i >= 0; i-- {
siftDown(data, i, hi, first)
}
// Pop elements, largest first, into end of data.
// 将根节点与最后一个叶节点交换 重新整理大顶堆
for i := hi - 1; i >= 0; i-- {
data.Swap(first, first+i)
siftDown(data, lo, i, first)
}
}
// siftDown implements the heap property on data[lo, hi).
// first is an offset into the array where the root of the heap lies.
func siftDown(data Interface, lo, hi, first int) {
//父节点为lo
root := lo
for {
//root作为父节点 第一个子节点的位置2*root+1
child := 2*root + 1
if child >= hi {
break
}
//在两个子节点中选择较大的子节点
if child+1 < hi && data.Less(first+child, first+child+1) {
child++
}
//如果父节点已经比两个子节点都大了 则直接返回
if !data.Less(first+root, first+child) {
return
}
//将子节点中的较大值与父节点的值交换
data.Swap(first+root, first+child)
//继续检查换完的子节点
root = child
}
}
参考:https://www.cnblogs.com/xiugeng/p/9645972.html#_label1_0
堆排序就是建立一个大顶堆,将根节点与最后一个叶子节点交换
然后用剩下的元素继续整理成为大顶堆,不断重复上述步骤
直到将最后一个元素换到数据的开始,排序完成
总结
到这里就将golang的sort包中用到的几种排序方法
包括快速排序,希尔排序,插入排序,堆排序进行了简单的分析
可以看到 实际应用中并非简简单单地使用某一种算法
而是不断地优化,不停变换排序方式,根据不同情况,不同数据量的特点,尽可能地使内存和时间都能做到最少
受益匪浅~