主頁(yè) > 知識(shí)庫(kù) > go語(yǔ)言中sort包的實(shí)現(xiàn)方法與應(yīng)用詳解

go語(yǔ)言中sort包的實(shí)現(xiàn)方法與應(yīng)用詳解

熱門(mén)標(biāo)簽:百度競(jìng)價(jià)排名 Linux服務(wù)器 服務(wù)外包 AI電銷(xiāo) 地方門(mén)戶(hù)網(wǎng)站 網(wǎng)站排名優(yōu)化 呼叫中心市場(chǎng)需求 鐵路電話(huà)系統(tǒng)

前言

Go語(yǔ)言的 sort 包實(shí)現(xiàn)了內(nèi)置和用戶(hù)定義類(lèi)型的排序,sort包中實(shí)現(xiàn)了3種基本的排序算法:插入排序.快排和堆排序.和其他語(yǔ)言中一樣,這三種方式都是不公開(kāi)的,他們只在sort包內(nèi)部使用.所以用戶(hù)在使用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)選擇高效的排序算法。

之前跟大家分享了Go語(yǔ)言使用sort包對(duì)任意類(lèi)型元素的集合進(jìn)行排序的方法,感興趣的朋友們可以參考這篇文章:https://www.jb51.net/article/60893.htm

下面來(lái)看看sort包的簡(jiǎn)單示例:

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 的類(lèi)型(一般為集合),均可使用該包中的方法進(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)介紹,這種寫(xiě)法更簡(jiǎn)潔,可以查看我寫(xiě)的堆排序的文章
  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
  }
  //如果上面都 滿(mǎn)足,則進(jìn)行數(shù)據(jù)交換
  data.Swap(first+root, first+child)
  root = child
 }
}

這個(gè)包中還有很多方法,這個(gè)包實(shí)現(xiàn)了很多方法,比如排序反轉(zhuǎn),二分搜索。排序通過(guò) quickSort()這個(gè)方法來(lái)控制該調(diào)用快排還是堆排。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

您可能感興趣的文章:
  • Go語(yǔ)言使用sort包對(duì)任意類(lèi)型元素的集合進(jìn)行排序的方法
  • golang使用sort接口實(shí)現(xiàn)排序示例
  • go語(yǔ)言中排序sort的使用方法示例

標(biāo)簽:黃山 銅川 湘潭 蘭州 衡水 仙桃 崇左 湖南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《go語(yǔ)言中sort包的實(shí)現(xiàn)方法與應(yīng)用詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266