冒泡排序
static void mp(int[] arr) { int temp; for (int i = 0; i < arr.length; i++) { boolean isSort=true; int last=0; int sortBorder =arr.length - 1 ; for (int j = 0; j < sortBorder - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; isSort=false; last=j+1; } } sortBorder=last; if(isSort) {//已经有序了 break; } } }
二分排序算法
public int ef(int[] array,int tag){
int left=0;
int right=array.length-1;
int middle=0;
for (int i = 0; i < array.length/2; i++) {
middle=(left+right)>>1;
if(array[middle] == tag){
return middle;
}else if(array[middle] > tag){//说明目标在左边
right=middle-1;
}else{//说明目标在右边
left=middle+1;
}
}
return -1;
}