[STUCK] Bubble sort algorithm on VB (console)-VBForums
Results 1 to 10 of 10

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

  1. #1

    Thread Starter
    New Member tushardevi's Avatar
    Join Date
    Dec 2017
    Location
    Wales
    Posts
    12

    [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
    
            Console.ReadLine()

    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
    Last edited by dday9; Jan 31st, 2018 at 05:11 PM. Reason: Code Tags

  2. #2
    Addicted Member
    Join Date
    Nov 2017
    Posts
    202

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

    In your other thread on bubble sorts, you posted this pseudocode:

    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. #3
    Addicted Member
    Join Date
    Nov 2017
    Posts
    202

    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. #4

    Thread Starter
    New Member tushardevi's Avatar
    Join Date
    Dec 2017
    Location
    Wales
    Posts
    12

    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
    
            Console.ReadLine()

    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
    Last edited by dday9; Jan 31st, 2018 at 05:12 PM. Reason: Code Tags

  5. #5
    Addicted Member
    Join Date
    Nov 2017
    Posts
    202

    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
    and your code with indentation:

    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. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,463

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    New Member tushardevi's Avatar
    Join Date
    Dec 2017
    Location
    Wales
    Posts
    12

    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
    
            Console.ReadLine()
    
    
        End Sub
    Last edited by dday9; Jan 31st, 2018 at 05:28 PM. Reason: Code Tags

  8. #8
    Addicted Member
    Join Date
    Nov 2017
    Posts
    202

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

    Your code:

    Code:
    Do Until swapped = True
    Pseudocode:

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

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,259

    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

    Console.ReadLine()

    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
    
            Console.ReadLine()
    You can either type it manually or you can click on the # sign in both the quick reply's and advance reply's toolbar.

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,175

    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
    '...
    Last edited by passel; Jan 31st, 2018 at 06:50 PM.

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


Click Here to Expand Forum to Full Width