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 4 3639
"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
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gram |
last post by:
Hello,
Can anyone help out with a bubble sort for strings using ASP? I have a text
file of words id like to sort and count.
Thanks in advance for any help.
Gram.
|
by: u473 |
last post by:
Running Totals by date misbehaving
I applied to the letter the sample code given
on http://support.microsoft.com/?kbid=290136
but it seems the running total breaks when the day number
is less...
|
by: Mark Kamoski |
last post by:
Hi--
Please help.
I need a code sample for bubble sort.
Thank you.
--Mark
|
by: Slowjam |
last post by:
How do I correct the program below to search all the anagrams in a given file.
First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
|
by: guest |
last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for...
|
by: midknight5 |
last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
|
by: xtheendx |
last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
|
by: mahdiahmadirad |
last post by:
Hi dears!
I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |