2. Sorting
⢠Given an array, put the elements in order
â Numerical or lexicographic
⢠Desirable characteristics
â Fast
â In place (donât need a second array)
â Stability
3. Insertion Sort
⢠Simple, able to handle any data
⢠Grow a sorted array from the beginning
â Create an empty array of the proper size
â Pick the elements one at a time in any order
â Put them in the new array in sorted order
⢠If the element is not last, make room for it
â Repeat until done
⢠Can be done in place if well designed
13. Merge Sort
⢠Fast, able to handle any data
â But canât be done in place
⢠View the array as a set of small sorted arrays
â Initially only the 1-element âarraysâ are sorted
⢠Merge pairs of sorted arrays
â Repeatedly choose the smallest element in each
â This produces sorted arrays that are twice as long
⢠Repeat until only one array remains
21. Divide and Conquer
⢠Split a problem into simpler subproblems
â Keep doing that until trivial subproblems result
⢠Solve the trivial subproblems
⢠Combine the results to solve a larger problem
â Keep doing that until the full problem is solved
⢠Merge sort illustrates divide and conquer
â But it is a general strategy that is often helpful
22. Quick Sort
⢠For example, given
80 38 95 84 99 10 79 44 26 87 96 12 43 81 3
we can select the middle entry, 44, and sort
the remaining entries into two groups, those
less than 44 and those greater than 44:
38 10 26 12 43 3 44 80 95 84 99 79 87 96 81
⢠If we sort each sub-list, we will have sorted
the entire array
23. A sample heap
⢠Each node is larger than its children
19
1418
22
321
14
119
15
25
1722
24. Sorting using Heaps
⢠What do heaps have to do with sorting an array?
Because the binary tree is balanced and left justified, it can be
represented as an array
â All our operations on binary trees can be represented as
operations on arrays
â To sort:
heapify the array;
while the array isnât empty {
remove and replace the root;
reheap the new root node;
}
25. Summary of Sorting Algorithms
⢠in-place, randomized
⢠fastest (good for large inputs)
O(n log n)
expected
quick-sort
⢠sequential data access
⢠fast (good for huge inputs)
O(n log n)merge-sort
⢠in-place
⢠fast (good for large inputs)
O(n log n)heap-sort
O(n2)
O(n2)
Time
insertion-sort
selection-sort
Algorithm Notes
⢠in-place
⢠slow (good for small inputs)
⢠in-place
⢠slow (good for small inputs)