排序算法記錄

排序算法


冒泡排序(bubble sort)

優(yōu)化后時間復(fù)雜度是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)

隨機快速排序在數(shù)據(jù)量大時優(yōu)于快速排序

計數(shù)排序(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

基數(shù)排序(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述 排序有內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進(jìn)行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部...
    蟻前閱讀 5,301評論 0 52
  • 概述:排序有內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進(jìn)行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部...
    每天刷兩次牙閱讀 3,826評論 0 15
  • 概述排序有內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進(jìn)行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部的...
    Luc_閱讀 2,372評論 0 35
  • Ba la la la ~ 讀者朋友們,你們好啊,又到了冷鋒時間,話不多說,發(fā)車! 1.冒泡排序(Bub...
    王飽飽閱讀 1,897評論 0 7
  • 總結(jié)一下常見的排序算法。 排序分內(nèi)排序和外排序。內(nèi)排序:指在排序期間數(shù)據(jù)對象全部存放在內(nèi)存的排序。外排序:指在排序...
    jiangliang閱讀 1,518評論 0 1

友情鏈接更多精彩內(nèi)容