以下由golang教學專欄跟大家介紹golang之排序使用,希望對需要的朋友有幫助!
#golang標準函式庫實作了許多常用的排序方法,例如對整數序列排序:sort.Ints()
,
那麼如果對自訂的資料結構排序怎麼做呢?
例如對一個使用者列表,按他們的積分排序:
先定義資料結構,為了能清楚說明問題,只給兩個字段。
type User struct { Name string Score int}type Users []User
golang中想要自訂排序,自己的結構要實作三個方法:
// 摘自: $GOROOT/src/sort/sort.gotype 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)}
這個設計太妙了有沒有,想想我們學過的排序,都要序列長度,比大小,交換元素。
那對上述的Users,也就是使用者清單如何使用golang的排序呢?
先按它說的,實作這三個方法:
func (us Users) Len() int { return len(us)}func (us Users) Less(i, j int) bool { return us[i].Score < us[j].Score}func (us Users) Swap(i, j int) { us[i], us[j] = us[j], us[i]}
然後就能排序了:
func main() { var us Users const N = 6 for i := 0; i < N; i++ { us = append(us, User{ Name: "user" + strconv.Itoa(i), Score: rand.Intn(N * N), }) } fmt.Printf("%v\n", us) sort.Sort(us) fmt.Printf("%v\n", us)}
可能的輸出為:
[{user0 5} {user1 15} {user2 11} {user3 11} {user4 13} {user5 6}]
[{user0 5} {user5 6} {user2 11} {user3 11} {user4 13} {user1 15}]
可以看到,分數從小到大排列了。
不過一般我們積分這種東西都是從大到小排序的,只要將sort.Sort(us)
改成sort.Sort(sort .Reverse(us))
就行。
確實很方便。
當然,如果出於特殊需要,系統提供的排序不能滿足我們的需要,
還是可以自己實現排序的, 例如針對上述,自己來排序(從小到大):
func myqsort(us []User, lo, hi int) { if lo < hi { pivot := partition(us, lo, hi) myqsort(us, lo, pivot-1) myqsort(us, pivot+1, hi) }}func partition(us []User, lo, hi int) int { tmp := us[lo] for lo < hi { for lo < hi && us[hi].Score >= tmp.Score { hi-- } us[lo] = us[hi] for lo < hi && us[lo].Score <= tmp.Score { lo++ } us[hi] = us[lo] } us[lo] = tmp return hi}
一個簡單的快速排序,呼叫時只需要myqsort(us)
就可以了。
總結:
- 自訂序列要實作Less, Swap, Len三個方法 才行
歡迎補充修正!
#以上是關於golang之排序使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Golang更適合高並發任務,而Python在靈活性上更有優勢。 1.Golang通過goroutine和channel高效處理並發。 2.Python依賴threading和asyncio,受GIL影響,但提供多種並發方式。選擇應基於具體需求。

Golang和C 在性能上的差異主要體現在內存管理、編譯優化和運行時效率等方面。 1)Golang的垃圾回收機制方便但可能影響性能,2)C 的手動內存管理和編譯器優化在遞歸計算中表現更為高效。

selectgolangforhighpperformanceandcorrency,ifealforBackendServicesSandNetwork程序; selectpypypythonforrapiddevelopment,dataScience和machinelearningDuetoitsverserverserverserversator versator anderticality andextility andextentensivelibraries。

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。Golang以其并发模型和高效性能著称,Python则以简洁语法和丰富库生态系统著称。

Golang和Python分別在哪些方面更易用和學習曲線更平緩? Golang更適合高並發和高性能需求,學習曲線對有C語言背景的開發者較平緩。 Python更適合數據科學和快速原型設計,學習曲線對初學者非常平緩。

Golang和C 在性能競賽中的表現各有優勢:1)Golang適合高並發和快速開發,2)C 提供更高性能和細粒度控制。選擇應基於項目需求和團隊技術棧。

Golang適合快速開發和並發編程,而C 更適合需要極致性能和底層控制的項目。 1)Golang的並發模型通過goroutine和channel簡化並發編程。 2)C 的模板編程提供泛型代碼和性能優化。 3)Golang的垃圾回收方便但可能影響性能,C 的內存管理複雜但控制精細。

goimpactsdevelopmentpositationality throughspeed,效率和模擬性。 1)速度:gocompilesquicklyandrunseff,IdealforlargeProjects.2)效率:效率:ITScomprehenSevestAndardArdardArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增強的Depleflovelmentimency.3)簡單性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)