搜索和排序

搜索分:线性搜索(普通的搜索)和二分搜索;
排序分:选择排序、冒泡排序、堆排序。
二分搜索:
public int binarySearch(int[] number,int searchValue){
int low=0,
high=number.length-1,
mid=(low+high)/2;

while(low<=high && number[mid] != searchValue){
System.out.println(mid);
if(number[mid]<searchValue){
low=mid+1;
}else{
high=mid-1;
}
mid=(low+high)/2;
}
if(low>high){
System.out.println("NoSuch");
}
return mid;
}

冒泡排序:
public void bubbleSort(int[] number){
int temp,bottom,i;
boolean exchanged=true;
bottom = number.legth-1;
while(changed){
exchanged=false;
for(i=0;i<bottom;i++){
if(number[i]>number[i+1]){
temp=number[i];
number[i]=number[i+1];
number[i+1]=temp;
exchange=true;
}
}
bottom--;
}
}

你可能感兴趣的:(Exchange)