一次巡回中,如果出现逆序的情况,就交换,一直往后移动直至巡回结束,开始下一个巡回,当没有交换发生的时候则结束。每次巡回的时候最后的元素是最大的。时间复杂度O(N^2)
- def bubble_sort(lst):
- if lst == []:
- return []
- for i in range(len(lst)):
- for j in range(1, len(lst) - i):
- if lst[j-1] > lst[j]:
- lst[j-1], lst[j] = lst[j], lst[j-1]
- return lst
Python 版:
- def select_sort(lst):
- if not lst:
- return []
- for i in range(len(lst) - 1):
- smallest = i
- for j in range(i, len(lst)):
- if lst[j] < lst[smallest]:
- smallest = j
- lst[i], lst[smallest] = lst[smallest], lst[i]
- return lst
Golang 版:
- func SelectionSort(lst []int) []int {
- if len(lst) == 0 {
- return lst
- }
- for i := 0; i < len(lst); i++ {
- smallest := i
- for j := i + 1; j < len(lst); j++ {
- if lst[j] < lst[smallest] {
- smallest = j
- }
- }
- lst[i], lst[smallest] = lst[smallest], lst[i]
- }
- return lst
- }
Python 版:
- def insert_sort(lst):
- if not lst:
- return []
- for i in range(1, len(lst)):
- j = i
- while j > 0 and lst[j] < lst[j-1]:
- lst[j-1], lst[j] = lst[j], lst[j-1]
- j -= 1
- return lst
Golang 版:
- func InsertionSort(lst []int) []int {
- if len(lst) == 0 {
- return lst
- }
- for i := 1; i < len(lst); i++ {
- for j := i; j > 0 && lst[j] < lst[j-1]; j-- {
- lst[j-1], lst[j] = lst[j], lst[j-1]
- }
- }
- return lst
- }
Python 版:
- def shell_sort(lst):
- if not lst:
- return []
- h = 1
- while h < len(lst)/3:
- h = 3*h + 1 # 1, 4, 13, 40, 121, 364...
- while h >= 1:
- for i in range(h, len(lst)):
- j = i
- while j >= h and lst[j] < lst[j-h]:
- lst[j], lst[j-h] = lst[j-h], lst[j]
- j -= h
- h /= 3
- return lst
Golang 版:
- func ShellSort(lst []int) []int {
- if len(lst) == 0 {
- return lst
- }
- h := 1
- for h < len(lst)/3 {
- h = h*3 + 1 // 1, 4, 13, 40, 121, 364...
- }
- for h >= 1 {
- for i := h; i < len(lst); i++ {
- for j := i; j >= h && lst[j] < lst[j-h]; j -= h {
- lst[j], lst[j-h] = lst[j-h], lst[j]
- }
- }
- h /= 3
- }
- return lst
- }
Python 版:
- def merge_sort(lst):
- if len(lst) <= 1:
- return lst
- mid = len(lst) // 2
- left = merge_sort(lst[:mid])
- right = merge_sort(lst[mid:])
- return merge(left, right)
-
- def merge(left, right):
- l, r, res = 0, 0, []
- while l < len(left) and r < len(right):
- if left[l] < right[r]:
- res.append(left[l])
- l += 1
- else:
- res.append(right[r])
- r += 1
- return res + left[l:] + right[r:]
Golang 版:
- func Merge(left []int, right []int) []int {
- var (
- l = 0
- r = 0
- res []int
- )
- for l < len(left) && r < len(right) {
- if left[l] < right[r] {
- res = append(res, left[l])
- l += 1
- } else {
- res = append(res, right[r])
- r += 1
- }
- }
- res = append(res, left...)
- res = append(res, right...)
- return res
- }
-
- func MergeSort(lst []int) []int {
- if len(lst) == 0 {
- return nil
- }
- mid := len(lst) / 2
- left := MergeSort(lst[:mid])
- right := MergeSort(lst[mid:])
- return Merge(left, right)
- }
Python 版:
- import random
- def quick_sort(lst):
- if not lst:
- return []
- random.shuffle(lst)
- base = lst[0]
- left = quick_sort([x for x in lst[1:] if x <= base])
- right = quick_sort([x for x in lst[1:] if x > base])
- return left + [base] + right
Golang 版:
- import (
- "time"
- "math/rand"
- )
-
- func QuickSort(lst []int) {
- if len(lst) == 0{
- return nil
- }
- rand.Seed(time.Now().UnixNano())
- rand.Shuffle(len(lst), func(i, j int) {lst[i], lst[j] = lst[j], lst[i]})
- index := Partition(lst)
- QuickSort(lst[:index])
- QuickSort(lst[index+1:])
- }
-
- func Partition(lst []int) (index int) {
- i := 0
- j := len(lst)
- base := lst[0]
- for i < j {
- for lst[++i] < base {
- if i == len(lst)-1 {break}
- }
- for lst[--j] > base {
- if j == 0 {break}
- }
- lst[i], lst[j] = lst[j], lst[i]
- }
- lst[0], lst[j] = lst[j], lst[0]
- return j
- }
Python 版:
- def sift_down(lst, index, end):
- while 2*index <= end:
- # 左子结点的索引
- child = 2 * index
- # 如果右子结点存在且比左子结点大,则应与右子结点交换
- if child + 1 <= end and lst[child+1] > lst[child]:
- child += 1 # 右子结点的索引
- # 如果当前结点的值小于子结点中的较大者,则应继续向下交换,否则结束
- if lst[index] < lst[child]:
- lst[index], lst[child] = lst[child], lst[index]
- index = child
- else:
- break
-
- def heap_sort(lst):
- if not lst:
- return []
- # 创建堆
- lst = [None] + lst
- index = (len(lst) - 1) // 2
- while index > 0:
- sift_down(lst, index, len(lst) - 1)
- index -= 1
- # 不断删除堆顶元素
- end = len(lst) - 1
- while end > 1:
- lst[1], lst[end] = lst[end], lst[1]
- end -= 1
- sift_down(lst, 1, end)
- return lst[1:]
Golang 版:
- func Sink(lst []int, index, end int) {
- for 2*index+1 <= end {
- child := 2 * index + 1
- if child + 1 <= end and lst[child+1] > lst[child] {
- child++
- }
- if lst[index] < lst[child] {
- lst[index], lst[child] = lst[child], lst[index]
- index = child
- } else {
- break
- }
- }
- }
-
- // 这里存放堆的数组采用下标从0开始,这样一来父结点是index/2-1;子结点是2*inde+1和2*index+2
- func HeapSort(lst []int) []int {
- if len(lst) == 0 {
- return lst
- }
- end := len(lst)-1
- for index := (end)/2 - 1; index >= 0; index-- {
- Sink(lst, index, end)
- }
- for end > 0 {
- lst[0], lst[end] = lst[end], lst[0]
- end--
- Sink(lst, 0, end)
- }
- return lst
- }
Python 版:
- def binary_search(array, val):
- a = 0
- b = len(array) - 1
- while a <= b:
- m = (a+b) // 2
- if val == array[m]:
- return m
- elif val < array[m]:
- b = m - 1
- else:
- a = m + 1
- return -1
Golang 版:
- func BinarySearch(array []int, val int) int {
- a := 0
- b := len(array) - 1
- for a <= b; m = (a+b)/2 {
- if val == array[m] {
- return m
- } else if val < array[m] {
- b = m - 1
- } else {
- a = m + 1
- }
- }
- return -1
- }
除了使用二分查找,还有其它的数据结构可以用于查找,由于已经在数据结构部分实现过了,因此不再详细展开: