归并排序是 前面特别慢, 忽然起速就特别快,然后就完了。是不是像什么
/**
* Created by fupeng on 2017/1/20.
*/
public class merge {
static void show(int [] a){
for(int i : a){
System.out.println(i);
}
}
static void exch(int [] a, int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
static void merge(int []a, int i, int mid ,int j){
int s1 = i, e1 = mid;
int s2 = mid+1, e2 = j;
int [] b = new int [a.length];
int tmp = 0;
while(s1<=e1 && s2<=e2){
if(a[s1] < a[s2]){
b[tmp++] = a[s1++];
}else{
b[tmp++] = a[s2++];
}
}
while(s1<=e1)
b[tmp++] = a[s1++];
while(s2<=e2)
b[tmp++] = a[s2++];
for(int o = 0; o < tmp; o++){
a[i+o] = b[o];
}
}
static void div(int [] a, int i, int j){
if(i>=j){
return;
}
int mid = i + (j-i)/2;
div(a,i,mid);
div(a,mid+1,j);
merge(a,i,mid,j);
}
static void sort(int [] a){
div(a, 0, a.length-1);
}
public static void main(String[] args) {
int [] a = {2,3,4,1,5,9,8,6,7,0};
sort(a);
show(a);
}
}
归并排序的代码 看起来比较啰嗦, 其实它一直在分解问题,把问题分解成最小的问题,然后通过解决一大堆小问题,最终完成一个大问题。
农村包围城市的战略。