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.
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?
Re: Using SelectionSort with a Listbox for Visual Basic
There is a better way to sort ListBox Items
vb.net Code:
Dim lb(ListBox1.Items.Count - 1) As Object ' set up array of appropriate dimension
ListBox1.Items.CopyTo(lb, 0) ' copy to the array
Array.Sort(lb) ' use the generic array sort (ascending)
Array.Reverse(lb) ' use reverse to create descending sort
ListBox1.Items.Clear()
ListBox1.Items.AddRange(lb) ' copy items back
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.
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.