Results 1 to 1 of 1
Thread: Quick sort problem
- 02-18-2012, 04:21 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
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.
If anyone can point out where its wrong, that will be great. Thanks in advance.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); } } }Last edited by fishy8158; 02-18-2012 at 04:22 AM. Reason: fix some indent
Similar Threads
-
Implementing quick sort and binary search
By syle_q in forum New To JavaReplies: 1Last Post: 04-07-2011, 08:46 PM -
Need help with quick sort method
By Get_tanked in forum New To JavaReplies: 1Last Post: 03-14-2011, 09:44 PM -
Quick Sort explanation.
By hawaiifiver in forum New To JavaReplies: 4Last Post: 03-10-2009, 02:28 AM -
Quick sort with median-of-three partitioning
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:40 PM -
Simple version of quick sort
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks