I am so lost with this Bubble Sort array. I am trying to alphabetize these baseball players. Having a lot of trouble getting everything right.

Public Class frmBubbleSort
    '	Declare array with Class (form) scope
    Dim players As String() = {"Crawford", "Upton", "Longoria", "Bartlett", "Pena", "Navarro", "Garza", "Shields", "Price", "Neiman"}
    Dim A As Integer = 1
    Private Sub frmBubbleSort_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '	Load array into ComboBox
        Array.Sort(players)

        cboBubble.MaxDropDownItems = players
        cboBubble.DataSource = A

    End Sub
    Private Sub Swap(ByRef first As String, ByRef second As String)
        '	Define Swap Method
        If first > second Then
            Swap(first, second)
        End If

        Dim temp As String   ' local variable for swap method
        temp = first
        first = second
        second = temp
    End Sub
    Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
        '	number of passes is one less than the number of items
        For passnum As String = 1 To players - 1 '	pass loop
            '	comparison loop - each pass needs one less comparison
            For i As Integer = 1 To players - passnum 'comparison loop
                If A(i - 1) > A(i) Then ' comparison
                    Swap(A(i - 1), A(i)) 'call swap and overwrite original values
                End If
            Next ' End of loop for comparisons
        Next ' end of loop for pass

        '	Display sorted array in ListBox
        For i As Integer = 0 To players - 1
            lstSort.Items.Add(A(i))
        Next
    End Sub ' End of Click Event


    Private Sub BubbleSort_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

To drag you out of trouble, here's a simple BSort Algorithm:

For outer = n - 1 to 0 step - 1
    For inner = 0 to outer - 1
     If list(inner) > list(inner + 1) then
        temp = list(inner) 
        list(inner) = list(inner + 1)
        list(inner + 1) = temp
     end if
    Next
Next

You should take this and reform it to your current needs.

Hope i helped :D

Hi,

I think you need to call your your array first and then sorting it.
Try something like this:

Public Class frmBubbleSort
' Declare array with Class (form) scope
Dim players As String() = {"Crawford", "Upton", "Longoria", "Bartlett", "Pena", "Navarro", "Garza", "Shields", "Price", "Neiman"}

Private Sub frmBubbleSort_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' Load array into ComboBox
players.Sort(players) ' your arrays name is players then sorting it

cboBubble.MaxDropDownItems = players
cboBubble.DataSource = A

End Sub

I wish I could use this but I have to use the other way. Teacher's orders. This was her response back to me on how to fix things. I don't really know how to "read" it.


To drag you out of trouble, here's a simple BSort Algorithm:

For outer = n - 1 to 0 step - 1
    For inner = 0 to outer - 1
     If list(inner) > list(inner + 1) then
        temp = list(inner) 
        list(inner) = list(inner + 1)
        list(inner + 1) = temp
     end if
    Next
Next

You should take this and reform it to your current needs.

Hope i helped :D

Repost my original code because the other looks awful.

Public Class frmBubbleSort
    '	Declare array with Class (form) scope
    Dim players As String() = {"Crawford", "Upton", "Longoria", "Bartlett", "Pena", "Navarro", "Garza", "Shields", "Price", "Neiman"}
    Dim A As Integer = 1
    Private Sub frmBubbleSort_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '	Load array into ComboBox
        Array.Sort(players)

        cboBubble.MaxDropDownItems = players
        cboBubble.DataSource = A

    End Sub
    Private Sub Swap(ByRef first As String, ByRef second As String)
        '	Define Swap Method
        If first > second Then
            Swap(first, second)
        End If

        Dim temp As String   ' local variable for swap method
        temp = first
        first = second
        second = temp
    End Sub
    Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
        '	number of passes is one less than the number of items
        For passnum As String = 1 To players - 1 '	pass loop
            '	comparison loop - each pass needs one less comparison
            For i As Integer = 1 To players - passnum 'comparison loop
                If A(i - 1) > A(i) Then ' comparison
                    Swap(A(i - 1), A(i)) 'call swap and overwrite original values
                End If
            Next ' End of loop for comparisons
        Next ' end of loop for pass

        '	Display sorted array in ListBox
        For i As Integer = 0 To players - 1
            lstSort.Items.Add(A(i))
        Next
    End Sub ' End of Click Event


    Private Sub BubbleSort_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

can anyone give me sjf and rr cpu scheduling program in vb???or sorting 5 numbers from lowest to highest..input will be on text boxt and out on label..tnx..:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.