GoLang之标准库sort包

本文基于Windos系统上Go SDK v1.8进行讲解

1.sort包介绍

sort 包主要用来实现排序相关的操作,它实现了四种基本的排序算法:插入排序(insertionSort)、归并排序(symMerge)、堆排序(heapSort)和快速排序(quickSort);sort 包会依据实际数据自动选择最优的排序算法会对原值进行改变
注:需要写import “sort”

image-20220111105245176

2.sort.Interface接口

image-20220209211907554

image-20220209213239404

3.sort.IntSlice切片类型

3.1IntSlice切片类型

IntSlice也是Interface接口,原因是它实现了那三个方法

image-20220209212007770

3.2Len、Less、Swap、Sort方法

image-20220209212128002

image-20220111114348510

3.3Search方法

Search等价于调用SearchInts(p, x)

func (p IntSlice) Search(x int) int

4.sort.Float64Slice切片类型

4.1Float64Slice类型

Float64Slice也是Interface接口,原因是它实现了那三个方法

// Float64Slice implements Interface for a []float64, sorting in increasing order,
// with not-a-number (NaN) values ordered before other values.
type Float64Slice []float64

4.2Len、Less、Swap、Sort方法

func (x Float64Slice) Len() int { return len(x) }

// Less reports whether x[i] should be ordered before x[j], as required by the sort Interface.
// Note that floating-point comparison by itself is not a transitive relation: it does not
// report a consistent ordering for not-a-number (NaN) values.
// This implementation of Less places NaN values before any others, by using:
//
//	x[i] < x[j] || (math.IsNaN(x[i]) && !math.IsNaN(x[j]))
//
func (x Float64Slice) Less(i, j int) bool { return x[i] < x[j] || (isNaN(x[i]) && !isNaN(x[j])) }
func (x Float64Slice) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }


// Sort is a convenience method: x.Sort() calls Sort(x).
func (x Float64Slice) Sort() { Sort(x) }

4.3Search方法

Search等价于调用SearchFloat64s(p, x)

func (p Float64Slice) Search(x float64) int

5.sort.StringSlice切片类型

5.1StringSlice切片类型

// StringSlice attaches the methods of Interface to []string, sorting in increasing order.
type StringSlice []string

5.2Len、Less、Swap、Sort方法

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

// Sort is a convenience method: x.Sort() calls Sort(x).
func (x StringSlice) Sort() { Sort(x) }

5.3Search

Search等价于调用SearchStrings(p, x)

func (p StringSlice) Search(x string) int

image-20220111114306212

6.sort.Sort函数

必须传入Interface接口,及实现了三个方法

image-20220209211824774

7.sort.Ints函数

该方法没有返回值
里面只可以传入int 的切片

image-20220209213539682

image-20220111113522319

8.sort.Float64s函数

该方法没有返回值
里面只可以传入float64 的切片

image-20220209213607366

image-20220111113713743

9.sort.Strings函数

该方法没有返回值
里面只可以传入string 的切片

image-20220209213623907

image-20220111113754594

10.sort.IsSorted函数

func IsSorted(data Interface) bool {… }
只用来判断是否有序,并不执行排序操作

11.sort.Reverse函数

image-20220111112744280

image-20220111112629578

12.sort.Slice函数

image-20220127135655681

image-20220127135910777

image-20220127140255753

image-20220127141105646

image-20220303215625578

13.IntsAreSorted函数

IntsAreSorted检查a是否已排序为递增顺序。

func IntsAreSorted(a []int) bool

14.SearchInts

SearchInts在递增顺序的a中搜索x,返回x的索引。如果查找不到,返回值是x应该插入a的位置(以保证a的递增顺序),返回值可以是len(a)。

func SearchInts(a []int, x int) int

15.Float64sAreSorted函数

Float64sAreSorted检查a是否已排序为递增顺序。

func Float64sAreSorted(a []float64) bool

16.SearchFloat64s函数

SearchFloat64s在递增顺序的a中搜索x,返回x的索引。如果查找不到,返回值是x应该插入a的位置(以保证a的递增顺序),返回值可以是len(a)。

func SearchFloat64s(a []float64, x float64) int

17.StringsAreSorted

StringsAreSorted检查a是否已排序为递增顺序。

func StringsAreSorted(a []string) bool

18.SearchStrings

SearchStrings在递增顺序的a中搜索x,返回x的索引。如果查找不到,返回值是x应该插入a的位置(以保证a的递增顺序),返回值可以是len(a)。

func SearchStrings(a []string, x string) int

19.Stable函数

Stable排序data,并保证排序的稳定性,相等元素的相对次序不变。
它调用1次data.Len,O(nlog(n))次data.Less和O(nlog(n)*log(n))次data.Swap。

func Stable(data Interface)