Professional Documents
Culture Documents
Competitive Resources WEEK @2
Competitive Resources WEEK @2
Competitive Resources WEEK @2
COMPETITIVE PROGRAMING
WEEK @2
➔ Two pointers
➔ Sorting
◆ [[Optional]] Read about all these sorting algos from gfg to have a basic
deeper understanding of sorting.
Sorting Time Time Time Space Stable Comments
Algo Best Worst Average
case case case
Bubble O(n^2) O(n^2) O(n^2) O(1) Yes For each pair of indices, swap the
sort elements if they are out of order.
Modified O(n) O(n^2) O(n^2) O(1) Yes At each pass check if the array is
bubble already sorted. Best case-array
sort already sorted
Selection O(n^2) O(n^2) O(n^2) O(1) Yes Swap happens only once in a single
Sort pass.
Insertion O(n) O(n^2) O(n^2) O(1) Yes Very small constant factor even if the
Sort complexity is O(n^2).
Best case: Array already sorted
Worst case: Sorted in reverse order
Quick O(n.lg(n)) O(n^2) O(n.lg(n)) O(1) Yes Best case: When pivot divide in 2
sort equal halves
Worst case: Array already sorted
-1/n-1 partitions