HW2 Solutions

# HW2 Solutions - CSc 445 Homework 2 Solutions 1 Finding k...

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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSc 445: Homework 2 Solutions February 20, 2008 1. Finding k ∈ [1 ..n ] efficiently s.t A [1 ..k ] is increasing and A [ k + 1 ..n ] is decreasing. Solution We use a fairly straightforward variant of the binary search. FindK(A, low, high) i = b ( low + high ) / 2 c if ( low == 0 or high == length ( A ) ) then output One way sequence(either only ascending or descending) else if ( A [ i- 1] < A [ i ] and A [ i ] ≥ A [ i + 1]) then output k = i. else if ( A [ i- 1] ≤ A [ i ] and A [ i ] ≤ A [ i + 1]) then call FindK(i, high) else call FindK(low, i) endif endFindK The recursion equation is T ( n ) = 2 T ( n/ 2) + c , yielding O (lg n ). 2. Who won the election? One approach is to sort the votes according to candidate ID, then scan the sorted array and count up which candidate so far has the most votes. We only have to remember the winner, so we don’t need a clever data structure. Since the candidate identifiers are very large integers, we cannot use an O ( n ) time sort. But there is no problem with using a nice comparison-based in-place sort like heapsort. We are allowed to use heapsort because we are given the array A to work with, i.e., as writable memory. Our solution naively ignores ties, although if a tie occurred it would be a very significant event in a major election. But extra code to detect a tie would only make the solution bulkier without providing any additional insight. So, assuming the input is given as A [1] ,...,A [ n ], we have the following rather tedious pseudocode (on the next page): 1 if n = 0 then return ‘no one voted, so everybody loses’ Heapsort( A [1] ,...,A [ n ] ) winID ← A [1] (* Identity of winner, as far as I now know *) winCount ← 1 (* Number of votes for winner *) nowID ← A [1] (* Identity of current candidate I’m tallying votes for *) nowCount ← 1 (* Number of votes for current candidate *) for i ← 2 to n if A [ i ] = nowID then nowCount ← nowCount + 1 else nowID ← A [ i ] nowCount ← 1 if winID = nowID then winCount ← nowCount else if nowCount > winCount winID ← nowID winCount ← nowCount return winID Heapsort takes time O ( n log n ), and can be done in-place, so it only uses an additional O (1) of storage in addition to the input array. The scan of the sorted array does a constant-time conditional n- 1 times, thus using O ( n ) time, and so our overall time bound is O ( n log n ). 3. Sorting input in the range [0 ,n 2- 1]. The intuitive idea here is to “view” each element as a two-digit integer in radix- n , where each digit ranges from 0 to n- 1; then radix sort gives us a linear time result. Given array A of n elements, each of which is an integer in the range [0 ,n 2- 1], we represent the i-th element by A [ i ] = H [ i ] n + L [ i ], where H [ i ] := j A [ i ] n k and L [ i ] := A [ i ]- H [ i ] n....
View Full Document

## This homework help was uploaded on 04/08/2008 for the course CSC 445 taught by Professor Kobourov during the Spring '08 term at Arizona.

### Page1 / 10

HW2 Solutions - CSc 445 Homework 2 Solutions 1 Finding k...

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

View Full Document
Ask a homework question - tutors are online