+ Reply to Thread
Results 1 to 7 of 7

quick sort code

Hybrid View

bceng quick sort code 08-29-2008, 12:16 PM
bceng Oh, I should mention that I... 08-29-2008, 12:25 PM
Leith Ross Hello bceng, Having 8... 08-29-2008, 12:28 PM
bceng The 8 levels of nesting isn't... 08-29-2008, 12:39 PM
shg Here's QuickSort: Sub... 08-29-2008, 12:45 PM
bceng Thanks shg I'll try that. 08-29-2008, 12:53 PM
bceng sort routine works great 09-25-2008, 04:55 PM
  1. #1
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Here's QuickSort:
    Sub DemoQS()
        Dim astr()  As String
    
        astr = Split("s,e,r,t,a,q,v,b,y,o,p", ",")
        QuickSort astr, 0, UBound(astr)
    End Sub
    
    Sub QuickSort(ByRef av As Variant, _
                  ByVal iFrst As Long, _
                  ByVal iLast As Long, _
                  Optional bAsc As Boolean = True)
        ' adapted from http://www.vba-programmer.com/Snippets/Code_VB/Quick_Sort_Single.html
    
        Dim iLo     As Long
        Dim iHi     As Long
        Dim Temp    As Variant
        Dim vSep    As Variant
    
        iLo = iFrst
        iHi = iLast
        vSep = av((iFrst + iLast) / 2)
    
        Do
            Do While IIf(bAsc, av(iLo) < vSep, av(iLo) > vSep)
                iLo = iLo + 1
            Loop
    
            Do While IIf(bAsc, av(iHi) > vSep, av(iHi) < vSep)
                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 iFrst < iHi Then QuickSort av, iFrst, iHi, bAsc
        If iLo < iLast Then QuickSort av, iLo, iLast, bAsc
    End Sub
    Last edited by shg; 08-29-2008 at 01:47 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA code to renames Tabs dependent on Cell name
    By Zyphon in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-28-2008, 05:44 AM
  2. VBA Match or Find code
    By djvice in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-20-2008, 08:03 PM
  3. List all the combinations of a group of cells containing letters, but not numbers
    By tanaka1986 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 02-12-2008, 06:38 AM
  4. converting table to list
    By Malte in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 12-25-2007, 05:56 PM
  5. Problems understanding automated emailing code.
    By DDONNI in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-19-2007, 12:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1