Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quicksort Algorithm

Status
Not open for further replies.

WildRyan

Programmer
Oct 15, 2003
2
US
If I understand this algorithm correctly it will sort an array (A) with p being a pointer to the first element, and r being a pointer to the last element in descending order.

1.Quicksort(A,p,r)
2. if p < r then
3. q = Partition(A,p,r)
4. Quicksort(A,p,q)
5. Quicksort(A,q+1,r)
6.Partition(A,p,r)
7.x = A[p]
8.i = p-1
9.j = r+1
10.while TRUE
11. do repeat j = j – 1
12. until A[j] >= x
13. repeat i = i + 1
14. until A <= x
15. if i < j then
16. exchange A with A[j]
17. else
18. return j


Will this work properly? I changed a generic version of quicksort by reversing the >= and the <= on lines 12,14.
 
Maybe I am in the wrong forum. I am new to tek-tips, could anyone point me in the right direction?
 
Hi Ryan,
If I'm correct in assuming that you are simply trying to sort the array in reverse order, then you should be good to go with the changes you've made.
I think you've got a possible typo in lines 14 and 16. The A should be A.

As with any algorithm, I find that the best confirmation is typing up a small program to give it a whirl.

-Steve

PS: Seems like a good place to post this.
 
The typo that Levis refers to is a result of TGML. In order to prevent the tags from being read, wrap any (pseudo)code in
Code:
tags. These work like HTML tags, except that they use the [] instead of <>. A more detailed description is given in the link just above &quot;Submit Post&quot; on the posting form.

Ben
A programmer was drowning. Lots of people watched but did nothing. They couldn't understand why he yelled &quot;F1!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top