util

package
v0.0.0-...-d7c879d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultComparator

func DefaultComparator(thisValue interface{}, compareValue interface{}) int

func HeapSort

func HeapSort(slice []interface{}, reverse bool, compareFunc common.CompareFunc)

O(n*log(n)) 堆排序,先将数组通过heapify的方式整理成堆,然后不断的取出堆顶部的元素,和数组尾部未排序的第一个元素进行交换 因为不需要额外的空间,所以对于随机数组比归并排序要快.

func InsertSort

func InsertSort(slice []interface{}, reverse bool, comparator common.CompareFunc)

O(log(n^2)) 插入排序: 从index=1开始遍历数组,不断将索引位置元素和索引之前的元素进行比较,找到合适的插入位置 如果数据是有序的,那么插入排序效率就是O(n)

func MergeSort

func MergeSort(slice []interface{}, reverse bool, comparator common.CompareFunc)

O(n*log(n)) 归并排序

func MergeSortBU

func MergeSortBU(slice []interface{}, reverse bool, comparator common.CompareFunc)

O(n*log(n)) 自底向上的归并排序

func QuickSort

func QuickSort(slice []interface{}, reverse bool, comparator common.CompareFunc)

O(n*log(n)) 快速排序

func SelectionSort

func SelectionSort(slice []interface{}, reverse bool, comparator common.CompareFunc)

O(log(n^2)) 选择排序: 遍历数组,不断寻找索引以及索引以后的元素中最小元素和当前索引位置元素进行交换. comparator 如果集合中的元素实现了common.CompareAble接口,或者是int float64 string 类型,comparator可以为空

否则必须传入非nil值

reverse 是否结果取反

func Sort

func Sort(sortAble common.SortAble, reverse bool, comparator common.CompareFunc)

默认的排序方法

func SortWithFunc

func SortWithFunc(sortAble common.SortAble, reverse bool, comparator common.CompareFunc, sortFunc SortFunc)

可以选择排序方法的排序方法

Types

type SortFunc

type SortFunc func(slice []interface{}, reverse bool, comparator common.CompareFunc)

常用的排序函数

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL