這篇文章給大家分享的是golang使用sort包的方法,相信大部分人都還沒學會這個技能,為了讓大家學會,給大家總結了以下內容,話不多說,一起往下看吧。
sort包中實現了3種基本的排序算法:插入排序.快排和堆排序.和其他語言中一樣,這三種方式都是不公開的,他們只在sort包內部使用。
所以用戶在使用sort包進行排序時無需考慮使用那種排序方式,sort.Interface定義的三個方法:獲取數據集合長度的Len()方法、比較兩個元素大小的Less()方法和交換兩個元素位置的Swap()方法,就可以順利對數據集合進行排序。sort包會根據實際數據自動選擇高效的排序算法。
type Interface interface {
// 返回要排序的數據長度
Len() int
//比較下標為i和j對應的數據大小,可自己控制升序和降序
Less(i, j int) bool
// 交換下標為i,j對應的數據
Swap(i, j int)
}任何實現了 sort.Interface 的類型(一般為集合),均可使用該包中的方法進行排序。這些方法要求集合內列出元素的索引為整數。
這里我直接用源碼來講解實現:
1、源碼中的例子:
type Person struct {
Name string
Age int
}
type ByAge []Person
//實現了sort接口中的三個方法,則可以使用排序方法了
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
func Example() {
people := []Person{
{"Bob", 31},
{"John", 42},
{"Michael", 17},
{"Jenny", 26},
}
fmt.Println(people)
sort.Sort(ByAge(people)) //此處調用了sort包中的Sort()方法,我們看一下這個方法
fmt.Println(people)
// Output:
// [Bob: 31 John: 42 Michael: 17 Jenny: 26]
// [Michael: 17 Jenny: 26 Bob: 31 John: 42]
}2、Sort(data Interface)方法
//sort包只提供了這一個公開的公使用的排序方法,
func Sort(data Interface) {
// Switch to heapsort if depth of 2*ceil(lg(n+1)) is reached.
//如果元素深度達到2*ceil(lg(n+1))則選用堆排序
n := data.Len()
maxDepth := 0
for i := n; i > 0; i >>= 1 {
maxDepth++
}
maxDepth *= 2
quickSort(data, 0, n, 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) //堆排序方法,a=0,b=n
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)
}
}//堆排序
func heapSort(data Interface, a, b int) {
first := a
lo := 0
hi := b - a
// Build heap with greatest element at top.
//構建堆結構,最大的元素的頂部,就是構建大根堆
for i := (hi - 1) / 2; i >= 0; i-- {
siftDown(data, i, hi, first)
}
// Pop elements, largest first, into end of data.
//把first插入到data的end結尾
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) {
//hi為數組的長度
//這里有一種做法是把跟元素給取到存下來,但是為了方法更抽象,省掉了這部,取而代之的是在swap的時候進行相互交換
root := lo //根元素的下標
for {
child := 2*root + 1 //左葉子結點下標
//控制for循環介紹,這種寫法更簡潔,可以查看我寫的堆排序的文章
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
}
}看完這篇文章,你們學會golang使用sort包的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。