Sorting Algorithm Analysis
Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a phone number in the phone book –looking up a word in the dictionary –finding a book in the library –buying a cd/dvd –renting a video –buying groceries Any more ideas???
How to sort? Sorting the student roster: Alex Guttler Matt Simon Alexandra Eurdolian Katie Blaszak Joseph Kelly An Xuan Rebecca Davis Sorting a hand of cards
Comparison-based sorting Ordering decision based on comparison of elements Requires that elements are comparable –have a natural order numerical numbers lexicographic (alphabetical) characters chronological dates
Arrays Used to store a collection or list of elements of the same type Arrays are indexed Example: an array A of integers 4 is the element of the array at index 1 A[1]= array elements array indices A =
Insertion Sort Similar to sorting a hand of cards Good for sorting a small number of elements Idea: –n is number of elements or input size –start at element A[0] already sorted –for k = 1, 2, …, n sort elements A[0] through A[k] by comparing A[k] with each element A[k-1], A[k-2], …, A[1], A[0]
Example 1. Start: 8 is sorted 2. Compare 4,8 Swap 4,8 3. Compare 1,8 Swap 1,8 Compare 1,4 Swap 1,4 4. Compare 9,8 5. Compare 6,9 Swap 6,9 Compare 6,8 Swap 6,8 Compare 6,
Algorithm analysis Determine the amount of resources an algorithm requires to run –computation time, space in memory Running time of an algorithm is the number of basic operations performed –additions, multiplications, comparisons –usually grows with the size of the input –faster to add 2 numbers than to add 2,000,000! Example: Adding n numbers takes linear time –requires n –1 basic operations (additions) –number of basic operations is proportional to the size of the input
Running times Worst-case running time –upper bound on the running time –guarantee the algorithm will never take longer to run Average-case running time –time it takes the algorithm to run on average (expected value) Best-case running time –lower bound on the running time –guarantee the algorithm will not run faster
Analysis of Insertion Sort We compare each element with previous elements until the ordering is correct In the worst case, we compare each element with all of the previous elements –A[1] is compared with A[0] –A[2] is compared with A[1], A[0] –A[3] is compared with A[2], A[1], A[0] –A[4] is compared with A[3], A[2], A[1], A[0] –A[n-1] is compared with A[n-2], …, A[1], A[0] …
Number of comparisons (worst case) Element k requires k comparisons Total number of comparisons: … + n-1 = ½ (n)(n-1) = ½ (n 2 -n) Running time of insertion sort in the worst case is quadratic Worst case behavior occurs when the array is in reverse sorted order
Number of comparisons (best case) What if the array is already sorted? How many comparisons per element will be made by insertion sort?
Running time of Insertion Sort Best case running time is linear Worst case running time is quadratic Average case running time is quadratic Insertion sort is only practical for small input –100 operations to sort 10 elements –10000 operations to sort 100 elements – operations to sort 1000 elements There are more efficient sorting algorithms!
The divide-and-conquer approach Insertion sort uses an incremental approach –puts elements in correct place one at a time We can design more efficient sorting algorithms using the divide-and-conquer approach: –Divide the problem into a number of subproblems –Conquer the subproblems by solving them recursively until the subproblems are small enough to solve directly –Merge the solutions for the subproblems into the solution for the original problem
Mergesort Divide-and-conquer sorting algorithm Given an array of n elements –Divide the array into two subarrays each with n/2 items –Conquer (solve) each subarray by sorting it recursively –Merge the solutions to the subarrays by merging them into a single sorted array
Example divide merge
Merging two sorted arrays result of mergesecond arrayfirst array