Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

bubble sort

Posted on 2003-03-03
7
Medium Priority
?
423 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
  • 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
Industry Leaders: 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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

564 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