Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

========================================================================================
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 aLike 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

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 commandSub swap(A, B)
Dim temp As Integer
temp = A
A = B
B = temp
End SubPrivate 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(

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 commandSub swap(A, B)
Dim temp As Integer
temp = A
A = B
B = temp
End SubPrivate 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 SubForgive 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

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

![]() |
![]() |
![]() |

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