Java 在数组中查找

import java.util.Scanner;
public class Text {
	public static void main(String args[]){
		Funtion f=new Funtion();
		int[] a={5,8,1,6,9,55,88};
		f.sort(a);
		//for(int i=0;i<a.length;i++){
		///	System.out.println(a[i]);
		//}
		Scanner scanner=new Scanner(System.in);
		System.out.print("输入整数判断该数是否在数组中");
		int number=scanner.nextInt();
		f.Isexist(a, number);
	}
	
}

public class Funtion {
		void sort(int[] array){
		for(int i=0;i<array.length;i++){
			for(int j=i+1;j<array.length;j++){
				if(array[j]<array[i]){
					int t=array[j];
					array[j]=array[i];
					array[i]=t;
				}
			}
		}
}


void Isexist(int [] array,int number){
	int start=0,end,middle,count=0;
	int N=array.length;
	end=N;
	middle=(start+end)/2;
	while(number!=array[middle]){
		if(number>array[middle])
			start=middle;
		else if(number<array[middle])
			end=middle;
		middle=(start+end)/2;
		count++;
		if(count>N/2)
			break;
	}
	if(count>N/2)
		System.out.printf("%d不在数组中。\n",number);
	else
		System.out.printf("%d在数组中。\n",number);		
	}
}

你可能感兴趣的:(Java 在数组中查找)