Results 1 to 5 of 5

Thread: Using SelectionSort with a Listbox for Visual Basic

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Using SelectionSort with a Listbox for Visual Basic

    Hello,

    I am starting out with Visual Basic, and I am trying to learn how to implement selectionsort with visual basic.

    This is my code.
    Code:
    Option Strict On
    
    Public Class Form1
    
        'Project    SelectionSort
        'Purpose    Sort and count the number of exchanges and comparisons which the algorithm performs. 
        'Input      None
        'Output     None
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCount.Click
    
            'Purpose    Count the number of exchanges.
            'Input      None
            'Output     None
    
            Dim min As Integer          'Variable min 
    
            Dim min_index As Integer    'Index of variable min
    
            Dim last_index As Integer   'Index of last
    
            last_index = Listbox1.Items.Count - 1
            min = CInt(Listbox1.Items(0))
            min_index = 0
    
            For J = 1 To last_index
                If min > CInt(Listbox1.Items(J)) Then
                    min = CInt(Listbox1.Items(J))
                    min_index = J
    
                End If
            Next
    
            lblCount.Text = CStr(min_index)
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandomize.Click
    
            'Purpose    Randomize the numbers in the listbox.
            'Input      None
            'Output     None
    
            Dim J As Integer        'Loop control variable
    
            'Clear the listbox
            Listbox1.Items.Clear()
    
            'Generate a list of random numbers between 0 and 1
            Randomize()
            For J = 1 To 100
                Listbox1.Items.Add(Rnd())
            Next
    
        End Sub
    
        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
    
            'Purpose    Selectionsort
            'Input      None    
            'Output     None
            'Error      There is an error with this part of the program. I believe that one of these conversions is not correct. Everything else seems to be running smoothly as it not giving me a build error but a logic error.
    
            Dim i As Integer        'Loop control variable for min to max
            Dim j As Integer        'Loop control variable for i + 1 to max
            Dim best_value As Long  'Best value in listbox
            Dim best_j As Integer   'Best j value from the listbox
            Dim min As Integer      'Min value in list
            Dim max As Integer      'Max value in list
    
            For i = min To max - 1
                best_value = CInt(Listbox1.Items(i))
                best_j = i
                For j = i + 1 To max
                    If CInt(Listbox1.Items(j)) < best_value Then
                        best_value = CInt(Listbox1.Items((j)))
                        best_j = j
                    End If
                Next j
                Listbox1.Items(best_j) = Listbox1.Items(i)
                Listbox1.Items(i) = best_value
            Next i
    
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    
            'Purpose    Terminates the program
            'Input      Click
            'Output     Closes program
    
            Close()
        End Sub
    End Class
    I have highlighted the area that I am having trouble with. I would like someone to work with me, so that I can figure out what I am doing wrong.
    Last edited by Hack; Jan 23rd, 2013 at 06:29 PM. Reason: Added Code Tags

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Using SelectionSort with a Listbox for Visual Basic

    Moved From The CodeBank (which is for sharing code rather than posting questions )

    You posted this in the VB6 Codebank, but it is clear from your example code that you are not using that version, but, rather, VB.NET

    What version of VB.NET are you using? 2008? 2010? 2012? Earlier?

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Using SelectionSort with a Listbox for Visual Basic

    There is a better way to sort ListBox Items

    vb.net Code:
    1. Dim lb(ListBox1.Items.Count - 1) As Object ' set up array of appropriate dimension
    2.         ListBox1.Items.CopyTo(lb, 0) ' copy to the array
    3.         Array.Sort(lb) ' use the generic array sort (ascending)
    4.         Array.Reverse(lb) ' use reverse to create descending sort
    5.         ListBox1.Items.Clear()
    6.         ListBox1.Items.AddRange(lb) ' copy items back
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Re: Using SelectionSort with a Listbox for Visual Basic

    Thank you Hack for moving my post. I am using VB 2010.

    Thank you, that code works. However, I kind of wanted to figure out how to use selectionsort to complete this program before moving on to other sorting codes.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Re: Using SelectionSort with a Listbox for Visual Basic

    Thank you Hack!

    I am using Visual Basic 2010.

    That method works. However, I am trying to learn selectionsort for a project that I am doing.
    Also, what sorting method is that called.

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