Sorting
Top K problem
Optimization progress:
Regular sorting: O(NlogN)
Priority Queue: O(NlogK)
QuickSelect: O(N) average with shuffle. still O(N^2) in worst case though.
Bucket sort: O(N)
Randomization
Shuffle
Reservoir sampling
Explain: https://www.youtube.com/watch?v=A1iwzSew5QY
Sorting in linkedlist
merge sort: https://leetcode.com/problems/sort-list
selection sort: https://leetcode.com/problems/insertion-sort-list/description/
Summary
Merge Sort
Quick Sort
Bubble Sort
Selection Sort
Insertion Sort
Heap Sort
Array
represent aheap
(Complete Binary Tree) : root i, left 2i+1, right 2i+2;heapify
the heap, make itmaxHeap
swap root and last one, therefore 'remove' largest to the array tail
heapify
the rest
Shell Sort
Bucket Sort
special sorting
counting sort: https://leetcode.com/problems/sort-colors/description/
partition sort: part of Quick Sort, https://leetcode.com/problems/kth-largest-element-in-an-array/description/
Reference
https://www.toptal.com/developers/sorting-algorithms
Last updated