折半查找只能针对有序的数组,如:
public static int halfSearch(int[] arr,key){
int max,mid,mid;
min=0
max=arr.length-1;
mid=(min+max)/2;
while(arr[mid]!=key){
if(arr[mid]>key){
min=mid+1;
}else if(key<arr[mid]){
max=mid-1;
}
return mid;
}
}
冒泡排序:
public static void bubbleSort(int[] arr){
for(int x=0;x<arr.length-1;x++){
for(int y=0;y<arr.length-x-1;y++){
if(arr[y]>arr[y+1]){
int temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
}