做j2ee半年,近来翻到怀念的数据结构,记得bruceEckeel说jdk上的基本类型的排序是用快速来实现的,兴致来了,写了下非递归的,看跟jdk的Arrays.sort效率比是怎么样的..
结果大失所望,
我的:             static class myStack{

private int array[];

private int top=-1;
public myStack(int max){

array=new int[max];
}

public int peek(){

if(top<0)
throw new EmptyStackException();
return array[top];
}

public int pop(){

if(top<0)
throw new EmptyStackException();
return array[top--];
}

public void push(int i){

if(top==array.length-1)
throw new RuntimeException("full stack all already");
array[++top]=i;
}
public boolean isEmpty(){

return top==-1;
}


}
         private  static int partition(int a[],int start,int end){

int temp=a[start-1];
while(start<end){

while(start<end&&a[end-1]>=temp) end--;
a[start-1]=a[end-1];
while(start<end&&a[start-1]<=temp) start++;
a[end-1]=a[start-1];

}
//System.out.print("start:"+start+"end:"+end+"\t");
a[end-1]=temp;


return start;
}
        private static void QSort(int a[],int start,int end){

/**
 * recurvise
 */
/*
if(start<end){

int privot=partition(a, start, end);
QSort(a, start, privot-1);
QSort(a, privot+1, end);
}
*/
/**
 * no recurvise
 */

if(start<end){
myStack s=new myStack(10000);
int temp=0;
int pivot=partition(a, start, end);
s.push(end);
s.push(pivot+1);
s.push(pivot-1);
s.push(start);
while(!s.isEmpty()){

start=s.peek();
s.pop();
end=s.peek();
s.pop();
pivot=partition(a, start, end);
if(start<end){

temp=pivot+1;
if(temp<end){
s.push(end);
s.push(pivot+1);
}
temp=pivot-1;
if(start<temp){
s.push(pivot-1);
s.push(start);
}
}
}

}
}        public static void intfill(int[] a,int count){

Random r=new Random();
for(int i=0;i<a.length;i++){

a[i]=r.nextInt(count);
}
}

public static void display(int[] a){

for(int i=0;i<a.length;i++){
if(i==a.length-1)
System.out.println(a[i]);
else
System.out.print(a[i]+",");
}
}
       public static void main(String args[]) throws SecurityException, NoSuchMethodException{
          int[] a=new int[100000];
          intfill(a, 100);
          long s1=System.currentTimeMillis();
          QSort(a, 1, a.length);
//          Arrays.sort(a);
          long e1=System.currentTimeMillis();
          System.out.println("myexe time is:"+(e1-s1)+"ms");
}下面的是jdk中的代码,还用到了递归,前面的一大堆的交换,最后还用到了递归,真奇怪,还竟然可以支持千万上的数据,
看得头有点麻,不懂....
 private static void sort1(char x[], int off, int len) {
// Insertion sort on smallest arrays
if (len < 7) {
    for (int i=off; i<len+off; i++)
for (int j=i; j>off && x[j-1]>x[j]; j--)
    swap(x, j, j-1);
    return;
} // Choose a partition element, v
int m = off + (len >> 1);       // Small arrays, middle element
if (len > 7) {
    int l = off;
    int n = off + len - 1;
    if (len > 40) {        // Big arrays, pseudomedian of 9
int s = len/8;
l = med3(x, l,     l+s, l+2*s);
m = med3(x, m-s,   m,   m+s);
n = med3(x, n-2*s, n-s, n);
    }
    m = med3(x, l, m, n); // Mid-size, med of 3
}
char v = x[m]; // Establish Invariant: v* (<v)* (>v)* v*
int a = off, b = a, c = off + len - 1, d = c;
while(true) {
    while (b <= c && x[b] <= v) {
if (x[b] == v)
    swap(x, a++, b);
b++;
    }
    while (c >= b && x[c] >= v) {
if (x[c] == v)
    swap(x, c, d--);
c--;
    }
    if (b > c)
break;
    swap(x, b++, c--);
} // Swap partition elements back to middle
int s, n = off + len;
s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);
s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s); // Recursively sort non-partition-elements
if ((s = b-a) > 1)
    sort1(x, off, s);
if ((s = d-c) > 1)
    sort1(x, n-s, s);
    }    /**
     * Swaps x[a] with x[b].
     */
    private static void swap(char x[], int a, int b) {
char t = x[a];
x[a] = x[b];
x[b] = t;
    }    /**
     * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
     */
    private static void vecswap(char x[], int a, int b, int n) {
for (int i=0; i<n; i++, a++, b++)
    swap(x, a, b);
    }    /**
     * Returns the index of the median of the three indexed chars.
     */
    private static int med3(char x[], int a, int b, int c) {
return (x[a] < x[b] ?
(x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
(x[b] > x[c] ? b : x[a] > x[c] ? c : a));
    }     public static void sort(int[] a) {
sort1(a, 0, a.length);
    }啥时候才可以成为真正的程序员.哎....

解决方案 »

  1.   

    嘿嘿,这个算法是经过调优的,数据量越大速度越快,这个算法不是 Sun
    原创的也是改编自其他地方的,可以看看 Arrays.sort 中的 API 说明
    里面有参考文献。
      

  2.   

    递归在Java里面并不可怕,因为JVM可能对曾经调用过的函数输入输出进行映射缓存,
    第二,快速排序显然是在大数据量的时候才更好的发挥作用的。
    第三,写JSP的确实不需要看这些东西。
      

  3.   

    This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    比一般都要优
      

  4.   

    nlogn是排序算法的最好的 时间复杂度了嘛应该能的吧
    以前写的是 C++的。。不是JAVA的
    没有真正比较过