十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
這篇文章給大家分享的是golang使用sort包的方法,相信大部分人都還沒(méi)學(xué)會(huì)這個(gè)技能,為了讓大家學(xué)會(huì),給大家總結(jié)了以下內(nèi)容,話不多說(shuō),一起往下看吧。
sort包中實(shí)現(xiàn)了3種基本的排序算法:插入排序.快排和堆排序.和其他語(yǔ)言中一樣,這三種方式都是不公開(kāi)的,他們只在sort包內(nèi)部使用。
所以用戶在使用sort包進(jìn)行排序時(shí)無(wú)需考慮使用那種排序方式,sort.Interface定義的三個(gè)方法:獲取數(shù)據(jù)集合長(zhǎng)度的Len()方法、比較兩個(gè)元素大小的Less()方法和交換兩個(gè)元素位置的Swap()方法,就可以順利對(duì)數(shù)據(jù)集合進(jìn)行排序。sort包會(huì)根據(jù)實(shí)際數(shù)據(jù)自動(dòng)選擇高效的排序算法。
type Interface interface { // 返回要排序的數(shù)據(jù)長(zhǎng)度 Len() int //比較下標(biāo)為i和j對(duì)應(yīng)的數(shù)據(jù)大小,可自己控制升序和降序 Less(i, j int) bool // 交換下標(biāo)為i,j對(duì)應(yīng)的數(shù)據(jù) Swap(i, j int) }
任何實(shí)現(xiàn)了 sort.Interface 的類型(一般為集合),均可使用該包中的方法進(jìn)行排序。這些方法要求集合內(nèi)列出元素的索引為整數(shù)。
這里我直接用源碼來(lái)講解實(shí)現(xiàn):
1、源碼中的例子:
type Person struct { Name string Age int } type ByAge []Person //實(shí)現(xiàn)了sort接口中的三個(gè)方法,則可以使用排序方法了 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)) //此處調(diào)用了sort包中的Sort()方法,我們看一下這個(gè)方法 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包只提供了這一個(gè)公開(kāi)的公使用的排序方法, func Sort(data Interface) { // Switch to heapsort if depth of 2*ceil(lg(n+1)) is reached. //如果元素深度達(dá)到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) }
//快速排序 //它這里會(huì)自動(dòng)選擇是用堆排序還是插入排序還是快速排序,快速排序就是 func quickSort(data Interface, a, b, maxDepth int) { //如果切片元素少于十二個(gè)則使用希爾插入法 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. //構(gòu)建堆結(jié)構(gòu),大的元素的頂部,就是構(gòu)建大根堆 for i := (hi - 1) / 2; i >= 0; i-- { siftDown(data, i, hi, first) } // Pop elements, largest first, into end of data. //把first插入到data的end結(jié)尾 for i := hi - 1; i >= 0; i-- { data.Swap(first, first+i) //數(shù)據(jù)交換 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為數(shù)組的長(zhǎng)度 //這里有一種做法是把跟元素給取到存下來(lái),但是為了方法更抽象,省掉了這部,取而代之的是在swap的時(shí)候進(jìn)行相互交換 root := lo //根元素的下標(biāo) for { child := 2*root + 1 //左葉子結(jié)點(diǎn)下標(biāo) //控制for循環(huán)介紹,這種寫法更簡(jiǎn)潔,可以查看我寫的堆排序的文章 if child >= hi { break } //防止數(shù)組下標(biāo)越界,判斷左孩子和右孩子那個(gè)大 if child+1 < hi && data.Less(first+child, first+child+1) { child++ } //判斷大的孩子和根元素之間的關(guān)系 if !data.Less(first+root, first+child) { return } //如果上面都 滿足,則進(jìn)行數(shù)據(jù)交換 data.Swap(first+root, first+child) root = child } }
看完這篇文章,你們學(xué)會(huì)golang使用sort包的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,感謝各位的閱讀。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。