- Home
·
- Math/Dates
·
- This Code includes many helpful SEARCHING functions and SORTING functions. (Bubble Sort, Insertion
This Code includes many helpful SEARCHING functions and SORTING functions. (Bubble Sort, Insertion
This Code includes many helpful SEARCHING functions and SORTING functions. (Bubble Sort, Insertion Sort, Selection Sort, Binary Search, Linear
Rate This Code includes many helpful SEARCHING functions and SORTING functions. (Bubble Sort, Insertion
(1(1 Vote))
'**************************************************************************
' Sorts intArray from low to high
'
' Pre: intArray() contains at least one element
' Post: intArray() elements are sorted low to high
'**************************************************************************
Sub BubbleSort(ByRef intArray() As Integer)
Dim intItem As Integer, intIndex As Integer
Dim blnSwapRequired As Boolean 'Flag
Dim intTemp As Integer
blnSwapRequired = True
Do While blnSwapRequired
blnSwapRequired = False
For intIndex = LBound(intArray) To UBound(intArray)
If intArray(intIndex) > intArray(intIndex + 1) Then
intTemp = intArray(intIndex)
intArray(intIndex) = intArray(intIndex + 1)
intArray(intIndex + 1) = intTemp
blnSwapRequired = True
End If
Next intIndex
Loop
End Sub
'**************************************************************************
' Sorts intArray from low to high
'
' Pre: intArray() contains at least one element
' Post: intArray() elements are sorted low to high
'**************************************************************************
Sub SelectionSort(ByRef intArray() As Integer)
Dim intIndex As Integer
Dim intLowItemIndex As Integer, intTemp As Integer
For intIndex = LBound(intArray) To UBound(intArray)
intLowItemIndex = FindLowest(intArray, intIndex, UBound(intArray))
intTemp = intArray(intIndex)
intArray(intIndex) = intArray(intLowItemIndex)
intArray(intLowItemIndex) = intTemp
Next intIndex
End Sub
'**************************************************************************
' Returns the index of the lowest item in elements intLow
' to intHigh of intArray()
'
' Pre: intArray() has at least one element
' Post: Index of the lowest item in range intLow to intHigh
' returned
'**************************************************************************
Function FindLowest(ByRef intArray() As Integer, ByVal intLow As Integer, _
ByVal intHigh As Integer) As Integer
Dim intIndex As Integer, intLowSoFar As Integer
' Make first element the Lowest
intLowSoFar = intLow
For intIndex = intLow To intHigh
If intArray(intIndex) < intArray(intLowSoFar) Then
intLowSoFar = intIndex
End If
Next intIndex
FindLowest = intLowSoFar
End Function
'**************************************************************************
' Sorts intArray from low to high
'
' Pre: intArray() has at least one element
' Post: intArray() elements are sorted low to high
'**************************************************************************
Sub InsertionSort(ByRef intArray() As Integer)
Dim intItem As Integer, intPreviousItem As Integer
Dim intTemp As Integer
For intItem = LBound(intArray) + 1 To UBound(intArray)
intTemp = intArray(intItem)
intPreviousItem = intItem - 1
Do While intPreviousItem > LBound(intArray) _
And intArray(intPreviousItem) > intTemp
intArray(intPreviousItem + 1) = intArray(intPreviousItem)
intPreviousItem = intPreviousItem - 1
Loop
If intArray(intPreviousItem) > intTemp Then
intArray(intPreviousItem + 1) = intArray(intPreviousItem)
intArray(intPreviousItem) = intTemp
Else
intArray(intPreviousItem + 1) = intTemp
End If
Next intItem
End Sub
'SEARCHING PROCEDURES
'**************************************************************************
' Returns the index of intNumToFind if found or a -1 if not found
'
' Pre: intArray() contains at least one element
' Post: index of intNumToFind is returned. -1 if not found
'**************************************************************************
Function BinarySearch(ByRef intArray() As Integer, _
ByVal intNumToFind As Integer) As Integer
Dim intHighIndex As Integer, intMidIndex As Integer, _
intLowIndex As Integer, blnFound As Boolean
intHighIndex = UBound(intArray)
intLowIndex = LBound(intArray)
blnFound = False
Do While Not blnFound And intLowIndex <= intHighIndex
intMidIndex = Int((intHighIndex + intLowIndex) / 2)
If intArray(intMidIndex) = intNumToFind Then
blnFound = True
ElseIf intArray(intMidIndex) > intNumToFind Then
intHighIndex = intMidIndex - 1
Else
intLowIndex = intMidIndex + 1
End If
Loop
If blnFound Then
BinarySearch = intMidIndex
Else
BinarySearch = -1
End If
End Function
'**************************************************************************
' Returns the index of the first occurrence of intNumToFind in
' intArray() or -1 if intNumToFind not found
'
' Pre: intArray() contains at least one element
' Post: index of intNumToFind is returned. -1 if not found
'**************************************************************************
Function LinearSearch(ByRef intArray() As Integer, _
ByVal intNumToFind As Integer) As Integer
Dim intIndex As Integer
intIndex = LBound(intArray)
Do While (intArray(intIndex) <> intNumToFind) _
And (intIndex < UBound(intArray))
intIndex = intIndex + 1
Loop
If intArray(intIndex) - intNumToFind Then
LinearSearch = intIndex 'Item Found
Else
LinearSearch = -1 'Item Not Found
End If
End Function
This Code includes many helpful SEARCHING functions and SORTING functions. (Bubble Sort, Insertion Comments
No comments yet — be the first to post one!
Post a Comment