13 Replies - 2474 Views - Last Post: 23 October 2008 - 07:05 AM Rate Topic: -----

#1 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Why is my bubble sort not working?

Posted 22 October 2008 - 02:18 AM

I can guarantee this is going to be something simple, but it's been one of those days and quite frankly I'm over it.
My sort works fine except for the fact that i have to run it manually (it fires on a button click, so lots of button clicks later) instead of me clicking the button once and it runs through until it's done

  Dim temp1, temp2, temp3 As String
		Dim i, j As Integer

		For i = 0 To ListBox1.Items.Count - 2
			For k As Integer = 0 To ListBox1.Items.Count - 2

				temp1 = ListBox1.Items(i)
				temp2 = ListBox1.Items(i + 1)
				j = temp1.CompareTo(temp2)
				If j < 0 Then

					temp3 = temp1
					temp1 = temp2
					temp2 = temp3

					ListBox1.Items(i) = temp1
					ListBox1.Items(i + 1) = temp2

				End If

			Next
		Next



Is This A Good Question/Topic? 0
  • +

Replies To: Why is my bubble sort not working?

#2 WayneSpangler   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 22-March 08

Re: Why is my bubble sort not working?

Posted 22 October 2008 - 04:35 AM

Try this:
  Dim temp1, temp2, temp3 As String
		Dim i, j As Integer
>>	Dim Done as boolean
	 
>> Do 
		For i = 0 To ListBox1.Items.Count - 2
			For k As Integer = 0 To ListBox1.Items.Count - 2

				temp1 = ListBox1.Items(i)
				temp2 = ListBox1.Items(i + 1)
				j = temp1.CompareTo(temp2)
				If j < 0 Then

					temp3 = temp1
					temp1 = temp2
					temp2 = temp3

					ListBox1.Items(i) = temp1
					ListBox1.Items(i + 1) = temp2
>>				Done=True
				End If

			Next
		Next
>> Loop While Done


P.S. You do know that the listbox has a sort property.
Color and bold doesn't work so had to use >> to show additions.

This post has been edited by WayneSpangler: 22 October 2008 - 04:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Why is my bubble sort not working?

Posted 22 October 2008 - 03:07 PM

View PostWayneSpangler, on 22 Oct, 2008 - 04:35 AM, said:

P.S. You do know that the listbox has a sort property.


.....i do now
Was This Post Helpful? 0
  • +
  • -

#4 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Why is my bubble sort not working?

Posted 22 October 2008 - 03:16 PM

Told you it would be something simple. Thanks alot.

Tried the code but it freezes the app.

I'm trying to apply it to

  Dim temp1, temp2 As String
		Dim i, j As Integer
			For i = 0 To ListBox1.Items.Count - 2


			temp1 = ListBox1.Items(i)
			temp2 = ListBox1.Items(i + 1)
			j = temp1.CompareTo(temp2)
			If j = 0 Then
				Try
					ListBox1.Items(i + 1) = ListBox1.Items(i + 2)
				Catch ex As ArgumentOutOfRangeException
					ListBox1.Items(i + 1) = ""
				End Try
			End If


		Next

		For i = 0 To ListBox1.Items.Count - 2


			If ListBox1.Items.Item(i) = "" Then
				ListBox1.Items.RemoveAt(i)
			End If

		Next



basically i just wanted to order the listbox so i could remove any duplicates. if i add in the loop while it becomes unresponsive.

This post has been edited by Damage: 22 October 2008 - 03:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 magicmonkey   User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Why is my bubble sort not working?

Posted 22 October 2008 - 04:26 PM

It is bad practice to use UI controls to process / manipulate data, as interacting with them is thousands of times slower then say a collection in memory. Where do you get the data from to populate the list? If anything you should check for duplicates as you load the data and remove them before you populate the list control. Or at the very least simply do not add an item to the list control if it already exists, ie check for it before you add it.

Preprocess data in collection, then add...
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DataSource As String() = {"One", "One", "Two", "Three"}

        Dim MyData As New List(Of String)
        For Each Data As String In DataSource
            If Not MyData.Contains(Data) Then
                MyData.Add(Data)
            End If
        Next

        ListBox1.BeginUpdate()
        For Each Data As String In MyData
            ListBox1.Items.Add(Data)
        Next
        ListBox1.EndUpdate()
    End Sub



Check for item before adding...
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim DataSource As String() = {"One", "One", "Two", "Three"}

        ListBox1.BeginUpdate()
        For Each Data As String In DataSource
            If Not ListBox1.Items.Contains(Data) Then
                ListBox1.Items.Add(Data)
            End If
        Next
        ListBox1.EndUpdate()
    End Sub


Was This Post Helpful? 0
  • +
  • -

#6 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Why is my bubble sort not working?

Posted 22 October 2008 - 10:03 PM

dude, i don't get it. I copied and pasted your code and it's not working. I created my string array from a text document and then used your code but i'm still getting duplicate records. I thought maybe i'd done something wrong creating the array but I don't think i have. Reading the code,it doesn't make sense that there should be any duplicates. Am I doing something wrong?

  Dim path As String = "c:\spider docs\Testfile2.txt"
		Dim DataSource As String() = File.ReadAllLines(path)


		Dim MyData As New List(Of String)
		For Each Data As String In DataSource
			If Not MyData.Contains(Data) Then
				MyData.Add(Data)
			End If
		Next

		ListBox1.BeginUpdate()
		For Each Data As String In MyData
			ListBox1.Items.Add(Data)
		Next
		ListBox1.EndUpdate()



here's a sample of the text file(sorted)

Quote

122343588751.jpg
122343589582.jpg
122343589582.jpg
122343590374.jpg
122343590374.jpg
122343591170.jpg
122343591170.jpg
122343591967.jpg
122343591967.jpg
122343592728.jpg
122343592728.jpg
122343593511.jpg
122343593511.jpg
122343594300.jpg
122343594300.jpg
122343595135.jpg
122343595135.jpg

Was This Post Helpful? 0
  • +
  • -

#7 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: Why is my bubble sort not working?

Posted 22 October 2008 - 10:31 PM

did you try the remove at method
Was This Post Helpful? 0
  • +
  • -

#8 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Why is my bubble sort not working?

Posted 22 October 2008 - 10:49 PM

In order to use removeat, I would have to sort the text file first and make sure I know how many duplicate copies there are. If you look further up you'll see my original method makes use of removeat. This way shuoldn't have to, it should be checking that no duplicates are imported into the listbox.....but it's not working.
Was This Post Helpful? 0
  • +
  • -

#9 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: Why is my bubble sort not working?

Posted 22 October 2008 - 11:28 PM

i don't think so

just enabled the sorted in listbox

and try this following code

        For i = 0 To ListBox1.Items.Count - 2
            Try
                temp1 = ListBox1.Items(i)
                temp2 = ListBox1.Items(i + 1)

                If temp1 Is temp2 Then
                    ListBox1.Items.RemoveAt(i + 1)
                End If
            Catch ex As ArgumentOutOfRangeException
                Exit For
            End Try
        Next



i didn't change lot more form your code think's it may be right
Was This Post Helpful? 0
  • +
  • -

#10 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Why is my bubble sort not working?

Posted 23 October 2008 - 01:06 AM

i'm getting confused. When I compare these two the result is a 1. Meaning that the one is greater than the other one. :blink: how does that work?
. Figured that the methods look right so it has to be the input. So using this code with this input.

			temp1 = ListBox1.Items(i)
			temp2 = ListBox1.Items(i + 1)
			j = temp1.CompareTo(temp2)



Quote

"http://img.7chan.org/b/src/122343589582.jpg "
"http://img.7chan.org/b/src/122343589582.jpg"

This post has been edited by Damage: 23 October 2008 - 01:06 AM

Was This Post Helpful? 0
  • +
  • -

#11 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Why is my bubble sort not working?

Posted 23 October 2008 - 01:11 AM

scratch that, the sneaky bastard was trying to fool me. A space had been added at the end of every alternate line. hence the uneven evaluation.
and all my problems are solved. Magic monkey's code works perfect as usual.

Remember kids, if it ain't the code, it's gotta be the input.

Valuable lesson learned

This post has been edited by Damage: 23 October 2008 - 01:13 AM

Was This Post Helpful? 0
  • +
  • -

#12 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Why is my bubble sort not working?

Posted 23 October 2008 - 01:29 AM

Thanks to everyone for their help and input. Greatly appreciated as always
Was This Post Helpful? 0
  • +
  • -

#13 magicmonkey   User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Why is my bubble sort not working?

Posted 23 October 2008 - 05:43 AM

I had to speed & clean it up even more for you. You can save some time by checking for duplicates while reading the file.

        Dim MyData As New List(Of String)
        Dim Path As String = "c:\spider docs\Testfile2.txt"
        Dim Line As String

        Using MyReader As New StreamReader(Path)
            Do Until MyReader.EndOfStream
                Line = MyReader.ReadLine.Trim
                If Not MyData.Contains(Line) Then
                    MyData.Add(Line)
                End If
            Loop
        End Using

        With ListBox1
            .BeginUpdate()
            .Items.AddRange(MyData.ToArray)
            .EndUpdate()
        End With


Was This Post Helpful? 1
  • +
  • -

#14 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 121
  • View blog
  • Posts: 666
  • Joined: 01-October 08

Re: Why is my bubble sort not working?

Posted 23 October 2008 - 07:05 AM

		Dim lines As New List(Of String)
		lines.AddRange(IO.File.ReadAllLines("c:\test.txt")) 'change file name
		lines.Sort() 'sortem
		Dim idx As Integer
		idx = 0
		Do 'remove dup's
			If idx > lines.Count - 2 Then Exit Do
			If lines(idx).Trim = lines(idx + 1).Trim Then
				lines.RemoveAt(idx)
			Else
				idx += 1
			End If
		Loop
		ListBox1.Items.AddRange(lines.ToArray) 'add to listbox
 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1