Quicksort

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,404
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
 

Some videos you may like

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,741
Office Version
  1. 365
Platform
  1. Windows
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.
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,404
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?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,741
Office Version
  1. 365
Platform
  1. Windows
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.
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,404
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,741
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,128,057
Messages
5,628,359
Members
416,314
Latest member
Dan99321

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
Top