做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);
}啥时候才可以成为真正的程序员.哎....
结果大失所望,
我的: 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);
}啥时候才可以成为真正的程序员.哎....
解决方案 »
- 广播编程的问题,我的接收端无法接收发送端发来的数据
- fatjar 的问题,怎么才能让配置文件留在外面
- 这里有人用nio吗?
- 【求Thinking in Java代码包】
- 请问若干个java进程不共享变量,如何用消息通讯,急问!!!
- java在什么网址有详细的学习材料?jbuild什么地方可以下载?怎么安装?分数随答案马上给出!
- 急需各位帮忙???送上100分!!!
- 用JAVA怎么画拓扑图,我现在一点谱都没有?请指点,高手----送绝对高分!
- 在JBuilder for winnt中如何去掉输入法的bug?
- 开机密码
- 初学者基础问题,我的ID就是立刻结贴的保障!
- 关于Hibernate通过数据库表生成PO类文件
原创的也是改编自其他地方的,可以看看 Arrays.sort 中的 API 说明
里面有参考文献。
第二,快速排序显然是在大数据量的时候才更好的发挥作用的。
第三,写JSP的确实不需要看这些东西。
比一般都要优
以前写的是 C++的。。不是JAVA的
没有真正比较过