Recursive : Divide and Conquer
Consider your input instance
- Allocate work
- construct one or more sub-instances
1 assume by magic your friends give you the answer for these, trust your friends to sovle sub problem.
2 each sub-instance must be a smaller instance of the same problem.
- Use this help to solve your own instance.
Do NOT worry about anything else.
- Who your boss is...
- How your friends solve their instances...
- No global variables or effects.
Recursive algorithm
- assume you have an algorithm that works
- use it to write an algorithm that works.
Recursive sorts
- given list of objects to be sorted
- split the list into two sub-lists
- recursively have a friend sort the two sub-lists.
- combine the two sorted-list into one entirely sorted list.
size of sublist | ||
n/2,n/2 | n-1,1 | |
minimal efforts splitting, lots of efforts recombining. |
merge sort | insertion sort |
lots of efforts splitting, minimal efforts recombining. |
quick sort | selection sort |
partition sort: partition set into two using randomly chosen pivot.