150 likes | 265 Views
Sorting & Searching. 27 10 12 20. divide. 27 10. 12 20. divide. divide. 10. 12. 20. 27. merge. merge. 10 27. 12 20. merge. 10 12 20 27. Mergesort. Merge two sorted arrays. first array. second array. result of merge. 10 27. 12 20. 10. 10 27.
E N D
27 10 12 20 divide 27 10 12 20 divide divide 10 12 20 27 merge merge 10 27 12 20 merge 10 12 20 27 Mergesort
Merge two sorted arrays first array second array result of merge 10 27 12 20 10 10 27 12 20 10 12 10 27 12 20 10 12 20 10 27 12 20 10 12 20 27
Analysis of Mergesort • Dividing two arrays takes constant time • just compute midpoint of array • Merging two arrays of size m requires at least m and at most 2m - 1 comparisons • one comparison at each step of the merge • elements in result array are not compared again • add at least one element to result at each step • Time to merge is linear in the size of the array • Total number of comparisons is the sum of the number of comparisons made at each merge
Logarithm • Logarithm of a number n • power to which any other number a must be raised to produce n • a is called the base of the logarithm • Frequently used logs have special symbols • lg n = log2 n logarithm base 2 • ln n = loge n natural logarithm (base e) • log n = log10 n commonlogarithm (base 10)
Logarithm base 2 • lg 1 = 0 because 20 = 1 • lg 2 = 1 because 20 = 2 • lg 4 = 2 because 22 = 4 • lg 8 = 3 because 23 = 8 • lg 16 = 4 because 24 = 16 • If we assume n is a power of 2, then the number of times we can recursively divide n numbers in half is lg n …
1 1 1 1 1 1 2 2 2 4 4 … … n/2 n/2 n Total number of comparisons < 2n comparisons < 4*(n/2) = 2n comparisons < 8*(n/4) = 2n comparisons lg n < 4*(n/2) = 2n comparisons
Running time • Running time of Mergesort in (n lg n) • best, worst and average-case • Compare with Insertion sort:
Quicksort • Most commonly used sorting algorithm • One of the fastest sorts in practice • Best and average-case running time in O(n lg n) • Worst-case running time is quadratic • Runs very fast on most computers when implemented correctly
Searching • Determine the location or existence of an element in a collection of elements of the same type • Easier to search when the elements are already sorted • finding a phone number in the phone book • looking up a word in the dictionary • What if the elements are not sorted?
Sequential search • Collection of n unsorted elements • Compare each element in sequence • Worst-case: Unsuccessful search • search element is not in input • make n comparisons • search time is linear • Average-case: • expect to search ½ the elements • make n/2 comparisons • search time is still linear
Searching sorted input • If the input is already sorted, we can search more efficiently than linear time • Example: “Higher-Lower” • think of a number between 1 and 1000 • have someone try to guess the number • if they are wrong, you tell them if the number is higher than their guess or lower • Strategy? • How many guesses should we expect to make?
Best Strategy • Always pick the number in the middle of the range • Why? • you eliminate half of the possibilities with each guess • We should expect to make at most lg1000 10 guesses • lg 512 = 9 • lg 1024 = 10 • 512 < 1000 < 1024
15 9 12 4 8 7 6 1 15 9 12 8 9 9 8 Binary search • Search n sorted inputs in logarithmic time • Example: Search for 9 • At most threecomparisons
Exercises • An algorithm takes 2 seconds to run for input size 64. How long will it take for input size 512 if the running time is • logarithmic? • linear? • quadratic?