I'm using bubble sort to sort a small array, and i'm having problems. I think i've just done too much and am missing a small mistake so i'd like to see if anyone notices. I can sort Z - A by changing to <. But when I try to sort A - Z it just returns the array blank.
Here's the subroutine for sorting, maybe i'm over looking something, but this is my attempt to sort A - Z.
Code:
Public Sub BubbleSort2(ByRef NameArray As Variant)
Dim iCount, i, j As Integer
Dim strTemp As String
iCount = UBound(NameArray, 0)
For j = 1 To iCount - 2
For i = 1 To iCount - 2
If NameArray(i) > NameArray(i + 1) Then
strTemp = NameArray(i + 1)
NameArray(i + 1) = NameArray(i)
NameArray(i) = strTemp
End If
Next i
Next j
End Sub
Any help would be lovely.
Thanks,
What language? Is this VB.NET?
Assuming a base 1 for your nameArray, you'll want to do 1 TO "len(array) - 1". Assuming 0, you'll want 0 TO len(array) - 2.
That's some pretty obfuscated code you got there. Here's mine:
Code:
PUBLIC SUB BubbleSort2(ByRef nameArray As Variant)
DIM lenA, i, j AS INTEGER
DIM strTemp AS STRING
lenA = UBOUND(nameArray, 0) - 1
FOR j = 1 TO lenA
FOR i = 1 TO lenA
IF nameArray(i) > nameArray(i + 1) THEN
strTemp = nameArray(i)
nameArray(i) = nameArray(i + 1)
nameArray(i + 1) = strTemp
END IF
NEXT i, j
END SUB
:lol:
Aga: it's just Visual Basic...
Sumo:
Next time please define an array as parameter like this: Array()
.
Let's see....:
Code:
Public Sub BubbleSort2(NameArray() As String)
Dim iCount As Integer, i As Integer, j As Integer
Dim strTemp As String, lCount As Integer
iCount = UBound(NameArray)
lCount = LBound(NameArray)
For j = lCount To iCount
For i = lCount To iCount - 1
If NameArray(i) > NameArray(i + 1) Then
strTemp = NameArray(i + 1)
NameArray(i + 1) = NameArray(i)
NameArray(i) = strTemp
'I don't know sure, but perhaps:
'Swap NameArray(i), NameArray(i + 1)
'also works, I can't remember if VB had support for Swap
End If
Next i
Next j
End Sub
Hope it works (didn't test)
Neo yours didn't work. Like I said, it was late, and i wasn't thinking too much. But I got it now. Of course, I didn't really fix the problem, I just decided to work around it. I set it up to sort Z-A then ran the loop for the array backwards. This should have been the most obvious answer to me last night, but it didn't come up till this morning.
Thanks everyone
Didn't work??!?!?!?!?! :???: Hrmmm. Strange... :roll:
You see, aga, lowlie users such as I don't
understand quicksort logic.
Quote:More optimization.
Technically, that's not an optimization, as you provided a different algo
Quote:More optimization.
Code:
SUB qsort.integer.lowstart (array1() AS INTEGER, a.max%)
DIM g2(1 TO a.max%) AS INTEGER, h2(1 TO a.max%) AS INTEGER, i AS INTEGER, j AS INTEGER, r AS INTEGER, E AS INTEGER, g AS INTEGER, h AS INTEGER, k AS INTEGER
E = 1: g2(1) = 1: h2(1) = a.max%
f1: g = g2(E): h = h2(E)
f2: i = g: j = h: r = (g + h) \ 2: k = array1(r)
f3: IF array1(i) < k THEN i = i + 1: GOTO f3
f4: IF array1(j) > k THEN j = j - 1: GOTO f4
IF i <= j THEN SWAP array1(i), array1(j): i = i + 1: j = j - 1: IF i <= j THEN GOTO f3
IF j - g + i < h THEN
IF i < h THEN g2(E) = i: h2(E) = h: E = E + 1
h = j
ELSE
IF g < j THEN g2(E) = g: h2(E) = j: E = E + 1
g = i
END IF
IF g < h THEN GOTO f2 ELSE E = E - 1: IF E THEN GOTO f1
ERASE g2, h2
END SUB
Now if i changed those integers to strings, would it sort a-z?