?
Solved

bubble sort

Posted on 2003-03-03
7
Medium Priority
?
421 Views
Last Modified: 2010-04-07
I am just practicing with visual basic and I am working on a bubble sort program.  However, I have a problem.  There is nothing wrong when I try and display and generate random numbers, but when I want to sort it it alwyas says Subscript out of range, and I don't know how to sort out the problem.

Dim Num(0 To 9) As Integer  'Decalring the variables
Dim Temp As Integer
Dim Flag As Boolean

Private Sub CmdReset_Click()

    For listnum = 0 To 9    'Clears the textbox and labels
       
        Lbl1(listnum).Caption = ""
       
        Lbl2(listnum).Caption = ""
   
    Next listnum

End Sub

Private Sub CmdRndNum_Click()

Randomize   'Initialize random-number generator

    For listnum = 0 To 9
   
        Num(listnum) = CInt(Int((1000 * Rnd()) + 1)) 'Generate random values between 0 and 1000
       
        Lbl1(listnum).Caption = Num(listnum)    'Insert the generated value into the label
       
        Lbl2(listnum).Caption = Num(listnum)   'Insert the generated value into the textbox
   
    Next listnum

End Sub

Private Sub CmdSort_Click()
   
    For SortNum = 0 To 9
        For listnum = 0 To 9
   
            If Num(listnum) > Num(listnum + 1) Then 'If the first value is bigger than the second value
               
                Flag = True
               
                Temp = Num(listnum)     'place the value as temp
       
                Num(listnum) = Num(listnum + 1) 'Swap places
       
                Num(listnum + 1) = Temp     'Make the second value temp
           
            End If
   
    Lbl2(listnum).Caption = Num(listnum)       'Rearrange the values
   
        Next listnum
    Next SortNum
   
End Sub


Is there something wrong with my code?

Thanks!
0
Comment
Question by:ccminted
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
Jacamar earned 320 total points
ID: 8058482
Private Sub CmdSort_Click()
   
   For SortNum = 0 To 8
       For listnum = 0 To 8
   
           If Num(listnum) > Num(listnum + 1) Then 'If the first value is bigger than the second value
               
               Flag = True
               
               Temp = Num(listnum)     'place the value as temp
       
               Num(listnum) = Num(listnum + 1) 'Swap places
       
               Num(listnum + 1) = Temp     'Make the second value temp
           
           End If
   
   Lbl2(listnum).Caption = Num(listnum)       'Rearrange the values
   
       Next listnum
   Next SortNum
   
End Sub
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8058495
the values should only go to 8
since your statement is
if Num(listnum)>Num(listnum+1) then........

Before, when you had your values going to 9, it got to

if Num(9)>Num(9+1) then........


and Num(10) is out of your range.  

To use the bubble sort, you only need to swap the list n-1 times since when n-1 numbers are sorted, the last is automatically sorted.

I hope this helps.

Jacamar
0
 
LVL 4

Expert Comment

by:AdsB
ID: 8058508
Your loops need changing.  Try changing the eights to nines. e.g.

For SortNum = 0 To 8
  For listnum = 0 To 8

There are also other places you could improve your code, but this should answer the question you have asked.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ccminted
ID: 8058568
I changed it to listnum = 0 to 8 and sortnum = 0 to 8, but what happens now is that the last lbl box wont change and will still display the old number, where the biggest number should be in that lbl box. why is that?

where else have I gone wrong?

Thanks.
0
 

Author Comment

by:ccminted
ID: 8058575
Apart from that, where else can I improve on the code?

Thanks.
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8058595
take the lbl2() second part and put in to a new loop

For listnum = 0 to 9
Lbl2(listnum).Caption = Num(listnum)
Next listnum

put this at the end of your code
0
 
LVL 4

Expert Comment

by:AdsB
ID: 8061678
Here is another improvement:

You have the line:-

  Flag = True

but you never use this value in your code.  What you should do is replace the outer "For" loop with a Do...Until loop.  The idea of a bubble sort is that you keep "bubbling" until no swaps were made.  The Flag is supposed to record the fact that at least one swap was made.  The code should be...

Do
  Flag = False
  For listnum = 0 To 8
 
    If Num(listnum) > Num(listnum + 1) Then
      'do a swap
      Flag = True              
      Temp = Num(listnum)
      Num(listnum) = Num(listnum + 1)
      Num(listnum + 1) = Temp
    End If
  Next listnum
Loop Until Flag = False

Consider the case where you have to sort a list that is already in order...  Your present code does all the work anyway.  The code I have shown here does one quick pass and since thre are no swaps it immediately finishes.

The strange thing is that you had the flag but weren't using it... now, why would that be?



A second improvement is possible if you realise that the the inner loop doent always have to go from 0 To 8.  In fact, after the first pass you can be certain that the 9th element will be in the right place, as the swapping will have put it there.  So the looping you really need, in order to minimoze work is as follows:-

 0-8
 0-7
 0-6
 ... and so on.

So the code will now look like


Dim Limit as Integer

Limit = 8
Do
  Flag = False
  For listnum = 0 To Limit
    If Num(listnum) > Num(listnum + 1) Then
      'do a swap
      Flag = True              
      Temp = Num(listnum)
      Num(listnum) = Num(listnum + 1)
      Num(listnum + 1) = Temp
    End If
  Next listnum
  Limit = Limit - 1
Loop Until Flag = False


There are other ways to improve a bubble sort,  but that will do for now I guess ;-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question