标签:swap 总结 wap static art ++ quic -- break
1 //merge sort 2 public static void merge(int[] a,int aux[],int lo ,int mid,int hi ){ 3 int i = lo; 4 int j = mid+1; 5 //copy a 6 for (int k = lo; k <= hi; k++) 7 aux[k] = a[k]; 8 //merge 9 for(int k = lo;k<=hi;k++){ 10 //先判断是否越界 11 if(i>mid) a[k]=aux[j++]; 12 else if(j>hi) a[k]=aux[i++]; 13 // merge 14 else if(aux[j]<=aux[i]) a[k]=aux[j++]; 15 else a[k]=aux[i++]; 16 } 17 } 18 public static void mergesort(int[] a,int aux[],int lo,int hi) { 19 //边界条件 20 if(lo>=hi) return ; 21 int mid = (hi-lo)/2+lo; 22 mergesort(a,aux, lo, mid); 23 mergesort(a,aux, mid+1, hi); 24 merge(a,aux, lo, mid, hi); 25 26 } 27 public static void mergeSort(int[] a){ 28 int aux[] = new int[a.length]; 29 mergesort(a,aux, 0, a.length-1); 30 }
1 //quick sort 2 //partion 3 private static int partion(int[] a ,int lo,int hi) { 4 int v = a[lo]; 5 int i = lo ,j=hi+1; 6 while(true){ 7 while(a[++i]<v) if(i>=hi) break; 8 while(a[--j]>v) if(j<=lo) break; 9 if(i>=j) break; 10 swap(a, i, j); 11 } 12 swap(a, lo, j); 13 return j; 14 } 15 public static void partionsort(int[] a,int lo,int hi) { 16 if(lo>=hi) return; 17 int j = partion(a, lo, hi); 18 partionsort(a, lo, j); 19 partionsort(a, j+1, hi); 20 21 } 22 public static void partionSort(int[] a) { 23 partionsort(a, 0, a.length-1); 24 } 25
标签:swap 总结 wap static art ++ quic -- break
原文地址:http://www.cnblogs.com/zle1992/p/8007143.html