Results 1 to 1 of 1
  1. #1
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Quick sort problem

    the professor wrote a quick sort algorithm on the board and just told us to copy and read it at home. How ever it didn't run and i tried my best to learn it online and fix the code but it still doesn't seem to run, or im misunderstanding quick sort.
    Java Code:
    public class QuickSort{
    	public QuickSort(int[] anArray){
    		a=anArray;
    	}
    public void sort(int from,int to){
    	if(from>=to) return;
    	int p=partition(from,to);
    	sort(from,to);
    	sort(p+1,to);
    	}
    private int partition(int from,int to){
    	int pivot=a[from];
    	int j=to+1;
    	int i=from;
    	while(i<j){
    		i++;
    		while(a[i]<pivot)i++;
    			j--;
    			while(a[j]>pivot)j--;
    			if(i<j)
    				swap(i,j);
    		}
    		return j;
    	}
    	private void swap(int i,int j){
    		int temp=a[i];
    		a[i]=a[j];
    		a[j]=temp;
    	}
    private int[] a;
    }
    
    public class QuickSortTester {
    	public static void main(String args[]){
    		ArrayUtil au=new ArrayUtil();
    		int[] a=au.randomIntArray(20, 100);
    		QuickSort qs=new QuickSort(a);
    		qs.sort(0,19); //2 random numbers i put, cuz i don't understand it.
    		au.print(a);
    		}
    	}
    }
    If anyone can point out where its wrong, that will be great. Thanks in advance.
    Last edited by fishy8158; 02-18-2012 at 04:22 AM. Reason: fix some indent

Similar Threads

  1. Implementing quick sort and binary search
    By syle_q in forum New To Java
    Replies: 1
    Last Post: 04-07-2011, 08:46 PM
  2. Need help with quick sort method
    By Get_tanked in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 09:44 PM
  3. Quick Sort explanation.
    By hawaiifiver in forum New To Java
    Replies: 4
    Last Post: 03-10-2009, 02:28 AM
  4. Quick sort with median-of-three partitioning
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:40 PM
  5. Simple version of quick sort
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •