1.对一个整形数组进行冒泡排序
public static void mp(int []a){
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[i]<a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
2.二分法查找(针对已排序数组)
public static int ef(int []a, int tag){
int low = 0;
int hign = a.length -1;
while(low <= high){
for(int i=0;i<a.length;i++){
int middle = (low + high)/2;
if(tag == a[middle]){
return middle;
}else if(tag < a[middle]){
high = middle -1;
}else{
low = middle + 1;
}
}
}else{
return -1;
}
}
原文地址:https://www.cnblogs.com/klzs226/p/8601855.html
时间: 2024-11-11 16:59:21