Quantcast
Viewing all articles
Browse latest Browse all 27402

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.

Viewing all articles
Browse latest Browse all 27402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>