435,131 Members | 1,494 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

Misbehaving Bubble Sort

 P: n/a 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 Replies

 P: n/a "Chris" 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

 P: n/a Hi, "Chris" 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

 P: n/a "Chris" 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

 P: n/a 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. 