算法函数,算法作业,算法导论
- 格式:docx
- 大小:320.35 KB
- 文档页数:7
1. Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size n, insertion sort runs in 8n2steps, while merge sort runs in (64n lg n) steps. For which values of n does insertion sort beat merge sort?Answer: if we want the insertion sort beat the merge sort, the condition that should be satisfied with is:8n2 < 64n lg nn < 8 lg n2n< n82<= n < =43So when n range from 2 to 43,the insertion sort beat the merge sort。
2. Algorithm Mystery(A: Array [i..j] of integer)if i=j then return A[i]elsek=i+floor((j-i)/2)temp1= Mystery(A[i..k])temp2= Mystery(A[(k+1)..j]if temp1<temp2 then return temp1 else return temp2What does the recursive algorithm above compute?Determine and state the two recurrence relations of this algorithm.Determine the values of constants in the recurrences by assuming that acomparison, returning a value, reading a value from an array cell, variable assignment, arithmetic operations and the floor function have a cost of 1 each.Use one of the methods discussed in class to determine the EXACT mathematical expression for T(n). Based on that expression, state the order of complexity of this algorithm.Now use a different method to solve the recurrences and show that the order of complexity you stated in (c) is right. Show details of your work if you want to get partial credit.Answer:(a) the recursive algorithm above compute the smallest integer in the A:Array[i..j](找序列里的最小值)(b)Divide and conquer algorithm. It divide the array into two parts ,each of the two parts continue divide the array into two parts ,and when it can’t be divided , it is recursive, and then ,compare the two part and get the smaller number.(分治方法,当递归时将数组分为两块来比较,递归取小的值) (c)Answer:T steps and time costreturned value calculate k function call compareT(1) 1 0 0 0T(2) 2 1 2 2T(3) 3 2 4 4T(4) 4 3 8 8…T(n) n n-1 2(n-1) 2(n-1)T(n)=n+(n-1)*5=6n-5complexity of this algorithm: O(n)(d)T(n) = 2T(n/2) + O(1)T(n) = 4T(n/4) + 3O(1)T(n) = 8T(n/8) + 7O(1)..T(n) = nT(1) + (2lgn-1)O(1)T(n) = O(n)So complexity of this algorithm: O(n)3. Provide an algorithm for Min-Heap-Increase-Key (A, i, key); use the same pseudo-code conventions as the Heap algorithms we discussed in Ch. 6. Answer:PARENT(i)return i/2LEFT(i)return 2*iRIGHT(i)return 2*i+1MIN-HEAPIFY(A,i)l=LEFT(i)r=RIGHT(i)if l<=A.heap-size and A[l] < A[i]smallest=lelsesmallest=iif r<=A.heap-size and A[r] < A[i]smallest=relsesmallest=iif smallest!=iexchange A[i] with A[smallest]MIN=HEAPIFY (A,smallest)A.heap-size = A.lengthfor i = A.length 」downto 1MIN-HEAPIFY(A,i)HEAPSORT(A)BUILD-MIN-HEAP(A)for i=A.length downto 2exchenge A[1] with A[i]A.heap-size = A.heap-size-1MIN-HEAPIFY(A,1)4. Illustrate the operation of Min-Heap-Insert (A, 5) on the heap A=<5, 7, 6, 9, 11, 15, 14, 12, 13, 18, 17, 16>. Use the heap of Fig. 6.5 (pp 141) as a model to show the operation of the Min-Heap-Decrease-Key call. For this problem you must first figure out how to change Max-Heap-Insert and Max-Heap-Increase-Key algorithms to get Min-Heap-Insert and Min-Heap-Decrease-Key algorithms, but you do not have to provide these algorithms in your answer.57 69 11 15 14 12 13 18 17 1657 69 11 15 14 12 13 18 17 16 +∞57 69 11 15 1412 13 18 17 16 557 69 11 5 14 12 13 18 17 16 1557 59 11 6 145. Using Figure 7.1 as a model, illustrate the operations of QUICKSORT which uses the Median-of-Three pivot picking strategy (assume that the median is swapped with the rightmost element and then the Partition algorithm is called) on the array <25, 33, 22, 45, 27, 37, 40, 28, 24>.Exchange median with the rightest6. Observe that the while loop of line 5-7 of the INSERTION-SORT procedure usesa linear search or scan (backward) through the sorted subarray A[1, … ,j-1]. Can we use a binary search instead to improve the overall worst-case running time of insertion sort to Θ(n lg n)?(reference 1: INSERTION-SORT12 13 18 17 16 15reference 2: binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value, whether alone or part of a record, within a sorted array. It works by comparing the target value to the middle element of the array; if they are not equal, the lower or upper half of the array is eliminated depending on the result and the search is repeated until the position of the target value is found.)将插入排序的顺序查找改为二分查找算法:Answer:INSERTION-SORT(A)for j = 2 to A.lengthkey = A[j]//Insert A[j] into the sorted sequence A[1..j-1]high = j-1low = 1while low < highmid = (low+high)/2if key ==A[mid] thenbreakif key < A[mid] thenhigh = mid-1if key > A[mid] thenlow = mid+1for i= mid to j-1A[i+1] = A[i]A[mid] = keyWith the overall worst-case the complexity of the binary search isΘ(n lg n) but when insert the complexity of the array move is still n ²,so in general the running time can’t inprove to Θ(n lg n).(最差情况下,二分查找法的时间复杂度是) Θ(n lg n),但是插入时数组移动的时间复杂度仍然是n ²,所以总体运行时间不能提高到Θ(n lg n).。