1. //归并排序
void merge_sort(int * A, int x, int y, int * T){
if (y-x > 1 ){
int m = x+(y-x)/2 ;
int p = x, q = m, i = x;
//递归
merge_sort(A, x, m, T);
merge_sort(A, m, y, T);
while (p<m || q<y) {
if (q >= y || (p<m && A[p]<=A[q]))
T[i++] = A[p++];
else T[i++] = A[q++];
}
for (i=x; i<y; i++)
A[i] = T[i];
}
}
2. //快速排序
3. //二分查找(跌倒效应)
int bsearch(int * A, int x, int y, int v){
int m;
while (x < y) {
m = x+(y-x)/2 ;
if (A[m] == v)
return m;
else if (A[m] > v)
y = m;
else x = m+1 ;
}
return -1 ;
}
//二分查找寻找下界
int lower_bound(int * A, int x, int y, int v){
int m;
while (x < y){
m = x+(y-x)/2 ;
if (A[m]>=v)
y = m;
else x = m+1 ;
}
return x;
}
//二分查找寻找上界
int upper_bound(int * A, int x, int y, int v){
int m;
while (x < y){
m = x+(y-x)/2 ;
if (A[m] < v)
y = m;
else x = m+1 ;
}
return x;
}