Hint: modify the quicksort partitioning part of quicksort.
Remark: This research paper gives an algorithm that runs in nlog4n time in the worst case.
use nut to partition bolts and use bolt to partition nuts at corresponding half.
Selection in two sorted arrays. Given two sorted arrays a[] and b[] , of sizes n1 and n2 , respectively, design an algorithm to find the kth largest key. The order of growth of the worst case running time of your algorithm should be logn , where n=n1+n2 .
- Version 1:
n1=n2 andk=n/2 - Version 2:
k=n/2 - Version 3: no restrictions
Hint: there are two basic approaches.
- Approach A: Compute the median in
a[] and the median inb[] . Recur in a subproblem of roughly half the size. - Approach B: Design a constant-time algorithm to determine whether
a[i] is thekth largest key. Use this subroutine and binary search.
Dealing with corner cases can be tricky.
binary search b[0] in a, get i
if i < k , return a[k]
else
k -= i
binary search a[i+1] in b
...
Decimal dominants. Given an array with
Hint: determine the (n/10)th largest key using quickselect and check if it occurs more than n/10 times.
Alternate solution hint: use 9 counters.
没有评论:
发表评论