This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CSE373 Homework 2 Solutions &amp; Hints In this solution, we assume that the input is S[1..n] if there s no specification about the form of the input with a size n. We also assume that Sort(S , i, j, i) will sort S[i], S *i+1, , S[j] in an increasing order in the worstcase ? log ? time ( ? = ? ? + 1 ); while Sort(S , i, j, d) will sort in a decreasing order in the worstcase ? log ? time. In this solution, log = log 2 if there s no specification. Mostly, the proofs are omitted. 1 Exercises 41 and 42 (10 pts + 10 pts = 20) 41: Sort S (2n elements in total), then return the first n elements and the last n elements. 42: (a): Return the maximal and minimal elements in the array. Finding the maximum and minimum can be done in 3 2 2 comparisons. (b): Return the maximal and minimal elements in the array. (S[1] and S[n] in this case) (c): First sort the array, then return min 1 ? 1 ? + 1 [ ? ] (d): Return min 1 ? 1 ? + 1 [ ? ] directly. 2 Exercises 45 and 46 (10 pts + 10 pts = 20) 45: Here if we use a hashtable, we can easily handle this in time. If we do not use a hashtable, first we sort the arraylist, and then do a linear sweep to check which numbers appear most. Details omitted. 46: Sort(S 1 , 1, n, i); Sort(S 2 , 1 , n, i); for (int i = 1, j = n; i &lt;= n &amp;&amp; j &gt;= 1;){ if (S 1 [i] + S 2 [j] == x) return true; else if (S 1 [i] + S 2 [j] &gt; x) j; else i++; } return false; 3 Exercises 412 to 416 (4 pts * 5 = 20) 412: We build a minheap upon the n inputs ( running time), and then call EXTRACT_MIN() k times. (k * log ). Notes: If we first build a maxheap upon the first k elements, and then insert the rest nk elements one by one, (i.e., if the element is larger than or equals to the top of the heap, we ignore it; otherwise we substitute the element for the original top, and then adjust the heap) we will have an log ? algorithm, with a relatively smaller space usage. Moreover, we could use a worstcase algorithm to find the kth smallest element in the array, and then use the Partition() algorithm in QuickSort. For more information, please visit http://en.wikipedia.org/wiki/Selection_algorithm or refer to Introduction to Algorithms , section 9.3, Selection in worstcase linear time , 2 nd edition. 413: (a): it doesnt matter (b): maxheap (here we suppose that we know the pointer to the element we want to delete, and that the sorted array is not saved as a linked list) (c): maxheap (d): sorted array 414: Like MergeSort, we keep a record of k pointers. Initially we set the pointers to 1. Firstly we build a heap upon S i [1] ( 1 ? ? ). Then we extract the minimum of the heap, increase by 1 the pointer of the array from which the minimum we extracted comes, and insert the next element of the array into the heap if there is any. The running time is...
View
Full
Document
This note was uploaded on 10/02/2011 for the course CS 7300 taught by Professor R during the Spring '11 term at LSU.
 Spring '11
 r
 Algorithms, Sort

Click to edit the document details