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

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

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

Attachment 191264Attachment 191264 it is basically like this

3. ## 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. ## Re: Hello! Id like some help with my bubblesort code

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

5. ## 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. ## 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. ## Re: Hello! Id like some help with my bubblesort code

it says i got this error

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

Originally Posted by Huntington
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. ## Re: Hello! Id like some help with my bubblesort code

For i = 0 toâ€¦

10. ## 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. ## Re: Hello! Id like some help with my bubblesort code

the error still persists (the numbers arent being sorted)

12. ## 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. ## 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. ## 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
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
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. ## 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
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
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```

alright

17. ## 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. ## Re: Hello! Id like some help with my bubblesort code

Its basically like this

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

the numbers wont bubblesort no matter what i do..

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

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

21. ## 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. ## Re: Hello! Id like some help with my bubblesort code

How many items does your unsorted list contain?

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

6 digits of numbers

24. ## 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. ## Re: Hello! Id like some help with my bubblesort code

sorry i meant that, thats the only integers there

26. ## 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. ## Re: Hello! Id like some help with my bubblesort code

i just want it to bubblesort thats all

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

i meant those numbers there on the post

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

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

30. ## 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. ## 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
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. ## 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. ## 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. ## 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()

End Sub```

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

thank you dude a million thanks to you

36. ## 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
•