Results 1 to 5 of 5

Thread: HELP!!! with Bubblesort

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    5

    Cool HELP!!! with Bubblesort

    I'm a rookie coder, here is the code I've created, but would like to know how to adda bubble sort to the already existing code.

    Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim arrMyArray As New ArrayList
    Dim arrLtr As New ArrayList
    Dim arrCount As New ArrayList
    Dim icounter, x, icount As Integer
    Dim strName As String = TextBox1.Text.ToLower
    Dim arrNBR As New ArrayList
    For icounter = 1 To TextBox1.Text.Length
    arrMyArray.Add(Mid(strName, icounter, 1))
    Next icounter



    arrMyArray.Sort()




    For icounter = 0 To arrMyArray.Count - 1

    If icounter < arrMyArray.Count - 1 Then
    x = StrComp(arrMyArray(icounter), arrMyArray(icounter + 1), CompareMethod.Text)
    If x = 0 Then
    icount += 1
    Else
    icount += 1
    arrLtr.Add(arrMyArray(icounter))
    arrNBR.Add(icount)
    icount = 0
    End If
    Else
    x = StrComp(arrMyArray(icounter), arrMyArray(icounter - 1), CompareMethod.Text)
    If x = 0 Then
    icount += 1
    arrLtr.Add(arrMyArray(icounter))
    arrNBR.Add(icount)
    Else
    icount += 1
    arrLtr.Add(arrMyArray(icounter))
    arrNBR.Add(icount)
    End If

    End If

    Next

    For x = 0 To arrLtr.Count - 1
    Label1.Text = Label1.Text + arrLtr(x) + " = " + CStr(arrNBR(x)) & vbCrLf
    Next


    End Sub

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: HELP!!! with Bubblesort

    Welcome to the forums

    You would probobally get better help on the .NET section of the forum or wait for mod to move the thread for you

  3. #3

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: HELP!!! with Bubblesort

    The only reason that I can think of that you would want to implement your own bubble sort rather than using the inbuilt Array.Sort method is this is homework and that's a requirement. In that case it is inappropriate for us to simply provide the code for you.

    As with any algorithm, the way to implement a bubble sort is to first write out the steps in words. You then write pseudo-code that corresponds to the words and, finally, you write VB code that corresponds to the pseudo-code. To go straight from the idea to the VB code is the lazy way because it seeks to remove the intermediate steps, but it actually makes things much harder. We all tried to do it when we were learning but, believe me, if you follow the proper steps the solution will come much more easily and you will learn more.

    If you make an attempt at an implementation and run into issues then you can post again with more specific questions.

    Also, please wrap your code in VBCODE tags to make it more readable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: HELP!!! with Bubblesort

    Just in case this isn't actually homework, and you have reason to write your own sort instead of using the Array.Sort, you might look up Shell sort. It's really bizarre, but it is also REALLY simple to code. Bubble sort is just about the worst possible sort algorithm out there. I much prefer Shell sort for custom sort routines, because I don't have to worry about the (rare) problems that QSort has. QSort is probably the algorithm used in Array.Sort, and is the fastest sort algorithm in most cases, but can be horribly slow in certain specific cases. I've also never seen a particularly simple code example of QSort, though there has to be one out there. The reason I have never seen one might simply be that it has been built into libraries for languages like C/C++, so you don't get the algorithm, you get a dll and a function prototype.
    My usual boring signature: Nothing

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