java array of query

Array Package; 

public class ArrayFind { 
	public static void main (String [] args) { 
		int [] = {13,15,19,28,33,45,78,106 ARR}; 
		int Key = 15; 
		int index = binarySearch2 (ARR , Key); 
		System.out.println (index); 
	} 

	/ * 
	 * array common features: Find 
	 * / 
	public static int getIndex (int [] ARR, int Key) { 
		for (int X = 0; X <arr.length ; X ++) { 
			IF (ARR [X] == Key) 
				return X; 
		} 
		return -1; 
	} 

	/ ** 
	 * binary search (for ordered array) 
	 * / 
	public static int binarySearch (int [] ARR, int Key) { 
		// definition of three indicator variables. 
		0 min = int;  
		int max = arr.length -. 1;
		int MID = (max + min) / 2;
		// binary cycle, condition, min <= max 
		the while (Key = ARR [MID]!) { 
			// find elements and make the intermediate index elements compared 
			IF (Key> ARR [MID]) { 
				min. 1 + = MID; 
			IF the else} (Key <ARR [MID]) { 
				max = MID -. 1; 
			} 
			IF (max <min) { 
				return -1; 
			} 
			MID = (max + min) / 2; 
		} 
		return MID; 
	} 
	
	/ ** 
	 * binary search (for ordered array) 
	 * / 
	public static int binarySearch2 (int [] ARR, int Key) { 
		int max, min, MID; 
		min = 0; 
		max =-arr.length. 1; 

		the while (min <= max) 
		{ 
			MID = (max + min) >>. 1; 
 
			IF (Key> ARR [MID])
				min = MID +. 1; 
			the else IF (Key <ARR [MID])
				max = mid - 1;
			else
				return mid;
		}
		return -1;
	}
}

  

Guess you like

Origin www.cnblogs.com/yueluoshuxiang/p/11361609.html