1. 程式人生 > >氣泡排序,二分查表法

氣泡排序,二分查表法

**

java氣泡排序

 public static void main(String[] args) {
	int x = 45;
	int[] arr = {12,3,33,58,36,223,24,41,5,45,36};
	for(int i=0;i<arr.length;i++){
	    for(int j=0;j<arr.length-1-i;j++){
		if(arr[j]>arr[j+1]){
		    int a = arr[j];
		    arr[j] = arr[j+1];
		    arr[j+1] = a;
		}
	    }
	}
	//  排序後得到的陣列
	System.out.println(Arrays.toString(arr));
	// 進行二分查表
	int low = 0;
	int hight = arr.length-1;
	while(low<hight){
	    int model = (low+hight-1)>>>1;
	    if(arr[model] == x){
		System.out.println("下標為:"+model+";--下標對應的值:"+arr[model]+";");
		return;
	    }else if(arr[model] > x ){
		hight = model - 1;
	    }else if(arr[model] <  x){
		low = low + 1;
	    }
	}
    }