key2 - CSE373 Homework 2 Solutions & Hints In...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSE373 Homework 2 Solutions & 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 worst-case ? log ? time ( ? = ? ? + 1 ); while Sort(S , i, j, d) will sort in a decreasing order in the worst-case ? log ? time. In this solution, log = log 2 if there s no specification. Mostly, the proofs are omitted. 1 Exercises 4-1 and 4-2 (10 pts + 10 pts = 20) 4-1: Sort S (2n elements in total), then return the first n elements and the last n elements. 4-2: (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 4-5 and 4-6 (10 pts + 10 pts = 20) 4-5: 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. 4-6: Sort(S 1 , 1, n, i); Sort(S 2 , 1 , n, i); for (int i = 1, j = n; i <= n && j >= 1;){ if (S 1 [i] + S 2 [j] == x) return true; else if (S 1 [i] + S 2 [j] > x) j--; else i++; } return false; 3 Exercises 4-12 to 4-16 (4 pts * 5 = 20) 4-12: We build a min-heap upon the n inputs ( running time), and then call EXTRACT_MIN() k times. (k * log ). Notes: If we first build a max-heap upon the first k elements, and then insert the rest n-k 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 worst-case algorithm to find the k-th smallest element in the array, and then use the Partition() algorithm in QuickSort. For more information, please visit or refer to Introduction to Algorithms , section 9.3, Selection in worst-case linear time , 2 nd edition. 4-13: (a): it doesnt matter (b): max-heap (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): max-heap (d): sorted array 4-14: 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.

Page1 / 7

key2 - CSE373 Homework 2 Solutions & Hints In...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online