Computing.Net > Forums > Programming > Bubble sort help!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Bubble sort help!

Reply to Message Icon

Name: bertyzz
Date: March 15, 2003 at 04:08:32 Pacific
OS: XP
CPU/Ram: 1.1
Comment:

Hi Guys,
I have created a small program that consists of an arry that holds a number of values that are entered by a user. However, I need to sort and then dispaly these numbers in assending order.
I am quite sure that I need to use a 'bubble sort' to do this but I have no idea on how to code it.

Could somebody please help me?

Thanks very much,

Bertyzz



Sponsored Link
Ads by Google

Response Number 1
Name: bertyzz
Date: March 15, 2003 at 04:27:12 Pacific
Reply:

Sorry, Im using VB6!


0

Response Number 2
Name: borelli34
Date: March 15, 2003 at 09:30:20 Pacific
Reply:


========================================================================================
You may need to modify this slightly for your use but the algorithm is in the code:

dim myarray[100]
rem this array can be whatever size
dim min,max,swp
rem below here you assign whatever values to your array
min=1
max=100
.
.
.


for a=min to max
for b=a to max
if myarray[b][less than or equal to]myarray[a] then goto skipit
swp=myarray[a]
myarray[a]=myarray[b]
myarray[b]=swp
:skipit
next b
next a

Like I said, this may need an adjustment or two but it should show you the method involved. This is about as basic an algorithm I can give not having the actual code to work with. The goto skipit is not really the recommended method either but it does the job. I hope this helps.

borelli34


0

Response Number 3
Name: elric
Date: March 16, 2003 at 06:07:34 Pacific
Reply:

G'day,

This programme works (there are many and varied versions of this algorithm...)

Dim bubbleArray(10) As Integer
Dim i As Integer
Dim j As Integer
'VB6 doesn't seem to support the QBASIC SWAP command

Sub swap(A, B)
Dim temp As Integer
temp = A
A = B
B = temp
End Sub

Private Sub cmdBubble_Click()
'Set up data
Dim x As Integer
Dim bub As String
Dim bubr As String
'Generate some numbers
Randomize
For x = 1 To 10
bubbleArray(x) = Int((Rnd * 50) + 1)
bubr = bubr + " " + Str$(bubbleArray(x))
Next x
txtdatain.Text = bubr
'Bubble Sort Algorithm
For i = 2 To 10
For j = 10 To 1 Step -1
If bubbleArray(


0

Response Number 4
Name: elric
Date: March 16, 2003 at 06:07:36 Pacific
Reply:

G'day,

This programme works (there are many and varied versions of this algorithm...)

Dim bubbleArray(10) As Integer
Dim i As Integer
Dim j As Integer
'VB6 doesn't seem to support the QBASIC SWAP command

Sub swap(A, B)
Dim temp As Integer
temp = A
A = B
B = temp
End Sub

Private Sub cmdBubble_Click()
'Set up data
Dim x As Integer
Dim bub As String
Dim bubr As String
'Generate some numbers
Randomize
For x = 1 To 10
bubbleArray(x) = Int((Rnd * 50) + 1)
bubr = bubr + " " + Str$(bubbleArray(x))
Next x
txtdatain.Text = bubr
'Bubble Sort Algorithm
For i = 2 To 10
For j = 10 To 1 Step -1
If bubbleArray(j - 1) ] bubbleArray(j) Then
Call swap(bubbleArray(j - 1), bubbleArray(j))
End If
Next j
Next i
For x = 1 To 10
bub = bub + " " + Str$(bubbleArray(x))
Next x
txtDataout.Text = bub
End Sub

Forgive my tendency to QBasic string manipulation, but this is only my second day at VB anything (actually it's the free download of VB5.0 CCE from Microsoft..)so I haven't quite got the hang of string input and output.
Anyway, it works with VB5.0; you just need one command button and two text boxes (one for the input sequence and one for the output). I have limited the numbers to 10 (using RND to produce 10 numbers between 1 and 50), but you can change this to your array size.
regards,
Elric


0

Response Number 5
Name: elric
Date: March 16, 2003 at 06:17:41 Pacific
Reply:

G'day,

I hope the "second" post makes some sense to you. It would appear that the very hint of a >>>>>>> sign sends this editor into a bit of a wobbler...
regards,
Elric


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Bubble sort help!

Sorting within a batch file www.computing.net/answers/programming/sorting-within-a-batch-file/15059.html

Sorting Combo box property www.computing.net/answers/programming/sorting-combo-box-property/3973.html

Help with C program www.computing.net/answers/programming/help-with-c-program/6914.html