Merge sort (divide and conquer)
- divide
- recur
- conquer
uses recursion to make it work
- first while loop will check which element is the smaller one
- removes the element and moves it to the back of S if so
- keeps iterating with same procedure
- if first while loop ends, continues to the second while loop with same steps
- keep repeating same steps until all elements are put to S in order
If 7 element exist, why 4 on the left, 3 on the right
mid = (0 + 6) / 2 = 3
MergeSort(S, 0, 3) // has 4 elements
MergeSort(S, 4, 6) // has 3 elementsgenerally if there’s odd number of elements, the extra one in the middle will be put to the left
Analysis of merge sort
- height h of the merge sort tree is 0(log n)
- total running time of merge sort is 0(n log n)
- overall amount or work done at the nodes of depth i is 0(n)
| depth | seqs | size |
|---|---|---|
| 0 | 1 | n |
| 1 | 2 | n/2 |
| i |
Bucket sort
Analysis
- phase 1 takes 0(n) time
- phase 2 takes 0(n + N) time
is the number of items needs to be inserted N is the number of times to iterate the entire list
Bucket sort takes 0(n + N) time
Radix sort
radix sort runs in time 0(d(n+1))
sorting in sequence of 4-bit integers its doing bucket sort in 4 bit integer lmao
Selection Algorithm
Quick Select
smallest, k = 0 2nd smallest, k = 1 5th smallest, k = 4
77 99 22 66 55 44 11 88 33_ 22 11 33* 77 99 66 55 44 88_ pi= 2 < k k > pi
22 11 33* 77 66 55 44_ 88* 99 pi= 7 k < pi
22 1 133* 44* 77 66 55_ 44 88* 99 pi= 3 k > pi
22 11 33* 44* 55* 77 66 88* 99 pi = 4 = k, found 5th smallest = 55
0(n)