Seven Bubble sort for you: which do you choose?
#12
(03-13-2023, 12:37 AM)bplus Wrote:
Code: (Select All)
_Title "Only 1 pass to sort this out!"
d$ = "1,3,5,2,4,8,9,7,0,6,2,4,8,9,7,0,6,1,3,5,2,8,9,7,0,6,2"
Dim tracker(9) As Integer

p = InStr(d$, ",")
start = 1
While p
    index = Val(Mid$(d$, start, p - start))
    tracker(index) = tracker(index) + 1
    start = p + 1
    p = InStr(start, d$, ",")
Wend
index = Val(Mid$(d$, start)) ' last index

Print "sorted:"
For i = 0 To 9
    If tracker(i) Then
        For j = 1 To tracker(i)
            Print i;
        Next
    End If
Next

Aye.  As I've mentioned before, counting can often be the fastest means of sorting.  Only issue that comes up is if you spend more time counting than you would sorting.  For example, you DIMMED your data type as INTEGER -- *WHY* is the data here limited to 0 to 9?  

You have what here??  Almost 30 values in the data set?  If those values were from -32000 to +32000, then you'd have to change that FOR i loop to run 64000 times instead of 10.  Any decent sorting method should be able to run in fewer iterations than that.

There's no *always best* solution for sorting.  You really have to look at your needs, before deciding what  to go with, if ultimate performance is your goal.  Smile
Reply


Messages In This Thread
RE: Seven Bubble sort for you: which do you choose? - by SMcNeill - 03-13-2023, 02:12 AM



Users browsing this thread: 10 Guest(s)