# Quick Sort explanation.

• 03-08-2009, 10:29 PM
hawaiifiver
Quick Sort explanation.
Hi guys. I am trying to understand the following code. This isnt an assignment, I am just trying to understand this program. It implements the quick sort method. But I find the code slightly confusing. Could you help me understand it a bit better? Some things I am not clear on:

1. Why do they have that return statement in the quickSort() method
2. What are those two inner while loops doing?
3. What is the code in the brackets doing. quickSort(a, lo == low ? lo+1 : lo, n);

Thanks.
Here is the code:

Code:

```class Quick {         public static void main(String[] args)         {         int [] a = {3,4,1,2,5, 9, 6};         int i;                 quickSort(a, 0, a.length - 1);         for(i = 0; i <a.length; i++)         {System.out.println(a[i]);}         }//main         public static void quickSort(int a[], int low, int n)         {         int lo = low;             int hi = n;             if (lo >= n) {                               return;                             }         int mid = a[(lo + hi)/2];         while (lo < hi)         {         while (lo < hi && a[lo] < mid)         {                 lo ++;                 }//end inner while         while (lo < hi && a[hi] > mid)         {                 hi --;         }//end inner while         if (lo < hi)         {                 int temp = a[lo];                 a[lo] = a[hi];                 a[hi] = temp;         }//end if         }//end outer while         if (hi < lo) {                               int temp = hi;                               hi = lo;                               lo = temp;                         }                     quickSort(a, low, lo);                     quickSort(a, lo == low ? lo+1 : lo, n);                         }//end method         }//class```
• 03-09-2009, 04:01 AM
Eranga
Quote:

Originally Posted by hawaiifiver
1. Why do they have that return statement in the quickSort() method

return statement use simply to exit the code. In this case is used to exit from the method.

Quote:

Originally Posted by hawaiifiver
2. What are those two inner while loops doing?

Better to take a piece of paper and write the logic on it and see.

Quote:

Originally Posted by hawaiifiver
3. What is the code in the brackets doing. quickSort(a, lo == low ? lo+1 : lo, n);

This is about ternary operator. Much similar to the if-else condition.

Code:

`lo == low ? lo+1 : lo`
You can right the same logic using if-else as follows.

Code:

```if(lo == low) {     [I]select [B]lo + 1[/B][/I] } else {     [I]select [B]lo[/B][/I] }```
• 03-09-2009, 05:36 PM
hawaiifiver
ah cool. i thought those inner while loops were searching the array.
• 03-09-2009, 06:47 PM
emceenugget
a better thing to do would be search out pseudocode for quick sort (i'd imagine wikipedia would have some) and then compare them. while it can be a good trait to be able to read code and decipher what it's doing, a more descriptive version might be more useful to you.

funny, i tried googling and everything is rejected as 'looking similar to automated requests from a virus'.
• 03-10-2009, 03:28 AM
Eranga
Yes, best way to read codes is, looking at the pseudo code or the logic of it rather working on each line of code straightly.