-
Apr 22nd, 2024, 02:10 PM
#1
Thread Starter
Junior Member
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
-
Apr 22nd, 2024, 02:17 PM
#2
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
-
Apr 22nd, 2024, 02:35 PM
#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.
-
Apr 22nd, 2024, 02:36 PM
#4
Re: Hello! Id like some help with my bubblesort code
Private Sub BubbleSort(ByRef x As List(of String))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 02:45 PM
#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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 02:46 PM
#6
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
it isnt sorting at all like its giving the same numbers as before sorting
-
Apr 22nd, 2024, 02:49 PM
#7
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
it says i got this error
-
Apr 22nd, 2024, 02:51 PM
#8
Re: Hello! Id like some help with my bubblesort code
 Originally Posted by Huntington
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 02:53 PM
#9
Re: Hello! Id like some help with my bubblesort code
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 02:54 PM
#10
Thread Starter
Junior Member
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
-
Apr 22nd, 2024, 02:57 PM
#11
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
the error still persists (the numbers arent being sorted)
-
Apr 22nd, 2024, 02:57 PM
#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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 03:00 PM
#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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 03:03 PM
#14
Thread Starter
Junior Member
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
-
Apr 22nd, 2024, 03:14 PM
#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
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 03:14 PM
#16
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
-
Apr 22nd, 2024, 03:18 PM
#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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 03:21 PM
#18
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
Its basically like this
-
Apr 22nd, 2024, 03:22 PM
#19
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
the numbers wont bubblesort no matter what i do..
-
Apr 22nd, 2024, 03:23 PM
#20
Re: Hello! Id like some help with my bubblesort code
Are your original listbox items just numbers or numbers and text?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 03:25 PM
#21
Thread Starter
Junior Member
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
-
Apr 22nd, 2024, 03:25 PM
#22
Re: Hello! Id like some help with my bubblesort code
How many items does your unsorted list contain?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 03:26 PM
#23
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
-
Apr 22nd, 2024, 03:27 PM
#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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 03:28 PM
#25
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
sorry i meant that, thats the only integers there
-
Apr 22nd, 2024, 03:29 PM
#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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 03:30 PM
#27
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
i just want it to bubblesort thats all
-
Apr 22nd, 2024, 03:32 PM
#28
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
i meant those numbers there on the post
-
Apr 22nd, 2024, 03:39 PM
#29
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
so to work i need to have multiple integers for it to work?
-
Apr 22nd, 2024, 03:43 PM
#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.
-
Apr 22nd, 2024, 03:44 PM
#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
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 03:46 PM
#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.
-
Apr 22nd, 2024, 03:46 PM
#33
Thread Starter
Junior Member
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
-
Apr 22nd, 2024, 03:52 PM
#34
Re: Hello! Id like some help with my bubblesort code
My last post will display your numbers as..
to display as...
, 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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2024, 04:06 PM
#35
Thread Starter
Junior Member
Re: Hello! Id like some help with my bubblesort code
thank you dude a million thanks to you
-
Apr 22nd, 2024, 04:07 PM
#36
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|