Results 1 to 36 of 36

Thread: Hello! Id like some help with my bubblesort code

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Hello! Id like some help with my bubblesort code

    So basically my bubblesort isnt working no matter what i do it doesnt work. Basically im trying to make an application where the user uploads a text file that includes numbers and it bubblesorts them everything works fine but not bubblesorting would anyone help me and tell me whats wrong? and can someone too explain in simplified terms since im new Name:  Screenshot 2024-04-23 020352.jpg
Views: 154
Size:  27.7 KB

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    Attachment 191264Attachment 191264 it is basically like this

  3. #3
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,544

    Re: Hello! Id like some help with my bubblesort code

    So when you say it isn't working, what exactly is happening? Is it not sorting at all? Sorting in an incorrect way? Hanging?

    You are much more likely to get help when you provide a useful description of the problem.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    Private Sub BubbleSort(ByRef x As List(of String))

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    Also x is a List(Of String), which you’re trying to sort as a List(Of Integer).
    You need to explicitly convert.

    Code:
    Dim i, a As Integer
    Dim t As String
    For 1 = 0 To x. Count - 1
        For a = i + 1 To x.Count - 1
            If CInt(x (i)) > CInt(x(a)) Then
               t=x(i)
               x(i) = x(a)
               x(a) = t
            End If
        Next
    Next

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    it isnt sorting at all like its giving the same numbers as before sorting

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    Name:  Screenshot 2024-04-23 025102.png
Views: 44
Size:  21.3 KB it says i got this error

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    Quote Originally Posted by Huntington View Post
    Attachment 191266 it says i got this error
    If you’re posting code, use plain text and the forum formatting.
    If you have an error, I don’t want to have to open an attachment, which may or may not work…

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    For i = 0 to…

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    I apologize, i copied this code (from you) and im getting an error called BC30074: Constant cannot be the target of an assignment.

    Dim i, a As Integer
    Dim t As String
    For 1 = 0 To x. Count - 1
    For a = i + 1 To x.Count - 1
    If CInt(x (i)) > CInt(x(a)) Then
    t=x(i)
    x(i) = x(a)
    x(a) = t
    End If
    Next
    Next

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    the error still persists (the numbers arent being sorted)

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    The errors you’re seeing now are a result of what I’ve copied and pasted. You’ll have to type it out again. The important part is using the correct types for your variables, and converting the elements of x to integer, which will sort differently to standard string sorting. Also pass your List(Of String) ByRef

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    Code:
    Dim i, a As Integer
    Dim t As String
    For i = 0 To x. Count - 1
        For a = i + 1 To x.Count - 1
            If CInt(x (i)) > CInt(x(a)) Then
                t=x(i)
                x(i) = x(a)
                x(a) = t
            End If
        Next
    Next

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    it still persists and yes i retyped it, if needed here is my whole code:




    ' Read the numbers from the ListBox
    Dim numbers As New List(Of Integer)
    For Each item As Object In lstNumbers.Items
    numbers.Add(CInt(item))
    Next

    ' Sort the numbers using bubble sort
    BubbleSort(numbers)

    ' Display the sorted elements in the lstSortedNumbers ListBox
    lstSortedNumbers.Items.Clear()
    For Each num In numbers
    lstSortedNumbers.Items.Add(num)
    Next
    End Sub

    ' Bubble sort function
    Private Sub BubbleSort(ByRef x As List(Of Integer))
    Dim i, a As Integer
    Dim t As String
    For i = 0 To x.Count - 1
    For a = i + 1 To x.Count - 1
    If CInt(x(i)) > CInt(x(a)) Then
    t = x(i)
    x(i) = x(a)
    x(a) = t
    End If
    Next
    Next
    End Sub

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    Now you've changed all of your datatypes, instead of just those i highlighted. Try exactly this..

    Code:
    ' Read the numbers from the ListBox
    Dim numbers As New List(Of Integer)
    For Each item As Object In lstNumbers.Items
    numbers.Add(CInt(item))
    Next
    
    ' Sort the numbers using bubble sort
    BubbleSort(numbers)
    
    ' Display the sorted elements in the lstSortedNumbers ListBox
    lstSortedNumbers.Items.Clear()
    For Each num In numbers
    lstSortedNumbers.Items.Add(num)
    Next
    End Sub
    
    ' Bubble sort function
    Private Sub BubbleSort(ByRef x As List(Of Integer))
        Dim i, a, t As Integer
        For i = 0 To x.Count - 1
            For a = i + 1 To x.Count - 1
                If x(i) > x(a) Then
                    t = x(i)
                    x(i) = x(a)
                    x(a) = t
                End If
            Next
        Next
    End Sub

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    alright

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    If that doesn't work, tell me the exact error, and the code where it breaks.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    Name:  Screenshot 2024-04-23 031558.jpg
Views: 43
Size:  8.8 KB Name:  Screenshot 2024-04-23 032012.png
Views: 44
Size:  4.5 KB Its basically like this

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    the numbers wont bubblesort no matter what i do..

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    Are your original listbox items just numbers or numbers and text?

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    just numbers also im using a system where the user would upload a notepad file with numbers only in them to bubblesort

  22. #22
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    How many items does your unsorted list contain?

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    6 digits of numbers

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    Your screenshot in post #18 only shows 1 integer number??? Where are the others?

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    sorry i meant that, thats the only integers there

  26. #26
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    You mean 1 six digit number?
    So 684723, you want to sort as 234678? That's not anywhere near what your code does

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    i just want it to bubblesort thats all

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    i meant those numbers there on the post

  29. #29

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    so to work i need to have multiple integers for it to work?

  30. #30
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,200

    Re: Hello! Id like some help with my bubblesort code

    By definition, a list of 1 item IS sorted. Both in ascending and descending order. It's like magic.

  31. #31
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim numbers() As Integer = Array.ConvertAll(lstNumbers.Items(0).ToString.ToCharArray, Function(c As Char) CInt(c.ToString))
    
        ' Sort the numbers using bubble sort
        BubbleSort(numbers)
        ' Display the sorted elements in the lstSortedNumbers ListBox
        lstSortedNumbers.Items.Clear()
        For Each num In numbers
            lstSortedNumbers.Items.Add(num)
        Next
    
    End Sub
    Code:
    ' Bubble sort function
    Private Sub BubbleSort(ByRef x() As Integer)
        Dim i, a, t As Integer
        For i = 0 To x.Count - 1
            For a = i + 1 To x.Count - 1
                If x(i) > x(a) Then
                    t = x(i)
                    x(i) = x(a)
                    x(a) = t
                End If
            Next
        Next
    End Sub

  32. #32
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,200

    Re: Hello! Id like some help with my bubblesort code

    By the way, this screams of someone in complete panic mode trying to turn in an assignment before the end of a term so that they don't fail a class. Just saying.

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    dude you are a life saver thank you so much i would literraly buy you a drink if its possible, biggest thanks to you

  34. #34
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Hello! Id like some help with my bubblesort code

    My last post will display your numbers as..

    2
    3
    4
    6
    7
    8
    to display as...

    234678
    , use this code...

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim numbers() As Integer = Array.ConvertAll(lstNumbers.Items(0).ToString.ToCharArray, Function(c As Char) CInt(c.ToString))
    
        ' Sort the numbers using bubble sort
        BubbleSort(numbers)
        Dim newNumber As Integer = CInt(String.Join("", Array.ConvertAll(numbers, Function(x) x.ToString)))
        ' Display the sorted elements in the lstSortedNumbers ListBox
        lstSortedNumbers.Items.Clear()
        lstSortedNumbers.Items.Add(newNumber)
    
    End Sub

  35. #35

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    thank you dude a million thanks to you

  36. #36

    Thread Starter
    Junior Member
    Join Date
    Apr 2024
    Posts
    31

    Re: Hello! Id like some help with my bubblesort code

    thats true i have like 2 weeks to submit which is emough time but i like to start early

Posting Permissions

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



Click Here to Expand Forum to Full Width