Results 1 to 5 of 5
Thread: Quick Sort explanation.
- 03-08-2009, 09:29 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
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:
Java 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, 03:01 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
return statement use simply to exit the code. In this case is used to exit from the method.
Better to take a piece of paper and write the logic on it and see.
This is about ternary operator. Much similar to the if-else condition.
You can right the same logic using if-else as follows.Java Code:lo == low ? lo+1 : lo
Java Code:if(lo == low) { [I]select [B]lo + 1[/B][/I] } else { [I]select [B]lo[/B][/I] }
- 03-09-2009, 04:36 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
ah cool. i thought those inner while loops were searching the array.
- 03-09-2009, 05:47 PM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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, 02:28 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
Similar Threads
-
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
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 -
Class explanation
By mcal in forum New To JavaReplies: 1Last Post: 02-05-2008, 06:50 PM -
need a little explanation
By cew27 in forum New To JavaReplies: 7Last Post: 12-13-2007, 11:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks