If you just want to sort the int array: use quicksort ... This is not a lot of code, but N * lgN in avarage or N ^ 2 in the worst case. To sort multiple data, use Java Compare (as above) or a stable sorting algorithm
static void quicksort(int[] a,int l, int r){ if(r <= l) return; int pivot = partition(a,l,r); //Improvement, sort the smallest part first if((pivot-l) < (r-pivot)){ quicksort(a,l,pivot-1); quicksort(a,pivot+1,r); }else{ quicksort(a,pivot+1,r); quicksort(a,l,pivot-1); } } static int partition(int[] a,int l,int r){ int i = l-1; int j = r; int v = a[r]; while(true){ while(less(a[++i],v)); //-> until bigger while((less(v,a[--j]) && (j != i))); //-> until smaller and not end if(i >= j){ break; } exch(a,i,j); } exch(a,i,r); return i; }
dario nascimento
source share