Find the number k in the array to find the number k in the array

Source: Internet
Author: User

Given an array, find the number k in the array. The implementation code is as follows:

Package com.threeTop.www; /** * Find out the number of K in the array * @author WJGS * * */public class Findk {public static void found (Int[]array,int begin,int end,int
		
		k) {int i=partition (array,begin,end);
		if (i+1>k) {//left half, recursive lookup find (array,begin,i-1,k);
		else if (i+1<k) {//Right half part, recursive lookup find (array,i+1,end,k);
		    }else {System.out.println ("found the" +k+ "large number is:" +array[i]);
		Return  }/** * Quick sort per round * @param array * @param begin * @param end * @return/private static int partition (int[)
	    	  
	    	  array, int begin, int end) {if (begin<end) {//first number as Datum int key=array[begin]; while (Begin<end) {while (Begin<end&&array[end]>key) {End
	    		  --;
	    			  } if (Begin<end) {array[begin]=array[end];
	    		  begin++;
	    		  while (Begin<end&&array[begin]<key) {begin++; } if (BegiN<end) {Array[end]=array[begin];
	    		  end--;
	    	  
	      }} Array[begin]=key;
	return begin;
		public static void Main (string[] args) {int []array={1,5,7,9,2,4,6,8,10,12,11,14,15,13};
		
	Findk.find (Array, 0,array.length-1, 8);
 }

}

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.