Sort Algorithms Flashcards
Time complexity of bubble sort
O(n**2)
Time complexity of insert sort
O(n^2)
Bubble sort vs Insertion sort
Both has time complexity of O(n^2), where sorting time will grow quadratically.
Insertion sort takes less time than bubble sort for large values of n.
Time complexity of merge sort
O(nlogn)
Time complexity of quick sort (worst and average)
Worst case: O(n^2)
Worst case occurs when:
1. the elements in the array is mostly sorted
2. Pivot chosen is not the median
average: O(nlogn)
What does stable mean?
Stable implementation ensures that order of elements with same sorting characteristics is preserved
What does in place/ out of place mean?
Out of place means that additional data structures are instantiated /created in the process
In place means that additional data structures are not created as the process is done in the original array.
Describe Bubble Sort (S,M,SD,LD,ASD)
Stable
small Memory usage as implementation is usually in place and not much overhead
efficient with Small Datasets
slowest with Large Datasets
efficient with Almost Sorted Datasets
Describe Insertion Sort (S,M,SD,LD,ASD)
Stable
small Memory usage as in place implementation
efficient with Small Datasets
inefficient with Large Datasets as time complexity O(n^2)
most efficient with Almost Sorted Datasets
Describe mergsort (S,M,SD,LD,ASD)
Stable
large Memory usage as implementation is usually recursive and thus incurs large overhead
inefficient with Small Datasets
efficient with Large Datasets
inefficient with almost sorted Datasets as the array will be divided regardless of
Describe quick Sort (S,M,SD,LD,ASD)
usually Unstable
Memory usage depends on in place/out of place
efficient with Small Datasets
efficient with Large Datasets but slower than mergesort
MOST INEFFICIENT with Almost Sorted Datasets as the array is split up regardless, which makes the array more unsorted