You are on page 1of 2

Sorting algorithms Cheat Sheet

by pryl via cheatography.com/66402/cs/16808/

Sorting algorithms and Methods Merge sort

Sorting algorit​ hms Meth​ods function merge_sort(list m)

Bubble sort Exchanging ​ ​ ​ // Base case. A list of zero or one elements is


sorted, by defini​tion.
Heapsort Selection
​ ​ ​ if length of m ≤ 1 then
Insertion sort Insertion
​ ​ ​ ​ ​ ​ ​ ​return m
Introsort Partit​ioning & Selection
​ ​ ​ // Recursive case. First, divide the list into
Merge sort Merging
equal-​sized sublists
Patience sorting Insertion & Selection ​ ​ ​ // consisting of the first half and second half of
Quicksort Partit​ioning the list.

Selection sort Selection ​ ​ ​ // This assumes lists start at index 0.


​ ​ ​ var left := empty list
Timsort Insertion & Merging
​ ​ ​ var right := empty list
Unshuffle sort Distri​bution and Merge
​ ​ ​ for each x with index i in m do

Best and Worst Case ​ ​ ​ ​ ​ ​ ​ if i < (length of m)/2 then


​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ add x to left
Algo​rithms Best Case Worst Case
​ ​ ​ ​ ​ ​ ​ else
Bogosort n ∞ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ add x to right
Bubble sort n n2 ​ ​ ​ // Recurs​ively sort both sublists.

Bucket sort (uniform keys) - n2k ​ ​ ​ left := merge_​sor​t(left)


​ ​ ​ ​right := merge_​sor​t(r​ight)
Heap sort n log n n log n
​ ​ ​ // Then merge the now-sorted sublists.
Insertion sort n n2
​ ​ ​ ​return merge(​left, right)
Merge sort n log n n log n

Quick sort n log n n2 Bogosort


Selection sort n2 n2
while not isInOrder(deck):
Shell sort n log n n4/3 ​ ​ ​ ​shu​ffl​e(deck)

Spreadsort n n(k/s+d)

Timsort n n log n Bucket sort

Unshuffle sort n kn function bucketSort(array, n) is


​ ​buckets ← new array of n empty lists
Insertion sort ​ for i = 0 to (lengt​h(a​rra​y)-1) do

function insertionSortR(array A, int n) ​ ​ ​ ​insert array[i] into bucket​s[m​sbi​ts(​arr​ay[i], k)]

​ ​ ​ ​ if n>0 ​ for i = 0 to n - 1 do

​ ​ ​ ​ ​ ​ ​ ​ins​ert​ion​Sor​tR(​A,n-1) ​ ​ ​ ​nex​tSo​rt(​buc​ket​s[i]);

​ ​ ​ ​ ​ ​ ​ x ← A[n] ​ ​return the concat​enation of bucket​s[0], ....,

​ ​ ​ ​ ​ ​ ​ j ← n-1 bucket​s[n-1]

​ ​ ​ ​ ​ ​ ​ ​while j >= 0 and A[j] > x


​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​A[j+1] ← A[j] Resources

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ j ← j-1 https:​//e​n.w​iki​ped​ia.o​rg​/wi​ki/​Sor​tin​g_a​lgo​rit​hm#​Com​par​iso​n_o​f_a​lgo​rithms
​ ​ ​ ​ ​ ​ ​ end while http:/​/bi​goc​hea​tsh​eet.com
​ ​ ​ ​ ​ ​ ​ ​A[j+1] ← x
​ ​ ​ ​ end if
end function

By pryl Published 27th August, 2018. Sponsored by CrosswordCheats.com


cheatography.com/pryl/ Last updated 27th August, 2018. Learn to solve cryptic crosswords!
Page 1 of 2. http://crosswordcheats.com
Sorting algorithms Cheat Sheet
by pryl via cheatography.com/66402/cs/16808/

Sorting algorithm comple​xities Bubble sort (cont)

Algo​rithms Average Case Memory end procedure


comple​xity

Bitonic sorter log 2 n n log2 n Quicksort

Bogosort n × n! 1 algorithm quicksort(A, lo, hi) is

Bubble sort n2 1 ​ ​ ​ if lo < hi then


​ ​ ​ ​ ​ ​ ​ p := partit​ion(A, lo, hi)
Bucket sort (uniform n+k nk
keys) ​ ​ ​ ​ ​ ​ ​ ​qui​cks​ort(A, lo, p - 1 )
​ ​ ​ ​ ​ ​ ​ ​qui​cks​ort(A, p + 1, hi)
Burstsort n(k/d) n(k/d)
algorithm partit​ion(A, lo, hi) is
Counting sort n+r n+r
​ ​ ​ ​pivot := A[hi]
Heap sort n log n 1
​ ​ ​ i := lo
Insertion sort n2 1
​ ​ ​ for j := lo to hi - 1 do
Introsort n log n log n ​ ​ ​ ​ ​ ​ ​ if A[j] < pivot then

LSD Radix Sort n(k/d) n+2 d ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ swap A[i] with A[j]

Merge sort n log n n ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ i := i + 1


​ ​ ​ swap A[i] with A[hi]
MSD Radix Sort (in- n(k/d) 2d
place) ​ ​ ​ ​return i

Patience sort - n
Selection sort
Pigeonhole sort n+2 k 2k
procedure selection sort
Quicksort n log n log n
​ ​ list : array of items
Selection sort n2 1
​ ​ n : size of list
Shell sort Depends on gap 1
​ ​ for i = 1 to n - 1
sequence
​ ​ / set current element as minimum /
Spaghetti sort n n2 ​ ​ ​ ​ ​ min = i
Spreadsort n(k/d) (k/d)2d ​

Stooge sort n(log 3/log1.5) n ​ ​ ​ ​ ​ / check the element to be minimum /


​ ​ ​ ​ ​ for j = i+1 to n
Timsort n log n n
​ ​ ​ ​ ​ ​ ​ ​ if list[j] < list[min] then

Bubble sort ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ min = j;


​ ​ ​ ​ ​ ​ ​ ​ end if
procedure bubbleSort( A : list of sortable items )
​ ​ ​ ​ ​ end for
​ ​ ​ n = length(A)
​ ​ ​ ​ ​ / swap the minimum element with the current
​ ​ ​ ​repeat
element/
​ ​ ​ ​ ​ ​ ​ ​swapped = false
​ ​ ​ ​ ​ if indexMin != i then
​ ​ ​ ​ ​ ​ ​ for i = 1 to n-1 inclusive do
​ ​ ​ ​ ​ ​ ​ ​ swap list[min] and list[i]
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ if A[i-1] > A[i] then
​ ​ ​ ​ ​ end if
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​swa​p(A​[i-1], A[i])
​ ​ end for
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​swapped = true
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ end if
end procedure
​ ​ ​ ​ ​ ​ ​ end for
​ ​ ​ ​ ​ ​ ​ n = n - 1
​ ​ ​ ​until not swapped

By pryl Published 27th August, 2018. Sponsored by CrosswordCheats.com


cheatography.com/pryl/ Last updated 27th August, 2018. Learn to solve cryptic crosswords!
Page 2 of 2. http://crosswordcheats.com

You might also like