[Hackerrank] Find the median (partition finds the median of the array)

Source: Internet
Author: User

In the quicksort challenges, you sorted an entire array. sometimes, you just need specific information about a list of numbers, and doing a full sort wocould be unnecessary. can you figure out a way to use your partition code to find the median in an array?

Challenge 
Given a list of numbers, can you find the median?

Input Format 
There will be two lines of input:

  • N-The size of the array
  • Ar-NNumbers that makes up the Array

Output Format 
Output one integer, the median.

Constraints 
1 <=N<= 1000001
-10000 <=X<= 10000, X, ar
There will be an odd number of elements.

 

Question: The jiuwen partition function can be used to find the median of an unordered array. Today, we have finally implemented this function.

Set a variable need to half the length of the array, and another variable hasfound to the number of smaller than the found number than the median. When hasfound = need, we find the median.

After each partition, check the number of elements in the array smaller than limit. If hasfound adds this element to the need, then limit is the median. If hasfound adds this element greater than need, it means that the division of the array smaller than limit needs to be continued, and partition is called recursively; If hasfound adds this element less than need, add the number of elements to hasfound and divide the array that is larger than the limit.

For example, for an array: 3 1 4 5 2, the process of finding the median is shown in:

The Code is as follows:

 1 import java.util.*; 2  3 public class Solution { 4     private static int need = 0; 5     private static int hasFound = 0; 6      7     private static void swap(int[] ar,int i,int j){ 8         int temp = ar[i]; 9         ar[i]= ar[j];10         ar[j]=temp;11     }12     private static int Partition(int[] ar,int start,int end){13         int pivot = ar[end];14         int i = start;15         int j = start;16         while(i < end){17             if(ar[i] >= pivot)18                 i++;19             else if(ar[i] < pivot){20                 swap(ar,i,j);21                 i++;22                 j++;23             }24         }25         swap(ar, j, end);26         if(hasFound+j-start+1==need)27             return pivot;28         else if(hasFound+j-start+1<need){29             hasFound += j-start+1;30             return Partition(ar, j+1, end);31         }32         else {33             return Partition(ar, start, j-1);34         }35         36     }37     38     public static void main(String[] args) {39         Scanner in = new Scanner(System.in);40         int n = in.nextInt();41         need = n%2==0?n/2:n/2+1;42         int[] ar = new int[n];43         44         for(int i = 0;i < n;i ++)45             ar[i] = in.nextInt();46         System.out.println(Partition(ar, 0, n-1));47         48     }49 }

The above code can be easily expanded to find the K-small number in the array. You only need to change the value of the need variable.

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.