To find the largest number of K in an array

Source: Internet
Author: User
Tags arrays rand
To find the largest number of K in an arrayTags: randomsystem algorithm 2011-08-01 19:44 8474 People read review (3) Favorite Report Category: Data structure (+/C + +) (62)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Problem: There is an array of size n a[0,1,2,..., n-1], which is the number of k large.

This problem is a classic problem, in the "Introduction to the algorithm" as a separate section, and its solution is a good use of the idea of division, the time complexity of the control in the O (n), which is more than we expected, here is not a table.

The problem can also be deformed as: there is an array of size n a[0,1,2,..., n-1], which is the number of the first K.

One word difference, the original problem is "the K big", the problem of deformation is "the former K big", but the average time complexity can be controlled in O (n), which makes people secretly amazed.

Let us first analyze the original problem: there is an array of size n a[0,1,2,..., n-1], which is the number of k large.

We first take the exception, make k=1, then is to take the largest number, as long as the array can be scanned once to determine the value, if k=2, then scan both sides of the array can determine the second largest number, and so on, time complexity is O (k*n), if K and N is an order of magnitude, then the time complexity is O (n*n) , obviously not the optimal solution.

Considering divide and conquer method, the difficulty lies in how to decompose the problem into two sub-problems.

One of the most basic steps for quick sorting:

Randomly take a number x, swap it with the element at the end of the array, and then swap the smaller number to the previous one, and the larger number to the back.

This step causes the fast sorting problem of an array to be sorted into two sub-arrays, and now we are going to solve the problem of taking the number of k large.

Set the following table from 0 onwards to n-1 end.

1. Randomly take a number and swap it with the element at the end of the array.

A) Idx=rand (0,n-1); generates a random number between [0,n-1].

b) Swap (Array[idx], array[n-1]);

2, with the end of the element x, will be smaller than the number of x Exchange to the previous, larger than the number of x Exchange to After, and return at this time x in the array position mid.

3, if mid==n-k, then return the value, this is the number of k large. http://blog.csdn.net/hackbuteer1/article/details/6651804

If mid>n-k, then the number of k large is in the left half group, and in the left half group is the number of K (n-mid) large.

If mid<n-k, then the number k is in the right half of the group, and is still the number of K.

Method One:

Method Two:[CPP]  View plain  copy #include   "iostream"    using namespace std;       Int random_partion (int *p, int n)    {         int idx=rand ()%n;        swap (p[idx], p[n-1]);        int i=-1;    //i represents the position of the last element less than p[n-1]          int j=0;     //j used to scan arrays          for (j=0; j<n; j++)         {                //less than p[n-1] to the first half                 if (p[j]<p[n-1])                 {           & nbsp;        swap (P[++i], p[j]);                }        }         swap (p[++i], p[n-1]);        return i;    }      int getmaxk (int *p, int n, int k)     {       int mid;        if (k<=0)                return -1;         if (n<k)                 return -1;        mid=random_partion (p, n)    //: The original array is divided into one         if (mid == n-k)        //if mid==n-k, then return this value, this is the number of K             return  p[mid];        else if (mid<n-k)        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;GETMAXK (p+mid+1, n-mid-1, k);   //if mid< N-k, then the number k is in the right half of the group, and is still the K large number         else             return getmaxk (p, mid, k-(N-mid));    //if mid> N-k, then the number of k large is in the left half, and in the left half of the group is the number of K (n-mid) Large   }      int main (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.