I am creating a Rhinoscript to sort surfaces based on proximity along a curve. I implemented a Bubble sort, but it is too slow. I have ~16,000 surfaces, pre-sorted into 42 “rows” of ~400. The Bubble sort takes about 2 minutes per 400 surfaces, so it would take about 84 minutes to run the script. I’d like to bring that down, thus my desire to implement a Merge or Quick sort. If anybody would be willing to help me implement a better sort algorithm, I greatly appreciate it. My script looks like this so far:

```
Sub SortSrfAlongCrv()
Dim arrSrf : arrSrf = Rhino.GetObjects("Select panels to sort", 8)
If IsNull(arrSrf) Then Exit Sub
Dim strCrv : strCrv = Rhino.GetObject("Select control curve", 4)
If IsNull(strCrv) Then Exit Sub
Call Rhino.Print(Time)
Call Rhino.EnableRedraw(False)
'BUBBLE SORT
Dim n : n = UBound(arrSrf) + 1
Dim newn
Dim swapped: swapped = False
Dim srf1, srf2, arrCen1, arrCen2, param1, param2
Dim temp
Dim j
Do
newn = 0
For j = 1 To UBound(arrSrf)
arrCen1 = Rhino.SurfaceAreaCentroid(arrSrf(j - 1))
arrCen2 = Rhino.SurfaceAreaCentroid(arrSrf(j))
param1 = Rhino.CurveClosestPoint(strCrv, arrCen1(0))
param2 = Rhino.CurveClosestPoint(strCrv, arrCen2(0))
'If this pair is out of order
If (param1 > param2) Then
'Swap them and remember something changed
temp = arrSrf(j)
arrSrf(j) = arrSrf(j - 1)
arrSrf(j - 1) = temp
newn = j
End If
Next
n = newn
Loop While n <> 0
'Give each surface a name based on it's material and index value
Dim i, arrCen
For i = 0 To UBound(arrSrf)
Call Rhino.ObjectName(arrSrf(i), id)
arrCen = Rhino.SurfaceAreaCentroid(arrSrf(i))
Call Rhino.AddTextDot(CStr(i), arrCen(0))
Next
Call Rhino.EnableRedraw(True)
Call Rhino.Print(Time)
End Sub
```