none
Using BubbleSort to sort 100 random numbers RRS feed

  • Question

  • Hello everyone, 

    I have the basic bubblesort code and I'm just confused on how to display it in a RichTextBox. Here's my code so far:

     Dim I As Integer
        Dim intNum(99) As Integer
        Dim List As Array
        Dim tmp As Long
        Dim Max As Integer = 100
        Dim min As Integer = 0
        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            'Exit Button
            Application.Exit()
    
        End Sub
    
        Private Sub btnNewNumbers_Click(sender As Object, e As EventArgs) Handles btnNewNumbers.Click
            'Randomizing 100 Numbers
            Randomize()
            For I = 0 To 99
                intNum(I) = Int(Rnd() * 100) + 1
            Next
            rtxtNumbers.Text = ""
            'Visualizing Numbers
            For I = 0 To 99
                rtxtNumbers.Text += vbTab + Str(intNum(I))
            Next
    
            btnSort.Enabled = True
    
        End Sub
    
        Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
            For Loop1 = min To Max
                For Loop2 = 0 To Max - Loop1
                    If (Loop2) > (Loop2 + 1) Then
                        'switch
                        tmp = List(Loop2)
                        List(Loop2) = List(Loop2 + 1)
                        List(Loop2 + 1) = tmp
                    End If
                Next
            Next
    
            rtxtSortedNumbers.Text = Str()
    
        End Sub
    
        Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
            rtxtNumbers.Text = ""
            rtxtSortedNumbers.Text = ""
            btnSort.Enabled = True
        End Sub
    
        Private Sub NumberSorterBySophia_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Disables Sort button until New Numbers is clicked
            btnSort.Enabled = False
    
        End Sub
    End Class

    Any and all help would be appreciated!

    Friday, November 17, 2017 4:54 PM

All replies

  • Hi

    Are you posting under two names?

    Or maybe you go to the same classes as another poster here who asked the same question showing the same code!


    Regards Les, Livingston, Scotland

    Friday, November 17, 2017 5:15 PM
  • You have a loop in btnNewNumbers_Click for showing the unsorted values; does that work? If so then use the same thing for the sorted values except with the relevant names.

    For the benefit of others, see Number Sort: How to bubble sort?; it seems to be from a class mate with the same problem.



    Sam Hobbs
    SimpleSamples.Info

    Friday, November 17, 2017 7:21 PM
  • Hi sophsqndpiper,

    I have tried your code and find you have created 100 number in richtextbox, now you have some problem to sort these number and how to display these number in richtextbox. Am I right?

    If yes, you can take a look the following code to sort number:

     Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
            BubbleSort(intNum)
            For I = 0 To 99
                rtxtSortedNumbers.Text += Environment.NewLine + Str(intNum(I))
            Next
        End Sub
    
        Private Sub BubbleSort(ByVal sortArray As Integer())
            Dim pass, i As Integer
            For pass = 1 To sortArray.GetUpperBound(0)
                For i = 0 To sortArray.GetUpperBound(0) - 1
                    If sortArray(i) > sortArray(i + 1) Then
                        Swap(sortArray, i)
                    End If
                Next
            Next
    
        End Sub ' BubbleSort
    
        Private Sub Swap(ByVal swapArray As Integer(), ByVal first As Integer)
            Dim hold As Integer
            hold = swapArray(first)
            swapArray(first) = swapArray(first + 1)
            swapArray(first + 1) = hold
        End Sub ' Swap

    You can modify your code according to my code above.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Monday, November 20, 2017 6:32 AM
    Monday, November 20, 2017 6:15 AM
    Moderator
  • Hi I found a solution to your issue using this code; Hope it works

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim I As Integer
        Dim intnum(99) As Integer
        Private Sub btnGetNumbers_Click(sender As Object, e As EventArgs) Handles btnGetNumbers.Click
            Randomize()
            For I = 0 To 99
                intnum(I) = CInt(Rnd() * 100) + 1
            Next
            rtxtOriginal.Text = Nothing
            For I = 0 To 99
                rtxtOriginal.Text += vbTab + Str(intnum(I))
            Next
        End Sub
        Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
            Dim First As Integer, Last As Integer
            Dim Temp As Integer
            First = LBound(intnum)
            Last = UBound(intnum)
            For i As Integer = First To Last - 1
                For j As Integer = i + 1 To Last
                    If intnum(i) > intnum(j) Then
                        Temp = intnum(j)
                        intnum(j) = intnum(i)
                        intnum(i) = Temp
                    End If
                Next j
            Next i
    
            rtxtSorted.Text = Nothing
            For I = 0 To 99
                rtxtSorted.Text += vbTab + Str(intnum(I))
            Next
        End Sub
    End Class

    Tuesday, December 12, 2017 2:04 PM
  • Food for thought.  This contains several LINQ methods to generate the random numbers, only one is not commented

    Public Class Form1
        'two buttons, one richtextbox
    
        Private Shared prng As New Random
        Private numbers() As Integer
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            '-----------------------------------------------------------------
            numbers = Enumerable.Range(1, 100).OrderBy(Function(z) prng.Next).ToArray '100 unique numbers between 1 and 100
            'numbers = Enumerable.Range(1001, 20).OrderBy(Function(z) prng.Next).ToArray '20 unique numbers starting at 1001
    
            '-----------------------------------------------------------------
            'different approach
            'create x random numbers, the Enumerable.Range defines how many
            'the numbders created are defined by the prng.Next
            'numbers = Enumerable.Range(1, 100).Select(Function(z) prng.Next(1001, 5001)).ToArray '100 non-unique numbers between 1001 and 5000
            'numbers = Enumerable.Range(1, 20).Select(Function(z) prng.Next(10, 301)).ToArray '20 non-unique numbers between 10 and 300
    
    
            ''debug
            'numbers(numbers.Length - 1) = 0
            'numbers(0) = Integer.MaxValue
    
            DispNums()
            Button2.Select()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            'bubble sort and display
            If numbers Is Nothing OrElse numbers.Length = 0 Then Exit Sub
            Dim swap As Boolean = False
            Dim temp As Integer
    
            For pass As Integer = numbers.Length - 1 To 0 Step -1
                swap = False
                For idx As Integer = 0 To pass - 1
                    If numbers(idx) > numbers(idx + 1) Then
                        swap = True 'an exchange was made
                        temp = numbers(idx)
                        numbers(idx) = numbers(idx + 1)
                        numbers(idx + 1) = temp
                    End If
                Next
                If Not swap Then
                    Exit For
                End If
            Next
            DispNums()
            Button1.Select()
        End Sub
    
        Private Sub DispNums()
            RichTextBox1.Clear()
            RichTextBox1.Lines = numbers.Select(Function(x) x.ToString("n0")).ToArray
        End Sub
    End Class
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Tuesday, December 12, 2017 3:09 PM
  • A Bubble sort is typical a school assignment. 

    That is why I always recognize the Microsoft MSFT answers. They remember that from previous year. 

    However, it is better that you find the solution yourself. There is endless times written around it. 


    Success
    Cor


    Tuesday, December 12, 2017 5:29 PM