排序算法记录

排序算法


冒泡排序(bubble sort)

优化后时间复杂度是O(n)

do
    swapped = false
    for i = 1 to indexOfLastUnsortedElement - 1
        if leftElement > rightEmement
            swap(leftElement,rightElement)
            swapped = true
while swapped

选择排序(selection sort)

repeat (numOfElements -1) times
    set the forst unsorted element as the minimum
    for each of the unsorted elemenys
        if element < currentMinimum
            set element as new minimum
    swap minimum with first unsort position

插入排序(insertion sort)

mark first element as sorted
for each unsorted element X
    'extract' the element X
    for j =  lastSortedIndex down to 0
        if current element j > X
            move sorted element to the right by 1
        break loop and insort X here

归并排序(merge sort)

split each element into partitions of size 1
recursively merge adjancent partitions
  for i = leftPartStartIndex to rightPartLastIndex inclusive
    if leftPartHeadValue <= rightPartHeadValue
      copy leftPartHeadValue
    else: copy rightPartHeadValue
copy elements back to original array

快速排序(quick sort)

for each (unsorted) partition
set first element as pivot
  storeIndex = pivotIndex + 1
  for i = pivotIndex + 1 to rightmostIndex
    if element[i] < element[pivot]
      swap(i, storeIndex); storeIndex++
  swap(pivot, storeIndex - 1)

随机快速排序在数据量大时优于快速排序

计数排序(countion sort)

create key (counting) array
for each element in list
  increase the respective counter by 1
for each counter, starting from smallest key
  while counter is non-zero
    restore element to list
    decrease counter by 1

基数排序(radix sort)

create 10 buckets (queues) for each digit (0 to 9)
for each digit placing
  for each element in list
    move element into respective bucket
  for each bucket, starting from smallest digit
    while bucket is non-empty
      restore element to list
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部...
    蚁前阅读 10,595评论 0 52
  • 概述:排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部...
    每天刷两次牙阅读 9,091评论 0 15
  • 概述排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的...
    Luc_阅读 6,776评论 0 35
  • Ba la la la ~ 读者朋友们,你们好啊,又到了冷锋时间,话不多说,发车! 1.冒泡排序(Bub...
    王饱饱阅读 5,773评论 0 7
  • 总结一下常见的排序算法。 排序分内排序和外排序。内排序:指在排序期间数据对象全部存放在内存的排序。外排序:指在排序...
    jiangliang阅读 5,239评论 0 1

友情链接更多精彩内容