473,770 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetU pperBound(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 3677

"Chris" <cw********@dsl extreme.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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.GetU pperBound(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********@dsl extreme.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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.GetU pperBound(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********@dsl extreme.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
10957
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.
1
2160
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 than the previous date Example using one Table (Access 2000):
34
7326
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
0
4440
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 "word", or "eelttr" for "letter". Build an array of all the keys, and sort it using a bubble sort. I have to write a modified version of bubble sort that maintains also the array of the primary words. For instance, if I had , our initial array of keys...
1
5661
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 integers. How do i get a bubble sort to put names in alphabetical order?
12
8646
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 multiple values. I need to make a bubble sort that reaches into an array called Tree which has in each storage location a class called Christmas Tree which have the values Species, Height, trunkDiameter, and costPerFoot. The sort will need to sort...
14
8063
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 program then should sort the names alphabetically by last name and display a list of all the students names and grades in alphabetical order. I have completed all my functions and classes, but i am getting a god aweful amount of errors and i am not...
7
7150
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 this array. please help me. my scenario: assume that we have a big 2d char array for example students for 20 persons an 30 character for each person. first 15 chars contains first name and the rest is last name. no i want to sort this array...
0
9617
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.