Indexed Bubble Sort
We have discussed the concepts of parallel arrays and the bubble sort. In this section we will combine features of the two concepts to present an indexed bubble sort. The idea behind the indexed bubble sort is to maintain a parallel array of indexes or positions in the primary array, and when it is necessary to obtain a sorted representation of the primary array, the index array is sorted, leaving the primary array in its original order. The sorted order can then be obtained by referencing array_name(index(element))
This approach would have been useful in the text analyzer program. The WordList and Frequency arrays were parallel arrays that first had to be listed in alphabetical order, and then in frequency of appearance order, and then searched for keywords. The steps in the program were rearranged to perform the binary search for keywords while the array was still in alphabetical order because once it was sorted by frequency the search would no longer work without again sorting the word list alphabetically. This could have been avoided by maintaining one or more indexes on the arrays: an alphabetical index and a frequency index.
==================
Example:
Private Sub Command1_Click( )
Const max = 5
Dim i As Integer, length As Integer
Dim index(1 To max) As Integer
Dim x(1 To max) As Integer' Fill array with random values for testing
For i = 1 To UBound(x)
x(i) = Int(Rnd( ) * 100)
index(i) = i
Next i
length = UBound(x)
Call indexBubbleSort(x, index, length) 'x & index start at 1
' Print the x array, the index array, and the x(index) array to
' verify that x is in its original order while x(index) is sorted.
For i = 1 To length
List1.AddItem x(i)
List2.AddItem index(i)
List3.AddItem x(index(i))
Next i
End Sub
'*********************************************************
'* Indexed version of bubble sort
'* Assume both x and index start at 1
'********************************************************
Sub indexBubbleSort(x( ) As Integer, index( ) As Integer, _
ByVal length as Integer)
Dim i As Integer, j As Integer, temp As Integer
For i = 1 To length - 1
For j = 1 To length - i
If x(index(j)) > x(index(j + 1)) Then 'look at x thru index
temp = index(j)
index(j) = index(j + 1)
index(j + 1) = temp
End If
Next j
Next i
End Sub




