GenusMax
09-07-2020, 01:43 PM
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
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