Consulting

Results 1 to 2 of 2

Thread: Syntax To QuickSort A Range Of Numbers In An Array

  1. #1

    Syntax To QuickSort A Range Of Numbers In An Array

    Hi,


    I'm trying to figure out the syntax to sort a range of numbers using QuickSort, but cant find anything online.


    Basically I want to sort the array from 10 to 20 - something like Call QuickSort(Array1, 1, (Array1),10 to 20, 1), but cant figure out the syntax.


    Thanks.,

    I tried to attach the excel file, but the attachment option doesnt seem to work, let me know if you require the excel sheet & ill upload it to google.


    Sub x1()
     
     
        Dim Destination As Range
        
        Dim Array1 As Variant
        Dim Array2() As Variant
        
        Array1 = Range("a1:a20").Value
        
          
        Call QuickSort(Array1, 1, UBound(Array1), 1)  '<---  1 for column(index of 2nd dimension) reference
     
        
     
     
    Range("d1:d20") = Array1
     'p = LBound(Array1, 1)
     
    'Range("e1") = Array1(p, 1)
    
    
     
     End Sub
    
    
    
    
    
    
    
    
    
    
    
    
    'Quick Sort Function
    
    
    Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long, ref As Long)
      Dim pivot   As Variant
      Dim tmpSwap As Variant
      Dim tmpLow  As Long
      Dim tmpHi   As Long, ii As Long
    
    
      tmpLow = inLow
      tmpHi = inHi
    
    
      pivot = vArray((inLow + inHi) \ 2, ref)
    
    
      While (tmpLow <= tmpHi)
         While (vArray(tmpLow, ref) < pivot And tmpLow < inHi)
            tmpLow = tmpLow + 1
         Wend
    
    
         While (pivot < vArray(tmpHi, ref) And tmpHi > inLow)
            tmpHi = tmpHi - 1
         Wend
    
    
         If (tmpLow <= tmpHi) Then
            For ii = LBound(vArray, 2) To UBound(vArray, 2)
                tmpSwap = vArray(tmpLow, ii)
                vArray(tmpLow, ii) = vArray(tmpHi, ii)
                vArray(tmpHi, ii) = tmpSwap
            Next
            tmpLow = tmpLow + 1
            tmpHi = tmpHi - 1
         End If
      Wend
    
    
      If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi, ref
      If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi, ref
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Capture.JPG

    Seems to work OK for me. What was the issue?

    Option Explicit
    
    
    Sub x1()
        Dim Array1 As Variant
        
        Array1 = Worksheets("Sheet1").Range("a1:a20").Value
        
        Call QuickSort(Array1, 1, UBound(Array1), 1)  '<---  1 for column(index of 2nd dimension) reference
    
    
        Worksheets("Sheet1").Range("d1:d20") = Array1
        
     End Sub
    
    
    'Quick Sort Function
    Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long, ref As Long)
      Dim pivot   As Variant
      Dim tmpSwap As Variant
      Dim tmpLow  As Long
      Dim tmpHi   As Long, ii As Long
    
    
      tmpLow = inLow
      tmpHi = inHi
    
    
      pivot = vArray((inLow + inHi) \ 2, ref)
    
    
      While (tmpLow <= tmpHi)
         While (vArray(tmpLow, ref) < pivot And tmpLow < inHi)
            tmpLow = tmpLow + 1
         Wend
    
    
    
    
         While (pivot < vArray(tmpHi, ref) And tmpHi > inLow)
            tmpHi = tmpHi - 1
         Wend
    
    
    
    
         If (tmpLow <= tmpHi) Then
            For ii = LBound(vArray, 2) To UBound(vArray, 2)
                tmpSwap = vArray(tmpLow, ii)
                vArray(tmpLow, ii) = vArray(tmpHi, ii)
                vArray(tmpHi, ii) = tmpSwap
            Next
            tmpLow = tmpLow + 1
            tmpHi = tmpHi - 1
         End If
      Wend
    
    
    
    
      If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi, ref
      If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi, ref
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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