# A recursion issue with quickSort algorithm

• 02-12-2013, 02:24 AM
patishi
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:
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.length-1] = num;                 arr = newArr;                 return arr;                         }                         public static int[] partition(int arr[]){                                 while(!sorted){                         int pivotIndex = (int)(arr.length/2);                         int left[] = new int;                         int right[] = new int;                                         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.length-arr.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]+" ");                 }                 } }```
• 02-12-2013, 02:28 AM
patishi
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
• 02-12-2013, 06:48 AM
JosAH
Re: A recursion issue with quickSort algorithm
Quote:

and one more thing..why is my code look like this when i post? originally it is with Indentations

You have to put your code between [code] ... [/code] tags; I did it for you this time.

kind regards,

Jos