# Thread: [STUCK] Bubble sort algorithm on VB (console)

1. ## [STUCK] Bubble sort algorithm on VB (console)

I am trying to write a code for bubble sort. This is the code:
Code:
Dim Names(4) As String

Names(0) = 81
Names(1) = 3
Names(2) = 6
Names(3) = 77
Names(4) = 5

Dim temp As Integer

For n As Integer = 1 To Names.Length - 1

If Names(n - 1) > Names(n) Then
temp = Names(n)
Names(n) = Names(n - 1)
Names(n - 1) = temp

End If
Next n

For i = 0 To Names.Length - 1
Console.WriteLine(Names(i))
Next

The outcome is : 3,6,77,5,81. What if I tell you this code works perfectly fine up to 4 arrays. Like it will work fine if I use up to Names(3) but as soon as I add another array the code breaks. What is going on!? What is wrong with my code?

Thank you

2. ## Re: [STUCK] Bubble sort algorithm on VB (console)

Code:
FUNCTION bubblesort(list)

REPEAT
swapped = false
FOR n =1 TO LEN(list)-1
IF list[n-1] > list[n] THEN
temp = list[n-1]
list[n-1] = list[n]
list[n] = temp
swapped = true
END IF
NEXT n
UNTIL NOT swapped

RETURN list

END FUCTION
If you compare the pseudocode to your code, do you see anything missing from your code?

3. ## Re: [STUCK] Bubble sort algorithm on VB (console)

Also you should be careful when you are mixing value types. I recommend using Option Strict On to force you to assign proper value types to variables. Note that the below is not what is causing your sorting to fail, it is just a best practice.

Code:
Dim Names(4) As String
You are creating an array of Strings, but then are assigning numeric values to the elements:

Code:
Names(0) = 81
and your Temp variable is an Integer:

Code:
Dim temp As Integer
Your code appears to be designed to sort numeric values, so I would recommend changing your array to store Integers:

Code:
Dim Names(4) As Integer

4. ## Re: [STUCK] Bubble sort algorithm on VB (console)

OptionBase1. I have changed the variable to integer and added the swapped variable as well but still doesnt work:
Code:
Dim Names(4) As Integer

Names(0) = 81
Names(1) = 3
Names(2) = 67
Names(3) = 77
Names(4) = 84

Dim temp As Integer
Dim swapped As Boolean

For n As Integer = 1 To Names.Length - 1
swapped = False
Do Until swapped = True
If Names(n - 1) > Names(n) Then
temp = Names(n)
Names(n) = Names(n - 1)
Names(n - 1) = temp
swapped = True
End If
Loop
Next n

For i = 0 To Names.Length - 1
Console.WriteLine(Names(i))
Next

To be honest I am not sure how to use the swapped variable. That might be the reason my code is not working. If you know how to use it could you please also explain why is it necessary? Thank you

5. ## Re: [STUCK] Bubble sort algorithm on VB (console)

Please use the Code tag around code you post, since it preserves indentation.

For comparison, again, here is the pseudocode:

Code:
REPEAT
swapped = false
FOR n =1 TO LEN(list)-1
IF list[n-1] > list[n] THEN
temp = list[n-1]
list[n-1] = list[n]
list[n] = temp
swapped = true
END IF
NEXT n
UNTIL NOT swapped

Code:
For n As Integer = 1 To Names.Length - 1
swapped = False
Do Until swapped = True
If Names(n - 1) > Names(n) Then
temp = Names(n)
Names(n) = Names(n - 1)
Names(n - 1) = temp
swapped = True
End If
Loop
Next n
If you compare the two, you should hopefully be able to see the issue.

6. ## Re: [STUCK] Bubble sort algorithm on VB (console)

Ok... time for some paper debugging... I had to read the code twice to see why it doesn't work.... and when I show you how to walk it through it should be obvious to you too...

First... start a loop...
For n As Integer = 1 To Names.Length - 1

Then... set swapped to "False"
swapped = False

Now, start another loop that will exit when swapped is True
Do Until swapped = True

...but hold on swapped is false, so it never enters the loop...

So it skips all this...
If Names(n - 1) > Names(n) Then
temp = Names(n)
Names(n) = Names(n - 1)
Names(n - 1) = temp
swapped = True
End If

... and jumps straight to here
Loop

and repeats for the next n value
Next n

so the problem is that it never gets into the inner loop... so it never actually does the sorting. Ever.

You've got all the pieces... just in the wrong places...

what you have as your inner loop with the do until... should be outside your for loop... look at your pseudo code again... see how the for is inside the REPEAT? Repeat is like your Do Until ... although you should be checking the condition at the end of hte loop, not hte beginning.

-tg

7. ## Re: [STUCK] Bubble sort algorithm on VB (console)

Ok, so I have re-arranged the swapped variable outside the for loop. However still doesnt work. This is the update:
Code:
Sub Main()

Dim Names(5) As Integer

Names(0) = 81
Names(1) = 3
Names(2) = 67
Names(3) = 77
Names(4) = 84
Names(5) = 4

Dim temp As Integer
Dim swapped As Boolean

Do Until swapped = True
swapped = False

For n As Integer = 1 To Names.Length - 1

If Names(n - 1) > Names(n) Then
temp = Names(n)
Names(n) = Names(n - 1)
Names(n - 1) = temp
swapped = True
End If
Next n
Loop

For i = 0 To Names.Length - 1
Console.WriteLine(Names(i))
Next

End Sub

8. ## Re: [STUCK] Bubble sort algorithm on VB (console)

Code:
Do Until swapped = True
Pseudocode:

Code:
REPEAT
...
UNTIL NOT swapped
NOT swapped is equivalent to saying swapped = False

9. ## Re: [STUCK] Bubble sort algorithm on VB (console)

@tushardevi - Whenever you post something that contains code, please wrap the code in the proper BB tags: [CODE][/CODE]. Compare this:
Dim Names(4) As String

Names(0) = 81
Names(1) = 3
Names(2) = 6
Names(3) = 77
Names(4) = 5

Dim temp As Integer

For n As Integer = 1 To Names.Length - 1

If Names(n - 1) > Names(n) Then
temp = Names(n)
Names(n) = Names(n - 1)
Names(n - 1) = temp

End If
Next n

For i = 0 To Names.Length - 1
Console.WriteLine(Names(i))
Next

To this:
Code:
Dim Names(4) As String

Names(0) = 81
Names(1) = 3
Names(2) = 6
Names(3) = 77
Names(4) = 5

Dim temp As Integer

For n As Integer = 1 To Names.Length - 1

If Names(n - 1) > Names(n) Then
temp = Names(n)
Names(n) = Names(n - 1)
Names(n - 1) = temp

End If
Next n

For i = 0 To Names.Length - 1
Console.WriteLine(Names(i))
Next

10. ## Re: [STUCK] Bubble sort algorithm on VB (console)

If you go through all the values without having to swap the sort is done.
That means the sort is done when the swapped flag is "Not True", i.e. that it is "False".

You can test expressions to see if it is "False" or if it is "True".
You can also test to see if it is "Not False" or it is "Not True".
You can loop "Until" an expression is "True", or you can loop "While" an expression is "True".
You can test for the condition at the beginning, i.e. top of the loop, or you can test for the condition at the bottom of the loop.

Your pseudo code tests the value at the bottom of the loop, and it tests for the "Not True" condition.
Your code tests the value at the top of the loop, and it tests for the "True" condition.

You should see that you are doing the opposite of what your pseudo code suggests.
If you want to follow your pseudo code you would use a loop structure like:
Code:
Do
'.....
Loop Until Not Swapped
Rather than loop until Swapped is false, you could express that the opposite way which would be to loop While Swapped is True, and test that at the bottom, which is what I would do rather than test for "Not True"
Code:
Do
'.....
Loop While Swapped
p.s. If you preset the variable so you get into the loop, you could change your existing Until loop to a While loop and testing at the top would work, but that is a little extra work to preset the value before starting the loop.
Code:
'....
Dim swapped As Boolean = True

Do While swapped = True    'Continue looping as long as we've swapped a value in the sort
swapped = False
'...

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured