InterviewQuestion-QuickSort

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014110320/article/details/78813667

1。Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is to find the corresponding pairs of nuts and bolts. Each nut fits exactly one bolt and each bolt fits exactly one nut. By fitting a nut and a bolt together, the carpenter can see which one is bigger (but the carpenter cannot compare two nuts or two bolts directly). Design an algorithm for the problem that uses nlogn compares (probabilistically).

Hint: modify the quicksort partitioning part of quicksort.

2。第 2 个问题

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 and k=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 in b[]. Recur in a subproblem of roughly half the size.
Approach B: Design a constant-time algorithm to determine whether a[i] is the kth largest key. Use this subroutine and binary search.

3。第 3 个问题

Decimal dominants. Given an array with n keys, design an algorithm to find all values that occur more than n/10 times. The expected running time of your algorithm should be linear.

Hint: determine the (n/10)th largest key using quickselect and check if it occurs more than n/10 times.

猜你喜欢

转载自blog.csdn.net/u014110320/article/details/78813667
今日推荐