-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortMap.go
More file actions
41 lines (33 loc) · 874 Bytes
/
sortMap.go
File metadata and controls
41 lines (33 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import "fmt"
func sortMap(_map map[string]int) []LanguageLineCount {
pairs := make([]LanguageLineCount, 0, len(_map))
for k, v := range _map {
pairs = append(pairs, LanguageLineCount{Extension: k, Lines: v})
}
fmt.Println()
quickSort(pairs, 0, len(pairs)-1)
return pairs
}
func quickSort(data []LanguageLineCount, lowIndex int, highIndex int) {
if lowIndex < highIndex {
p := partition(data, lowIndex, highIndex)
quickSort(data, lowIndex, p-1)
quickSort(data, p+1, highIndex)
}
}
func partition(data []LanguageLineCount, lowIndex int, highIndex int) int {
pivot := data[highIndex].Lines
i := lowIndex
for j := lowIndex; j <= highIndex; j++ {
if data[j].Lines > pivot {
swap(data, i, j)
i++
}
}
swap(data, i, highIndex)
return i
}
func swap(data []LanguageLineCount, i int, j int) {
data[i], data[j] = data[j], data[i]
}