Site hosted by Angelfire.com: Build your free website today!

'===================================================================================================

' SelectionSort Algorithm for VBScript (Numeric Sorting)

'

' Note: ByRef can be replaced with ByVal. If this is the case, then uncomment out the last line of

'       the function.

'===================================================================================================

Function SelectionSort(ByRef numArray)

    Dim min

    Dim temp

    Dim index

    Dim scan

 

    For index = 0 To UBound(numArray) - 1

        min = index

        For scan = index + 1 To UBound(numArray)

            If numArray(scan) < numArray(min) Then

                min = scan

            End If

        Next

        temp = numArray(min)

        numArray(min) = numArray(index)

        numArray(index) = temp

    Next

    'SelectionSort = numArray

End Function

'===================================================================================================

 

 

 

 

'===================================================================================================

' QuickSort Algorithm for VBScript (Numeric Sorting)

'

' Note: The low argument must be LBound(array()) and the high argument must be UBound(array()). The

'       ByRef keyword in front of array() means the argument being passed into the function is

'       similar to a c++ pointer. The reference is needed since the function doesn't return

'       anything.

'===================================================================================================

Function QuickSort(ByRef array(), ByVal low, ByVal high)

    If low < high Then

        Dim pivot_index : pivot_index = Partition(array, low, high)

        QuickSort array, low, pivot_index - 1

        QuickSort array, pivot_index + 1, high

    End If

End Function

 

 

 

Function Partition(ByRef array(), ByVal low, ByVal high)

    Dim pivot : pivot = low - 1

    Dim x : x = array(high)

 

    While low < high

        If array(low) <= x Then

            pivot = pivot + 1

            Dim tmp : tmp = array(pivot)

            array(pivot) = array(low)

            array(low) = tmp

        End If

        low = low + 1

    Wend

 

    Dim temp : temp = array(pivot + 1)

    array(pivot + 1) = array(high)

    array(high) = temp

 

    Partition = pivot + 1

End Function

'===================================================================================================