sorts

package
v0.0.0-...-36d00e2 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BubbleSort

func BubbleSort(arr []int)

冒泡排序-将最大的数依次排到数组后面去, 时间复杂度O(N^2) 过程: 在 arr[0 ~ N-1] 范围上: arr[0] 和 arr[1], 谁大谁来到 1 位置; arr[1] 和 arr[2], 谁大谁来到 2 位置上 ... arr[N-2] 和 arr[N-1] 谁大谁来到 N-1 位置上 在 arr[1 ~ N-2] 范围上, 重复上述过程, 但最后一步是 arr[N-3] 和 arr[N-2], 谁大谁来到 N-2 位置上 ... 最后在 arr[0-1] 范围上, 重复上述过程, 但最后一步是 arr[0] 和 arr[1], 谁大谁来到 1 位置上

func InsertionSort

func InsertionSort(arr []int)

插入排序-将最小的数依次排到数组前面去, 时间复杂度和数据初始状态有关, 如果数组数据已经有序, 则是 O(N), 最差的情况是 O(N^2), 一般选择最差的情况来估计 想让 arr[0-0] 上有序, 这个范围只有一个数, 当然是有序的 想让 arr[0-1] 上有序, 所以从 arr[1] 开始往前看, 如果 arr[1] < arr[0], 就交换, 否则什么也不做 ... 想让 arr[0-i] 上有序, 所以从 arr[i] 开始往前看, 如果 arr[i] < arr[i-1], 就交换, 这样 arr[i] 这个数就在不停地 向左移动, 一直移动到左边的数字不再比自己大, 就停止移动

func SelectSort

func SelectSort(arr []int)

选择排序-将最小的数排到数组前面去, 时间复杂度O(N^2) 过程: arr[0 ~ N-1] 范围上,找到最小值所在的位置,然后把最小值交换到 0 位置上 arr[1 ~ N-1] 范围上,找到最小值所在的位置,然后把最小值交换到 1 位置上 ... arr[N-1 ~ N-1] 范围上,找到最小值所在的位置,然后把最小值交换到 N-1 位置上

Types

type HeapArr

type HeapArr []int

func (*HeapArr) IsEmpty

func (h *HeapArr) IsEmpty() bool

func (*HeapArr) Len

func (h *HeapArr) Len() int

func (*HeapArr) Less

func (h *HeapArr) Less(i, j int) bool

func (*HeapArr) Pop

func (h *HeapArr) Pop() (v interface{})

func (*HeapArr) Push

func (h *HeapArr) Push(x interface{})

func (*HeapArr) Sort

func (h *HeapArr) Sort()

func (*HeapArr) Swap

func (h *HeapArr) Swap(i, j int)

Jump to

Keyboard shortcuts

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