Sorting Algorithms Flashcards
Quick Sort Time & Space Complexity
Quick Sort (Divide & Conquer)
- Time Complexity
- Best: O(n log n)
- AVG: O(n log n)
- Worst: O(n^2) - when the pivot is always the smallest or largest element
- Space Complexity
- O(log n) - due to the recursion stack
Merge Sort Time & Space Complexity
Merge Sort (Divide & Conquer)
- Time Complexity
- Best: O (n log n)
- AVG: O (n log n)
- Worst: O (n log n) - same for all cases
- Space Complexity
- O(n) - for temporary arrays used during the merge process
Heap Sort Time & Space Complexity
- Time Complexity
- Best: O (n log n)
- AVG: O (n log n)
- Worst: O (n log n)
- Space Complexity
- O(n) for heapq (min-heap, push/pop)
- O (1) (only for manual)
When to use Merge Sort
large datasets
when stability is needed (not OK if equal elements are moved around after sorting)
linked lists
When not to use merge sort
small datasets (overhead isnt worth it. overhead = processing power, memory)
memory constrained environments
Strengths of Merge Sort
Stable sorting (equal objects won’t move around places)
Guaranteed O(n log n) time complexity
good for linked lists
weaknesses of merge sort
O(n) space complexity (requires extra memory)
slower than quicksort on average
when to use quicksort
small to medium datasets
when average case performance matters
in-place sorting desired (modifies input data directly, spatially efficient)
when not to use quicksort
worst-case performance is bad (already sorted data)
when stability is needed (not OK with it moving around values of equal value)
strengths of quick sort
average case O(n log n) time
in place sorting: O(log n) space
weaknesses of quick sort
worst case O(n^2) time complexity
not stable
recursive depth can lead to stack overflow on large datasets
when to use heap sort
when guaranteed O(n log n) performance is needed
memory constrained environments
steaming data (continuous data flow)
when not to use heap sort
for small datasets (often slower than other algorithms)
when stability is required (no moving around same value objects)
strengths of heapsort
guaranteed O(n log n) time complexity
useful for priority queues
weaknesses of heap sort
not stable
slow than quicksort on average