Algoritmos - Mit Press - Introduction To Algorithms
Algoritmos - Mit Press - Introduction To Algorithms
r, the subarray has at most one element and is therefore already sorted. Otherwise, the divide step simply computes an index q that partitions A[p..r] into two subarrays: A[p..q], containing [n/2] elements, and Afq + 1..r], containing (n/2] elements.* Merce-Sort(4,p,7) 1 ifpI elements, we break down the running time as follows. Divide: The divide step just computes the middle of the subarray, which takes constant time. Thus, D(n) = @(1) Conquer: We recursively solve two subproblems, each of size n/2, which contributes 27(n/2) to the running time. Combine: We have already noted that the MERGE procedure on an n- element subarray takes time @(71), 50 C(n) = (7).1.3. Designing algorithms Is When we add the functions D(n) and C(n) for the merge sort analysis, we are adding a function that is @(n) and a function that is @(1). This sum isa linear function of n, that is, @(n). Adding it to the 27(n/2) term from the “conquer” step gives the recurrence for the worst-case running time T(n) of merge sort U1) ifn=1, Tin)= {Hey +O(n) ifn>t In Chapter 4, we shall show that T(n) is O(n lgn), where Ign stands for log, n. For large enough inputs, merge sort, with its @(7# lg) running time, outperforms insertion sort, whose running time is (7), in the worst case. Exercises 13-1 Using Figure 1.3 as a model, illustrate the operation of merge sort on the array A= (3,41, 52,26, 38, 57,9,49). Write pseudocode for ManoE(A, 0.4.7) 13-3 Use mathematical induction to show that the solution of the recurrence T(n) = Gren) +n the Wk>t is T(n) =nign. 13-4 Insertion sort can be expressed as a recursive procedure as follows. In order to sort A[1...1}, we recursively sort A[1..n— 1] and then insert [7] into the sorted array [1.1]. Write a recurrence for the running time of this recursive version of insertion sort. 13-5 Referring back to the searching problem (see Exercise 1.1-3), observe that if the sequence is sorted, we can check the midpoint of the sequence against v and eliminate half of the sequence from further consideration. Binary search is an algorithm that repeats this procedure, halving the si of the remaining portion of the sequence each time, Write pseudocode, either iterative or recursive, for binary search. Argue that the worst-case running time of binary search is @(Ig 7). 13-6 ‘Observe that the while loop of lines 5-7 of the INsERTION-SoRT procedure in Section 1.1 uses a linear search to sean (backward) through the sorted subarray A[1..j — 1]. Can we use a binary search (see Exercise 1.3-5)16 14 Summary Chapter I Introduction instead to improve the overall worst-case running time of insertion sort to Olnign)? 13-7 & Describe a ©(11Ign)-time algorithm that, given a set S of n real numbers and another real number x, determines whether or not there exist two elements in $ whose sum is exactly x. A good algorithm is like a sharp knife—it does exactly what it is supposed to do with a minimum amount of applied effort. Using the wrong algo- rithm to solve a problem is like trying to cut a steak with a screwdriver: you may eventually get a digestible result, but you will expend consider- ably more effort than necessary, and the result is unlikely to be aesthetically pleasing. Algorithms devised to solve the same problem often differ dramatically in their efficiency. These differences can be much more significant than the difference between a personal computer and a supercomputer. As an example, let us pit a supercomputer running insertion sort against a small personal computer running merge sort. They each must sort an array of one million numbers. Suppose the supercomputer executes 100 million instructions per second, while the personal computer executes only one million instructions per second. To make the difference even more dra- matic, suppose that the world’s craftiest programmer codes insertion sort in machine language for the supercomputer, and the resulting code re- quires 2n? supercomputer instructions to sort n numbers. Merge sort, on the other hand, is programmed for the personal computer by an average programmer using a high-level language with an inefficient compiler, with the resulting code taking 50” 1gn personal computer instructions. To sort million numbers, the supercomputer takes 2+ (10°)? instructions TOF instructions/second = 20.000 seconds ~ 5.56 hours , while the personal computer takes 50 10° lg 10° instructions 10* instructions/second ~ 1,000 seconds = 16.67 minutes By using an algorithm whose running time has a lower order of growth, even with a poor compiler, the personal computer runs 20 times faster than the supercomputer! This example shows that algorithms, like computer hardware, are a tech- nology. Total system performance depends on choosing efficient algorithms ‘as much as on choosing fast hardware. Just as rapid advances are beingProblems Problems for Chapter 1 "7 made in other computer technologies, they are being made in algorithms as well, Exercises 14-1 Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size m, insertion sort runs in 8n? steps, while merge sort runs in 64” 1gn steps. For which values of 1 does insertion sort beat merge sort? How might one rewrite the merge sort pseudocode to make it even faster on small inputs? 14-2 What is the smallest value of ” such that an algorithm whose running time is 100n? runs faster than an algorithm whose running time is 2" on the same machine? 1-1 Comparison of running times For each function f(n) and time 1 in the following table, determine the largest size m of a problem that can be solved in time ¢, assuming that the algorithm to solve the problem takes f(n) microseconds. 1 1 1 1 1 1 1 second | minute | hour _| day | month | year_| century Ign 1-2 Insertion sort on small arrays in merge sort Although merge sort runs in @(7t1g7) worst-case time and insertion sort runs in @(n2) worst-case time, the constant factors in insertion sort make it faster for small n. Thus, it makes sense to use insertion sort within merge sort when subproblems become sufficiently small. Consider a modification to merge sort in which n/k sublists of length k are sorted using inser18 Chapter 1 Introduction sort and then merged using the standard merging mechanism, where k is a value to be determined. 4. Show that the n/k sublists, each of length k, can be sorted by insertion sort in @(mk) worst-case time. 4. Show that the sublists can be merged in @(nlg(n/k)) worst-case time. ¢. Given that the modified algorithm runs in @(nk + mlg(n/k)) worst-case time, what is the largest asymptotic (@-notation) value of k asa function of m for which the modified algorithm has the same asymptotic running time as standard merge sort? 4, How should k be chosen in practice? 1-3 Inversions Let A[1..2] be an array of n distinct numbers. If i ALi), then the pair (i, j) is called an inversion of A. 4@, List the five inversions of the array (2,3,8, 6,1) 4, What array with elements from the set {1, sions? How many does it have? +7} has the most inver- ¢. What is the relationship between the running time of insertion sort and the number of inversions in the input array? Justify your answer. Give an algorithm that determines the number of inversions in any permutation on n elements in @(71 Ign) worst-case time. (Hint: Modify merge sort.) Chapter notes There are many excellent texts on the general topic of algorithms, including those by Aho, Hopcroft, and Uliman [4, 5], Baase (14], Brassard and Brat- ley [33], Horowitz and Sahni [105], Knuth (121, 122, 123], Manber [142], Mehlhorn (144, 145, 146], Purdom and Brown [164], Reingold, Nievergelt, and Deo [167], Sedgewick [175], and Wilf (201]. Some of the more prac- tical aspects of algorithm design are discussed by Bentley [24, 25} and Gonnet [90]. In 1968, Knuth published the first of three volumes with the general title The Art of Computer Programming (121, 122, 123]. The first vol- ume ushered in the modern study of computer algorithms with a focus on the analysis of running time, and the full series remains an engaging and worthwhile reference for many of the topics presented here. According to Knuth, the word “algorithm” is derived from the name “al-Khowarizmi, a ninth-century Persian mathematician.