sorting algorithms Flashcards
Insertion sort
- keys
- average runtime
- worst case run time
- additional space
- in place - yes/no
- stable - yes/no
Insertion sort
- any
- O (n2)
- O (n2)
- O (1)
- in place - yes
- stable - yes
note - insertion sort is really fast for small arrays, even more than quick sort for example
Bubble sort
- keys
- average runtime
- worst case run time
- additional space
- in place - yes/no
- stable - yes/no
Bubble sort
- any
- O (n2)
- O (n2)
- O (1)
- in place - yes
- stable - yes
Merge sort
- keys
- average runtime
- worst case run time
- additional space
- in place - yes/no
- stable - yes/no
Merge sort
- any
- O (nlogn)
- O (nlogn)
- O (n)
- in place - no
- stable - yes
Heap Sort
- keys
- average runtime
- worst case run time
- additional space
- in place - yes/no
- stable - yes/no
Heap Sort
- any
- O (nlogn)
- O (nlogn)
- O (1)
- in place - yes
- stable - no
Quick sort
- keys
- average runtime
- worst case run time
- additional space
- in place - yes/no
- stable - yes/no
Quick sort
- any
- O (nlogn)
- O (n2) - depends on pivot selection (generally the mean should be picked)
- O (1)
- in place - yes
- stable - no
Counting sort
- keys
- average runtime
- worst case run time
- additional space
- in place - yes/no
- stable - yes/no
counting sort
- integers [0…k]
- O (n+k)
- O (n+k)
- O (n+k)
- in place - no
- stable - yes
Radix sort
- keys
- average runtime
- worst case run time
- additional space
- in place - yes/no
- stable - yes/no
Radix sort
- integers with d digits in base b
- O (d(n+b)) - assuming we use counting sort, for b = O (n) we will get O(n)
- O (d(n+b)
- depends on the used stable sort
- in place - depends on the used stable sort
- stable - yes
Bucket sort
- keys
- average runtime
- worst case run time
- additional space
- in place - yes/no
- stable - yes/no
Bucket Sort
- numbers with a unioform distribution from [0,1)
- O(n)
- O(n2)
- O(n)
- in place - no
- stable - yes
Selection sort
- keys
- average runtime
- worst case run time
- additional space
- in place - yes/no
- stable - yes/no
Selection sort
- any
- O(n2)
- O(n2)
- O(1)
- in place - yes
- stable - no (could be implemented as stable for added overhead)
sort according to insertion sort
[50, 80, 10, 30, 90, 60]

sort according to bubble sort
[9, 6, 2, 8, 3, 1, 7, 5, 4]

sort according to merge sort
[7, 3, 2, 16, 24, 4, 11, 9]

sort according to selection sort
[29, 72, 98, 13, 87, 66, 52, 51, 36]

sort according to heap sort
[6, 5, 3, 1, 8, 7, 2, 4]
https://en.wikipedia.org/wiki/Heapsort#/media/File:Heapsort-example.gif
sort according to quick sort (take 5 = pivot)
[8, 1, 6, 4, 0, 3, 9, 5]
[8, 1, 6, 4, 0, 3, 9, 5] (pivot = 5)
[1, 4, 0, 3, 5, 8, 6, 9] pivots = (3,6)
[1, 0, 3, 4, 5, 6, 8, 9] pivots = (1,8)
[0, 1, 3, 4, 5, 6, 8, 9]
sort according to counting sort
[2, 3, 0, 5, 2, 3, 0, 0, 5] (k=5)
count the number of appearances of each number:
A [2, 3, 0, 5, 2, 3, 0, 0, 5]
0 1 2 3 4 5
C [3, 0, 2, 2, 0, 2]
go over C and find number of elements smaller than each number
C [3, 3, 5, 7, 7, 9]
go over A from end to beginning and place each number correct amount of times
[0,0,0,2,2,3,3,5,5]
sort according to radix sort
329
457
657
839
436
720
sort each digit from right to left using a stable sort
329 720 720 329
457 436 329 436
657 457 436 457
839 657 839 657
436 329 457 720
720 839 657 839
sort according to bucket sort
[0.87, 0.71, 0.36, 0.62, 0.27, 0.94, 0.12, 0.21, 0.32]
Creat array B
[, 0.12 , 0.27 , 0.36, , 0.62, 0.71 , 0.84, 0.94]
0.21 , 0.32
than sort each sub list using insertion sort and concat them