468,771 Members | 1,889 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

Misbehaving Bubble Sort

I have a bubble sort for a 2-dimensional array that sorts a
string,number pair based on the number. The code for the sort is as
follows:

Private Sub SortArray(ByRef roundarray(,) As String)
Dim i, j, x As Integer
x = roundarray.GetUpperBound(0)
Dim tempname, tempnumber As String
For i = 0 To x - 1
For j = i + 1 To x
If CInt(roundarray(i, 1) < CInt(roundarray(i + 1, 1)))
Then
tempname = roundarray(i, 0)
tempnumber = roundarray(i, 1)
roundarray(i, 0) = roundarray(i + 1, 0)
roundarray(i, 1) = roundarray(i + 1, 1)
roundarray(i + 1, 0) = tempname
roundarray(i + 1, 1) = tempnumber
End If
Next j
Next i
End Sub

My problem is that when I call the sort the first time, it misses 1
sort so if the list it had to sort was:
Bob 10
Ed 3
Zeek 11

it returns
Bob 10
Zeek 11
Ed 3.

Regardless of the length of the list, it always puts the highest value
1 from the top instead of at the top. If I re-apply the sort, however,
it is put in the right order. I have a feeling it has something to do
with one of my loop limits, but I've tried increasing them by 1 without
any effect. I know the bubble sort is not terribly effective, but it
works for what I need to do right now.

Many thanks for any suggestions,
Chris

Nov 23 '05 #1
4 3410

"Chris" <cw********@dslextreme.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I have a bubble sort for a 2-dimensional array that sorts a
string,number pair based on the number. The code for the sort is as
follows:

Private Sub SortArray(ByRef roundarray(,) As String)
Dim i, j, x As Integer
x = roundarray.GetUpperBound(0)
Dim tempname, tempnumber As String
For i = 0 To x - 1
For j = i + 1 To x
If CInt(roundarray(i, 1) < CInt(roundarray(i + 1, 1)))
Then
tempname = roundarray(i, 0)
tempnumber = roundarray(i, 1)
roundarray(i, 0) = roundarray(i + 1, 0)
roundarray(i, 1) = roundarray(i + 1, 1)
roundarray(i + 1, 0) = tempname
roundarray(i + 1, 1) = tempnumber
End If
Next j
Next i
End Sub


Turn on Option Strict. The error will pop out at you.

David
Nov 23 '05 #2
Hi,

"Chris" <cw********@dslextreme.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I have a bubble sort for a 2-dimensional array that sorts a
string,number pair based on the number. The code for the sort is as
follows:
Looking at other buble sort implementations it looks like you have a problem
with the inner loop. Expecially because you're not using the inner loop
variable, and so during the inner loop you keep comparing the same values.

And like David suggests, using option strict reveals some missplaced ( ) at
comparison.

Try:

Private Sub SortArray( roundarray(,) As String )
Dim i, j, x As Integer
x = roundarray.GetUpperBound(0)
Dim tempname, tempnumber As String
For i = 0 To x - 1
For j = 0 To x - i - 1
If CInt( roundarray(j, 1) ) < CInt( roundarray(j + 1, 1) ) Then
tempname = roundarray(j, 0)
tempnumber = roundarray(j, 1)
roundarray(j, 0) = roundarray(j + 1, 0)
roundarray(j, 1) = roundarray(j + 1, 1)
roundarray(j + 1, 0) = tempname
roundarray(j + 1, 1) = tempnumber
End If
Next j
Next i
End Sub

You also don't gain anything by passing array byref, so i removed it.

HTH,
Greetings

My problem is that when I call the sort the first time, it misses 1
sort so if the list it had to sort was:
Bob 10
Ed 3
Zeek 11

it returns
Bob 10
Zeek 11
Ed 3.

Regardless of the length of the list, it always puts the highest value
1 from the top instead of at the top. If I re-apply the sort, however,
it is put in the right order. I have a feeling it has something to do
with one of my loop limits, but I've tried increasing them by 1 without
any effect. I know the bubble sort is not terribly effective, but it
works for what I need to do right now.

Many thanks for any suggestions,
Chris

Nov 23 '05 #3
"Chris" <cw********@dslextreme.com> schrieb
I have a bubble sort for a 2-dimensional array that sorts a
string,number pair based on the number. The code for the sort is as
follows:
[...]

Enable Option Strict.
Armin

Nov 23 '05 #4
I enabled Option Strict and corrected a few cast errors that were
elsewhere in the program, including the one in the loop. Your code
worked perfectly. Thanks very much. I went back to one of my vb.net
textbooks and found the proper structure for the bubble sort. It's the
first time I've been back to the language in about a year and a half so
I'm relearning some things and moving from VS 2003 and VS 2005.

Many many thanks,
Chris

Nov 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Gram | last post: by
34 posts views Thread by Mark Kamoski | last post: by
reply views Thread by Slowjam | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.