Partial Public Class _Default
Inherits System.Web.UI.Page
Private min As Integer
Private max As Integer
Public Shared Sub FastestSort(ByVal lbList() As Integer, ByVal min As Integer, ByVal max As Integer)
Dim random_number As New Random
Dim avg As Integer
Dim i As Integer
Dim low As Integer
Dim high As Integer
'if minimum is >= max, then there is nothing to sort
If min >= max Then Exit Sub
' randomize i to select a random number as a starter and use it as a divider
i = random_number.Next(min, max + 1)
avg = lbList(i)
' Swap it to the front.
lbList(i) = lbList(min)
low = min
high = max
Do
' Look down from hi for a value < med_value.
Do While lbList(high) >= avg
high = high - 1
If high <= low Then Exit Do
Loop
If high <= low Then
lbList(low) = avg
Exit Do
End If
' Swap the lo and hi values.
lbList(low) = lbList(high)
' increase the lowest number to look for the next one, while it's lower than average
' keep increasing till we got it
low = low + 1
Do While lbList(low) < avg
low = low + 1
If low >= high Then Exit Do
Loop
If low >= high Then
low = high
lbList(high) = avg
Exit Do
End If
' Swap the low and high values.
lbList(high) = lbList(low)
Loop
FastestSort(lbList, min, low - 1)
FastestSort(lbList, low + 1, max)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim lapTime As Integer
Dim lapNum As Integer
Integer.TryParse(txtLapNum.Text, lapNum)
Integer.TryParse(txtLapTime.Text, lapTime)
'each time we click, we add time to the list
lbList.Items.Add(lapTime)
End Sub
Protected Sub btnFastest_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFastest.Click
' pass the parameters
FastestSort(lbList, min, max)
End Sub
End Class