Go語言的堆排序實現

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

關於堆排序的演算法,可以參考我去年的文章《堆排序(HEAP SORT)》。那篇文章講的是建立小頂堆進行的排序,這裡說的是建立大頂堆建立的排序,差不多。

在Golang源碼的sort包裡,內建了排序函數。該函數可以對各種類型進行排序,只不過該類型需要實現三個函數,使得該類能夠實現Interface介面。

type Interface interface {// Len is the number of elements in the collection.Len() int// Less reports whether the element with// index i should sort before the element with index j.Less(i, j int) bool// Swap swaps the elements with indexes i and j.Swap(i, j int)}

這三個函數分別是,擷取排序隊列長度、隊列任意兩個元素比較大小和交換任意兩個元素。

func (a BySortIndex) Len() int      { return len(a) }func (a BySortIndex) Swap(i, j int) { a[i], a[j] = a[j], a[i] }func (a BySortIndex) Less(i, j int) bool {return a[i] < a[j]}

如果是整形數組,可以像上面這樣實現。Golang支援多值賦值,所以交換值很簡單。內建的len也使得長度遍曆很簡單。比較大小,可以根據實際情況自己定義。

堆排序的核心就是建立大頂堆和交換值,它是本地排序,不需要新分配空間。Golang的源碼我已經加了注釋,也不難,大家直接閱讀即可。

func heapSort(data Interface, a, b int) {first := alo := 0hi := 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.// 二叉樹結構當中最後一個有子結點的結點for i := hi - 1; i >= 0; i-- {data.Swap(first, first+i)siftDown(data, lo, i, first)}}// 建立樹函數// 父節點root的左孩子2*root + 1func siftDown(data Interface, lo, hi, first int) {root := lofor {child := 2*root + 1if child >= hi { // child 超出了數組長度,也就是該結點無孩子結點,返回break}if child+1 < hi && data.Less(first+child, first+child+1) { // 右孩子結點存在child++}// 以上是為了在孩子結點當中找到較大的結點,用child表示if !data.Less(first+root, first+child) {return}// 如果根結點小於較大的孩子結點,則進行交換;該孩子結點的堆結構可能會被影響,繼續去處理孩子結點data.Swap(first+root, first+child)root = child}}

本文所涉及到的完整源碼請參考。

參考文獻
  • 【1】堆排序(HEAP SORT) - Cyeam

原文連結:Go語言的堆排序實現,轉載請註明來源!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.