Thread: Could Be Simple
View Single Post
Old Nov 14th, 2004, 5:48 AM   #2
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 406
Rep Power: 5 xavier is on a distinguished road
Send a message via Yahoo to xavier
And another thing : in this quick sort I get:
Exception in thread "main" java.lang.StackOverflowError
i'm not sure y!! -- actualy i don't know y!
import java.util.*;
class quicksort{
	public static void main(String args[]){
 final int N=1000;
        final int L=1000;
          int a[]=new int[N];
 Random r=new Random();
  for(int i=0;i<N;i++)
 	a[i]=r.nextInt(L);
 
 Date d=new Date();
 long t0=d.getTime();
 
 quickSort(a,0,N-1);
 
 d=new Date();
 long t1=d.getTime();
 System.out.println("Sorting time="+((t1-t0)/1000)+"sec");
  
	}
	
	public static void quickSort(int a[],int left, int right){
 int p=Pivot(a,left,right);
 quickSort(a,left,p-1);
 quickSort(a,p+1,right);
	}
	
	public static int Pivot(int a[],int left,int right){
 int i,j;
 i=left;
 j=right;
 int di=0;
 int dj=1;
 while(i<j){
 	if(a[i]>a[j]){
  int aux=a[i];
  	a[i]=a[j];
  	a[j]=aux;
  	
  	//aux=di;
  
  	aux=di;
  	di=dj;
  	dj=aux;
 	}
 i=i+di;
 j=j-dj;
 }
	return i;
	}
}


HELP please!!
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote