Except to learn Basic coding you should not waste time on Bubble sort!
As Steve has mentioned already when it comes to sorting on an integer key, you need only iterate through an array once!
Here is my mods of this idea:
OK maybe I am done with edits? sorry
As Steve has mentioned already when it comes to sorting on an integer key, you need only iterate through an array once!
Here is my mods of this idea:
Code: (Select All)
_Title "Sort 3 Integer array on first integer" ' b+ 2023-03-12
Dim As Integer MaxI
MaxI = 32767
Dim three(0 To MaxI, 1 To 3) As Integer
ReDim sort(0 To MaxI, 1 To 3) As Integer
'Init array
Randomize Timer
For i = 0 To MaxI
three(i, 1) = Int(Rnd * (MaxI))
three(i, 2) = Int(Rnd * (MaxI))
three(i, 3) = Int(Rnd * (MaxI))
Next
' show first and last 10
For i = 0 To 9
Print three(i, 1); three(i, 2); three(i, 3); Tab(40); three(MaxI - i, 1); three(MaxI - i, 2); three(MaxI - i, 3)
Next
Print
' begin processing sort
t# = Timer(.001)
sort3Ints three(), sort()
Print "'Sorted' in:"; Timer(.001) - t#; " secs.)"
For i = 0 To 9
Print sort(i, 1); sort(i, 2); sort(i, 3); Tab(40); sort(MaxI - i, 1); sort(MaxI - i, 2); sort(MaxI - i, 3)
Next
Sub sort3Ints (three() As Integer, sort() As Integer)
maxI = UBound(three)
bas = LBound(three)
Dim tracker$(0 To maxI)
Dim integ As Integer
For i = bas To maxI ' store the indexes that have a certain integer value under tracker$(of certain integer value)
integ = three(i, 1)
If tracker$(integ) = "" Then
tracker$(integ) = _Trim$(Str$(i))
Else
tracker$(integ) = tracker$(integ) + "," + _Trim$(Str$(i))
End If
Next
'after only 1 interation through array we have all we need to sort on first integer key!!!
For i = bas To maxI
If tracker$(i) <> "" Then 'get index and print
p = InStr(tracker$(i), ",")
start = 1
While p
index = Val(Mid$(tracker$(i), start, p - start))
sort(sortI, 1) = three(index, 1)
sort(sortI, 2) = three(index, 2)
sort(sortI, 3) = three(index, 3)
sortI = sortI + 1
start = p + 1
p = InStr(start, tracker$(i), ",")
Wend
index = Val(Mid$(tracker$(i), start)) ' last index
sort(sortI, 1) = three(index, 1)
sort(sortI, 2) = three(index, 2)
sort(sortI, 3) = three(index, 3)
sortI = sortI + 1
End If
Next
End Sub
OK maybe I am done with edits? sorry
b = b + ...