MCPcopy
hub / github.com/greyireland/algorithm-pattern / sink

Function sink

src/sort/heap_sort.go:19–44  ·  view source on GitHub ↗
(a []int, i int, length int)

Source from the content-addressed store, hash-verified

17 return a
18}
19func sink(a []int, i int, length int) {
20 for {
21 // 左节点索引(从0开始,所以左节点为i*2+1)
22 l := i*2 + 1
23 // 有节点索引
24 r := i*2 + 2
25 // idx保存根、左、右三者之间较大值的索引
26 idx := i
27 // 存在左节点,左节点值较大,则取左节点
28 if l < length && a[l] > a[idx] {
29 idx = l
30 }
31 // 存在有节点,且值较大,取右节点
32 if r < length && a[r] > a[idx] {
33 idx = r
34 }
35 // 如果根节点较大,则不用下沉
36 if idx == i {
37 break
38 }
39 // 如果根节点较小,则交换值,并继续下沉
40 swap(a, i, idx)
41 // 继续下沉idx节点
42 i = idx
43 }
44}
45func swap(a []int, i, j int) {
46 a[i], a[j] = a[j], a[i]
47}

Callers 1

HeapSortFunction · 0.85

Calls 1

swapFunction · 0.85

Tested by

no test coverage detected