Search code examples
arraysvb.netloopsbubble-sort

Swaping positions within an array vb.net


I'm trying to code a program that has two sections depending on which of two buttons has been pressed.

The first section is the bit that is working, the user presses the first button labeled "unsort", this triggers a loop which displays an input box asking for a random number 8 times. These 8 numbers are stored in an array.

However it is the second section I'm struggling with; the second button is labeled sort and should output the numbers the user just entered using the first button is order, smallest to largest. I understand that a bubble sort must be used here and that a loop within a loop must also be used however it is the content of these loop that I don't understand. Since my original post I've edited the post to include some code in the loop I was previously stuck with, however it still isn't producing the desired output (all numbers in order) but is instead just outputting the numbers in a seemingly random order

The code is posted below with annotations:

Public Class BubbleSort1
    Dim Bubble(8) As Integer
    Dim UnsortedList As String
    Dim n As Integer
    Dim SortedList As String
    Dim temp As String


    Private Sub btnUnsort_Click(sender As Object, e As EventArgs) Handles btnUnsort.Click
        n = 8 ' number off values on array
        For i = 1 To n ' when i is between 1 and size of array
            Bubble(i) = InputBox("Enter Number") ' User inputs a number
            UnsortedList = UnsortedList & " " & Bubble(i) & vbNewLine ' number is added to the unsorted list variable
        Next i
        lblUnsort.Text = UnsortedList ' outputs the array
    End Sub

    Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click

        For i = 1 To n - 1 ' When i is between 1 and the array size - 1 (8-1):
            For j = 1 To n - 1 ' Second loop - when j is between 1 and the array size - 1 (8-1):
                If Bubble(j) > Bubble(j + 1) Then ' if bubble value j is greater than value j - 1:
                    temp = Bubble(j)
                    Bubble(j) = Bubble(j + 1) ' These lines are supost to order the numbers but aren'r currently doing so
                    Bubble(j + 1) = temp
                    SortedList = SortedList & Bubble(j) & vbNewLine ' Adding the number in order to a variable 
                End If
            Next j
        Next i

        lblSort.Text = SortedList ' outputting the ordered numbers

    End Sub
End Class

As is pointed out in the code, the section of this code that orders the numbers is just putting them in a random order rather than actually ordering them.


Solution

  • With your updated code which now includes the swapping of array elements, you are building the string which shows the sorted array too soon: it will show the workings rather than the final result.

    All you need to do is build the string once the array is in order:

    Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
    
        ' Bubble sort the array...
        For i = 1 To n - 1 ' When i is between 1 and the array size - 1 (8-1):
            For j = 1 To n - 1 ' Second loop - when j is between 1 and the array size - 1 (8-1):
                If Bubble(j) > Bubble(j + 1) Then ' if bubble value j is greater than value j - 1:
                    temp = Bubble(j)
                    Bubble(j) = Bubble(j + 1)
                    Bubble(j + 1) = temp
                End If
            Next j
        Next i
    
        'lblSort.Text = String.Join(vbNewLine, Bubble.Skip(1)) ' an easy one-liner
    
        ' Create a string to show the sorted array...
        SortedList = "" ' clear it out in case it was used previously
        For i = 1 To n
            SortedList = SortedList & Bubble(i).ToString()
            If i < n Then ' only add a newline if it isn't the last element
                SortedList = SortedList & vbNewLine
            End If
        Next
    
        lblSort.Text = SortedList
    
    End Sub
    

    I put the .ToString() in there in anticipation of you explicitly converting the input strings into numbers; strictly speaking, the & operator will convert its arguments into strings but I prefer to make it obvious in the code.


    As your code is, there is an implicit conversion from the input (a string of digits) into an integer (the type of the array elements). While this seems convenient, it can be a problem if VB guesses the wrong conversion for you. There is a way to tell it to let you know if the types of variables don't match: put Option Strict On as the very first line and it will even give you suggestions on what needs to be done to put it right.