Quicksort

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This quicksort code:

Code:
https://wellsr.com/vba/2018/excel/vba-quicksort-macro-to-sort-arrays-fast/

has been adapted as follows:

Code:
Sub Quicksort(vArray As Variant, arrLbound As Long, arrUbound As Long)
'Sorts a one-dimensional VBA array from smallest to largest
'using a very fast quicksort algorithm variant.
Dim pivotVal As Variant
Dim vSwap    As Variant
Dim tmpLow   As Long
Dim tmpHi    As Long
 
tmpLow = arrLbound
tmpHi = arrUbound
pivotVal = vArray((arrLbound + arrUbound) \ 2, 1)
 
While (tmpLow <= tmpHi) 'divide
   While (vArray(tmpLow, 1) < pivotVal And tmpLow < arrUbound)
      tmpLow = tmpLow + 1
   Wend
  
   While (pivotVal < vArray(tmpHi, 1) And tmpHi > arrLbound)
      tmpHi = tmpHi - 1
   Wend
 
   If (tmpLow <= tmpHi) Then
      vSwap = vArray(tmpLow, 1)
      vArray(tmpLow, 1) = vArray(tmpHi, 1)
      vArray(tmpHi, 1) = vSwap
      tmpLow = tmpLow + 1
      tmpHi = tmpHi - 1
   End If
Wend
 
  If (arrLbound < tmpHi) Then Quicksort vArray, arrLbound, tmpHi 'conquer
  If (tmpLow < arrUbound) Then Quicksort vArray, tmpLow, arrUbound 'conquer
  
  Sheet3.Cells(1, 3).Resize(UBound(vArray, 1), 1).Value = vArray
End Sub

I use it like this:

Code:
Sub abc()
    
    Dim MyArray() As Variant
    
    MyArray() = Sheet3.Cells(1, 1).CurrentRegion.Value
    
    Call Quicksort(MyArray(), LBound(MyArray), UBound(MyArray))
    
End Sub

What I don't understand is when I break a breakpoint in Quicksort on this line:

Code:
Sheet3.Cells(1, 3).Resize(UBound(vArray, 1), 1).Value = vArray

after executing that line, it immedaitely jumps to this line above, ie:

Code:
If (tmpLow < arrUbound) Then Quicksort vArray, tmpLow, arrUbound 'conquer

I expected it t run to End Sub and then End.

Can someone please explain.

Thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The function uses recursion, so is calling itself repeatedly.
You should really be writing the array to the sheet in you abc macro & not in the function.
 
Upvote 0
The function uses recursion, so is calling itself repeatedly.
You should really be writing the array to the sheet in you abc macro & not in the function.
I understand that when the code hits these lines:

Code:
If (arrLbound < tmpHi) Then Quicksort vArray, arrLbound, tmpHi 'conquer
  If (tmpLow < arrUbound) Then Quicksort vArray, tmpLow, arrUbound 'conquer

there is a chance the code will return to the first line of Sub Quicksort but this line:

Code:
Sheet3.Cells(1, 3).Resize(UBound(vArray, 1), 1).Value = vArray

occurs after the two If lines, meaning those two If conditions are NOT met, so how can it still go back to the first line of the sub Quicksort?
 
Upvote 0
Because it's a recursive function and that's how they work.
The 1st time the function is called it may get to If (arrLbound < tmpHi) Then Quicksort vArray, arrLbound, tmpHi and call itself again, but that 1st call hasn't yet been completed, so when the 2nd call is finished the code will go back to the where it was called & complete the 1st call. If that makes sense.
 
Upvote 0
Because it's a recursive function and that's how they work.
The 1st time the function is called it may get to If (arrLbound < tmpHi) Then Quicksort vArray, arrLbound, tmpHi and call itself again, but that 1st call hasn't yet been completed, so when the 2nd call is finished the code will go back to the where it was called & complete the 1st call. If that makes sense.
Thanks for explaining.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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