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]
![](https://s3.amazonaws.com/brainscape-prod/system/cm/320/062/209/a_image_thumb.jpg?1599304059)
sort according to bubble sort
[9, 6, 2, 8, 3, 1, 7, 5, 4]
![](https://s3.amazonaws.com/brainscape-prod/system/cm/320/062/280/a_image_thumb.png?1599304106)
sort according to merge sort
[7, 3, 2, 16, 24, 4, 11, 9]
![](https://s3.amazonaws.com/brainscape-prod/system/cm/320/062/321/a_image_thumb.png?1599304186)
sort according to selection sort
[29, 72, 98, 13, 87, 66, 52, 51, 36]
![](https://s3.amazonaws.com/brainscape-prod/system/cm/320/062/355/a_image_thumb.png?1599304246)
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]