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 | | | | re: Misbehaving Bubble Sort
"Chris" <cwhite2812@dslextreme.com> wrote in message
news:1132699124.923874.62130@z14g2000cwz.googlegro ups.com...[color=blue]
>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
>[/color]
Turn on Option Strict. The error will pop out at you.
David | | | | re: Misbehaving Bubble Sort
Hi,
"Chris" <cwhite2812@dslextreme.com> wrote in message
news:1132699124.923874.62130@z14g2000cwz.googlegro ups.com...[color=blue]
>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:[/color]
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
[color=blue]
> 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
>[/color] | | | | re: Misbehaving Bubble Sort
"Chris" <cwhite2812@dslextreme.com> schrieb[color=blue]
> 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:
> [...][/color]
Enable Option Strict.
Armin | | | | re: Misbehaving Bubble Sort
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 |  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|