Sorting algorithms Cheat Sheet
by pryl via cheatography.com/66402/cs/16808/
Sorting algorithms and Methods Merge sort
Sorting algorit hms Methods function merge_sort(list m)
Bubble sort Exchanging // Base case. A list of zero or one elements is
sorted, by definition.
Heapsort Selection
if length of m ≤ 1 then
Insertion sort Insertion
return m
Introsort Partitioning & 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 Partitioning 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 Distribution 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
Algorithms Best Case Worst Case
else
Bogosort n ∞ add x to right
Bubble sort n n2 // Recursively sort both sublists.
Bucket sort (uniform keys) - n2k left := merge_sort(left)
right := merge_sort(right)
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 shuffle(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 (length(array)-1) do
function insertionSortR(array A, int n) insert array[i] into buckets[msbits(array[i], k)]
if n>0 for i = 0 to n - 1 do
insertionSortR(A,n-1) nextSort(buckets[i]);
x ← A[n] return the concatenation of buckets[0], ....,
j ← n-1 buckets[n-1]
while j >= 0 and A[j] > x
A[j+1] ← A[j] Resources
j ← j-1 https://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms
end while http://bigocheatsheet.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 complexities Bubble sort (cont)
Algorithms Average Case Memory end procedure
complexity
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 := partition(A, lo, hi)
Bucket sort (uniform n+k nk
keys) quicksort(A, lo, p - 1 )
quicksort(A, p + 1, hi)
Burstsort n(k/d) n(k/d)
algorithm partition(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
swap(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