Do a Quicksort in descending order

Bagharmin

Board Regular
Joined
Nov 22, 2010
Messages
168
I used a version of Quicksort in my code which displays things in ascending order. The code is below:

Code:
Private Sub BinarySort(CategoryTypes() As Variant, ByVal iStart As Double, ByVal iEnd As Double)
Dim RunThru As Double, imTiny As Double, imHuge As Double
Dim Half As String, Temp As String
 
    imTiny = iStart
    imHuge = iEnd
    Half = CategoryTypes((imTiny + imHuge) / 2)
 
    Do While imTiny <= imHuge
 
        Do While (CategoryTypes(imTiny) < Half And imTiny < iEnd)
            imTiny = imTiny + 1
        Loop
 
        Do While (Half < CategoryTypes(imHuge) And imHuge > iStart)
            imHuge = imHuge - 1
        Loop
 
        If imTiny <= imHuge Then
            Temp = CategoryTypes(imTiny)
            CategoryTypes(imTiny) = CategoryTypes(imHuge)
            CategoryTypes(imHuge) = Temp
            imTiny = imTiny + 1
            imHuge = imHuge - 1
        End If
 
    Loop
 
    If iStart < imHuge Then Call BinarySort(CategoryTypes(), iStart, imHuge)
    If imTiny < iEnd Then Call BinarySort(CategoryTypes(), imTiny, iEnd)
 
End Sub

If I want to have it display in descending order instead, do I need to switch every inequality sign or do I only need to change a select few?

Thanks for the help.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I posted this late last Friday afternoon, so it may have gotten lost in the weekend shuffle.

Can anyone tell me how to alter the sort so that it sorts in descending order instead of ascending? Thanks.
 
Upvote 0
Here's a descending QuickSort:

Code:
Sub QuickSortDes(ByRef av As Variant, ByVal iBeg As Long, ByVal iEnd As Long)
    ' from [URL]http://www.vba-programmer.com/Snippets/Code_VB/Quick_Sort_Single.html[/URL]
    Dim iLo     As Long
    Dim iHi     As Long
    Dim Temp    As Variant
    Dim vSep    As Variant
 
    iLo = iBeg
    iHi = iEnd
    vSep = av((iBeg + iEnd) / 2)
 
    Do
        Do While av(iLo) > vSep ' descending
            iLo = iLo + 1
        Loop
 
        Do While av(iHi) < vSep ' descending
            iHi = iHi - 1
        Loop
 
        If iLo <= iHi Then
            Temp = av(iLo)
            av(iLo) = av(iHi)
            av(iHi) = Temp
            iLo = iLo + 1
            iHi = iHi - 1
        End If
    Loop While iLo <= iHi
 
    If iBeg < iHi Then QuickSortDes av, iBeg, iHi
    If iLo < iEnd Then QuickSortDes av, iLo, iEnd
End Sub
 
Upvote 0
Thanks. Based on that, it looks like the part of my sort that reads:

Code:
        Do While (CategoryTypes(imTiny) < Half And imTiny < iEnd)
            imTiny = imTiny + 1
        Loop
 
        Do While (Half < CategoryTypes(imHuge) And imHuge > iStart)
            imHuge = imHuge - 1
        Loop

...should be changed to:

Code:
        Do While (CategoryTypes(imTiny) [COLOR=red][B]>[/B][/COLOR] Half And imTiny [COLOR=red][B]>[/B][/COLOR] iEnd)
            imTiny = imTiny + 1
        Loop
 
        Do While (Half [COLOR=red][B]>[/B][/COLOR] CategoryTypes(imHuge) And imHuge [COLOR=red][B]<[/B][/COLOR] iStart)
            imHuge = imHuge - 1
        Loop

The rest of it all looks the same (as far as the inequalities go). If that's not correct, please let me know. Otherwise, thank you very much for the help.
 
Upvote 0

Forum statistics

Threads
1,224,509
Messages
6,179,192
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top