Find the number M in N unordered numbers

Source: Internet
Author: User

Question: Find the M-largest number in a group of unordered numbers. For example, in a group of unordered numbers: 3, 2, and 321, find the 2nd-largest number. Obviously, here we can see that it is number 3.


Resolution: There are many solutions to this type of question. The simplest way is to directly find the number of M after the overall sorting. This efficiency is obviously very low. I am here to use the fast sorting idea to solve this problem. First, find a number in the unordered number as the middle value (I am here using the first number ), then, move the number greater than the median value to the left of the median value, and move the number greater than the median value to the right of the median value. This will happen after the split:

1. When the number of left-side intervals is greater than or equal to M (the maximum value of M), this interval is recursive.

2. When the left interval is smaller than M and the difference between M and M is 1, the center value is output.

3. the above two cases are not consistent, recursive right range, find the right range of the {M-1-(number of the Left range)} large number

 


Java code:


[Java]
Public class FindNumber {
// Locate the M-largest number in N unordered numbers
Public static void main (String [] args ){
Int [] so = new int [] {321,654, 234,233,432,623, 4 };
Int m = new FindNumber (). find (so, 8, 0, so. length-1 );
System. out. println (m );
}
Public int find (int [] so, int m, int start, int end ){
If (start <end ){
Int midValue = so [start]; // The first element is used as the intermediate value.
Int I = start;
Int j = end + 1;
While (true) {// This while loop will be less than the center value to the left, greater than the center value to the right
While (I <end & so [++ I] <= midValue );
While (j> start & so [-- j]> = midValue );
If (I <j ){
// I, j subscript value exchange
Swap (so, I, j );
} Else {
Break;
}
}
Swap (so, start, j );
// Number of the left half
Int p = j-1-start <0? 1 :( j-start );
If (p> = m ){
Return find (so, m, start, J-1 );
} Else if (p + 1 = m ){
Return so [j];
} Else {
Return find (so, m-1-p, j + 1, end );
}
} Else {
Return so [start];
}
}
// Exchange
Public void swap (int [] so, int I, int j ){
Int t = so [I];
So [I] = so [j];
So [j] = t;
}
}

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.