Sorting Flashcards
Bubble Sort time and space complexity
Time O(n^2), Space O(1)
Timsort time and space complexity
Time O(n log(n)), Space O(n)
what is the property of a heap?
heap[k] <= heap[2k+1] and heap[k] <= heap[2k+2]
https://runestone.academy/runestone/books/published/pythonds/Trees/BinaryHeapImplementation.html
how does bubble sort work and what’s the complexity?
Bubble sort is probably the least efficient sorting algorithm with time O(n^2) and space O(1). Because it can be easily done in place.
how does quick sort work and what’s the complexity?
quick sort works by splitting the list into two, and then picking a partition point, and then swapping items until the partition condition is met, which is that all values <= to the partition come before the partition, and that all values > partition come after. How you pick the partition has a big impact on the performance in different cases.
Performance Time O(n log n), Space O(log n)
how does merge sort work and what’s the complexity?
The basic algorithm is recursive, and breaks the list to be sorted into halves, sorts each half, then merges the result.
Time O(n log n), Space (worst) O(n)
Merging in place could use a deque, or you could use the merge function from heapq.