二分法的查找
public int dicFind(int min,int max,int a[],int value){
if (a[min] > value || a[max] < value){
return -2;
}
int mid = (min+max)/2;
if(min == max && a[min] != value){
return -2;
}
if(a[mid]>value){
return dicFind(min,mid,a,value);
} else if(a[mid] < value){
return dicFind(mid,max,a,value);
} else {
return mid;
}
}
单例-懒汉式
class A{
private static A a = null;
private A(){}
public static A getInstance(){
if(a == null){
a = new A();
}
return a;
}
}
单例-饿汉式
class B{
private static B a = new B();
private B(){}
public static B getInstance(){
return b;
}
}
快速排序
public static void quickSort(int[] arr,int begin,int end){
if(begin>=end) return;
int point = data[begin];
int i = begin + 1;
int j = end;
while(true){
while(i<end&& point > data[i]){
i++;
}
while(j>begin && point < data[j]){
j--;
}
if(i<j){
swap(data,i,j);
} else {
break;
}
}
swap(data,begin,j);
quickSort(data,begin,j-1);
quickSort(data,j+1,end);
}
private static void swap(int[] data,i,j){
if(i==j){
return;
}
data[i] = data[i] + data[j];
data[j] = data[i] - data[j];
data[i] = data[i] - data[j];
}
网友评论