An open API service indexing awesome lists of open source software.

https://github.com/golang-infrastructure/go-heap

Go的堆排序实现,提供线程安全和非安全的两个实现,API相比较于Go内置的堆更易使用,使用者不需要了解底层的堆知识即可使用,并且支持泛型不需要类型强转
https://github.com/golang-infrastructure/go-heap

go-library heapsort-algorithm sorting-algorithms

Last synced: 3 months ago
JSON representation

Go的堆排序实现,提供线程安全和非安全的两个实现,API相比较于Go内置的堆更易使用,使用者不需要了解底层的堆知识即可使用,并且支持泛型不需要类型强转

Awesome Lists containing this project

README

        

# Go 堆排序

# Install

```bash
go get github.com/golang-infrastructure/go-heap
```

# Example
```go
package main

import (
"fmt"
"github.com/golang-infrastructure/go-heap"
"math/rand"
)

func main() {
options := &heap.Options[int]{
Comparator: heap.IntComparator(),
// 支持N叉堆,默认是2叉堆
Ary: 4,
}

// 非线程安全的堆,仅限于单个goroutine里使用
//heap :=heap.New(heap.IntComparator())
heap := heap.NewWithOptions(options)

// 创建线程安全的堆
//heap :=heap.NewSync(heap.IntComparator())
//heap := heap.NewSyncWithOptions(options)
for i := 0; i < 10; i++ {
n := rand.Int() % 100
heap.Push(n)
}
heapNumSlice := heap.PopToSlice()
fmt.Println(heapNumSlice) // [10 16 20 21 37 48 49 51 51 58]
}
```