Results 1 to 3 of 3
 02122013, 03:24 AM #1Member
 Join Date
 Feb 2013
 Posts
 4
 Rep Power
 0
A recursion issue with quickSort algorithm
Hi. I implemented the QuickSort algorithm myself in a very innoccent and uneffiecient way,but i am doing this for learning and practicing. by the way,i know how this should by rightly implemented ,but i still want to try and make it my way which seems logic.
after debugging this,it all seems to work just fine, but the problem is that when i want to print the new ordered list (see the main method) it just prints me the original unsorted one. the partition function is coming back to where is started from because of the recursion. how can i make it "save" all the steps? and return the ordered list?
thx!
this is the code:
Java Code:public class MyQuickSort { public static int list[] = {6,5,3,1,8,7,2,4}; public static boolean sorted = false; public static int[] addNumberToArray(int arr[],int num){ int newArr[] = new int[arr.length+1]; for(int i = 0;i<arr.length;i++){ newArr[i] = arr[i]; } newArr[newArr.length1] = num; arr = newArr; return arr; } public static int[] partition(int arr[]){ while(!sorted){ int pivotIndex = (int)(arr.length/2); int left[] = new int[0]; int right[] = new int[0]; for(int i = 0;i<arr.length;i++){ if(i==pivotIndex){ continue; } else if(arr[i]<=arr[pivotIndex]){ left = addNumberToArray(left,arr[i]); } else{ right = addNumberToArray(right,arr[i]); } } int origPivot = arr[pivotIndex]; int k = 0; while(k<left.length){ arr[k] = left[k]; k++; } arr[k] = origPivot; k++; while(k<arr.length){ arr[k] = right[arr.lengtharr.length(left.length+1)+k]; k++; } if(left.length>1){ partition(left); } if(right.length>1){ partition(right); } if(left.length<=1&&right.length<=1){ sorted = true; } } return arr; } public static void main(String[] args) { list = partition(list); for(int i = 0;i<list.length;i++){ System.out.print(list[i]+" "); } } }
Last edited by JosAH; 02122013 at 07:47 AM. Reason: added [code] ... [/code] tags
 02122013, 03:28 AM #2Member
 Join Date
 Feb 2013
 Posts
 4
 Rep Power
 0
Re: A recursion issue with quickSort algorithm
and one more thing..why is my code look like this when i post? originally it is with Indentations
 02122013, 07:48 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Similar Threads

Algorithm's execution time evaluation issue
By EngineerVic in forum New To JavaReplies: 7Last Post: 12142012, 05:55 PM 
How to limit threads in a Java QuickSort algorithm.
By roise_r in forum Threads and SynchronizationReplies: 6Last Post: 06022011, 08:53 PM 
Boggled by this, Fast QuickSort Algorithm
By gcampton in forum New To JavaReplies: 0Last Post: 12082009, 04:07 PM 
quicksort program
By jvasilj1 in forum New To JavaReplies: 5Last Post: 03032009, 04:47 AM 
Quicksort
By little_polarbear in forum New To JavaReplies: 12Last Post: 07122008, 10:20 PM
Bookmarks