Hi, I'm just starting out in programming and I'm currently making a small program which shows how Bubble, selection and Insertion sorts work. I've found a small program which bubble sorts a user selected number of integers and shows each step at which these are sorted (each pass). However this program doesn't sort the way a bubble sort should work (eg the largest element should be at the last index on the first pass).

I'm in the middle of altering the source code, this is what it was originally:

Private Sub Command1_Click()
    For x = 1 To UBound(myArr) - 1
        For y = (x + 1) To UBound(myArr)
            If myArr(x) >= myArr(y) Then
                doSwap x, y
            End If
        Next y
    Next x
End Sub

you can see that if the first element isn't bigger then the second it will increment only y so if the first elemnt is bigger then the third the first and third will switch positions (like a selection sort).
I want the code to look something like this:

Private Sub cmdSort_Click()
For x = 1 To UBound(Sarray) - 1
        For y = (x + 1) To UBound(Sarray)
            If Sarray(x) >= Sarray(y) Then
                doSwap x, y
            Else
            increment y
            increment x
            End If
End Sub

so that if the first element is smaller then the second, the first element stays in current position and then the second elemnt is compared to the third. Bust since I'm only new to programming i have no idea what to type.

Help is appreciated. Thank you.

Recommended Answers

All 12 Replies

Hi,

Use This Code:

Dim i As Integer
    Dim J As Integer
    Dim k
    '
    For i = 1 To UBound(SArray)
        For J = i To 1 Step -1
            If Val(SArray(J)) < Val(SArray(J - 1)) Then
                k = SArray(J - 1)
                SArray(J - 1) = TArr(J)
                SArray(J) = k
            Else
                Exit For
            End If
        Next
    Next

Regards
Veena

k = SArray(J - 1)
                SArray(J - 1) = TArr(J)
                SArray(J) = k

This just swaps the values right? because i've already got a function 'doSwap' which does it for me.

Else
                Exit For
            End If
        Next
    Next

After the 'else' why does it exit the 'for'? because i want it to increment the two variables if the "IF' statement isn't satisfied.

Why can't I put this in?

For x = 1 To UBound(Sarray) - 1
        For y = (x + 1) To UBound(Sarray)
            If Sarray(x) >= Sarray(y) Then
                doSwap x, y
            Else
                 Exit For
         Next y
Next x
            End If
         Next y
Next x

I get a compile error stating 'Next without For' on the first Next.

For x = 1 To UBound(Sarray) - 1
        For y = (x + 1) To UBound(Sarray)
            If Sarray(x) >= Sarray(y) Then
                doSwap x, y
            Else
                 Exit For
         Next y
Next x
            End If
         Next y
Next x

I think that there is really a compiler error here.. you see your ebd if,, it is in a wrong place...

try replacing your code above with this one...
hope it helps

For x = 1 To UBound(Sarray) - 1
        For y = (x + 1) To UBound(Sarray) 
              If Sarray(x) >= Sarray(y) Then
                   doSwap x, y
              End If
        Next
Next

note that I don't use any else statement since if you want to increment just the second ( variable y) as variable x remains in its position then all you have to do is to increment 'y' and after the last element in the array is being compared then that's the only time the variable y increment..


Regards

Cruize²

Hi Cruize,

I've used your code in the program and here is the output (4 array values used).
11 2 5 9
2 11 5 9 ------pass 1
2 5 11 9 ------pass 2
2 5 9 11 ------pass 3
This isn't the expected output, as a bubblesort should bring the largest value to the right of pass 1 (which is 11). It's only sorting by one position at a time, what it should look like is this:
11 2 5 9
2 5 9 11 ----pass 1
2 5 9 11 ----pass 2
the 11 is compared to the 2, since it is greater it is swapped (temp), then it is compared to 5 swapped (temp), then 9 swapped and stored in the last index. After the first pass the x value is incremented as well as y value, so 5 is compared to 9, since it is not greater it doesn't swap and should increment again to x=9 and y=11.

So what it's doing should be shown as one pass, ie the final result of your code should be displayed as the first pass. It's a little difficult with those numbers. Heres another example with 3 values (i've added x and y to show you what i mean):
6 2 0
2 6 0
0 6 2
0 2 6
It should look like this
6 2 0
x y
2 6 0
x y
2 0 6 ---- pass 1
x y
0 2 6
x y
0 2 6 -----pass 2

I got confused, well this is my understanding, correct me if I am wrong,

there should be an indicator to tell if the array is sorted in ascending.. and if not the sorting should continue after each passes...

dim check as integer
check = -1
 
while check=-1
    For x = 1 To UBound(Sarray) - 1
        y = x + 1;
        If Sarray(x) >= Sarray(y) Then
            doSwap x, y
        End If
    Next 
'end of the pass
'this would check the array each passes
    for x = 1 to UBound(Sarray) -1
        if sarray(x) > sarray(x+1)
            check=-1
            exit for
        else
            check=0
        end if
    next
wend

I'd like to explain this how, but try it and you'll understand...

Hope this answers your question... sorry for the late post,, I got busy this past few days.. and by the way, I think you can still minimize my code.

Hi,

I've run through the code and done a deskcheck on 4 values and it is correct.
I've made the following chnages to the code as there was 2 lines that wernt accepted.

Dim check As Integer
check = -1
 
While check = -1
    For x = 1 To UBound(Sarray) - 1
        y = x + 1; 'THIS line wasnt recognised change to y=(x+1) and nothing would work when i click my sort command button
        If Sarray(x) >= Sarray(y) Then
            doSwap x, y
        End If
    Next
'end of the pass
'this would check the array each passes
    For x = 1 To UBound(Sarray) - 1
        If Sarray(x) > Sarray(y) Then 'ADDED THEN and still wouldnt dort so changed x+1 to y and it still didn't work:(
            check = -1
            Exit For
        Else
            check = 0
        End If
    Next
Wend

I'm really sorry for the trouble.. I misused the syntax for I have a project on C++ in creating a game. Sorry for the trouble.. and thanks for reminding me. I GOT LOST.... THIS IS VB..

int the first error, there is simple a semicolon in the end of the code... you see.. that shoudn't be there...

about the second mistake I have... I am wondering, wouldn't there should be y=x+1 before the if statement... since that if you're going to compare the first element and the next...

I've deskchecked and its correct, except it just doesn't seem to work when i click the sort button, perhaps something to do with my doSwap or DoShow functions?
ie i get no errors when i click the sort button, just nothing is displayed.


EDIT: ok i got it to sort, but it still doesn't do it correctly, could it be where my doShow function is? because it occurrs within DoSwap.

Hi Pasta,

Why not try my code..?

REgards
Veena

uhm,, maybe in the doshow code, I don't know your doshow code.. but as QVeen72 said, have you tried her code??

Private Sub doShow()
    DoEvents
    For cnt = 1 To UBound(Sarray)
        txtSwap.Text = txtSwap.Text & Sarray(cnt) & " "
    Next cnt
    txtSwap.Text = txtSwap.Text & vbCrLf
    txtSwap.SelStart = Len(txtSwap.Text)
    txtSwap.SelLength = 0
End Sub

This is executed at the end of the DoSwap function.

Problem solved!
I just had to simply move the do show function to the 'end of the pass, should have clicked earlier, largest number is at the end of array in first pass thanks alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.