0% found this document useful (0 votes)
6 views3 pages

Listing Sorting

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Listing Sorting

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Public Class Form1

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click


If txtNilai.Text.Trim <> Nothing Then
txtNilai.Text &= ","
End If
txtNilai.Text &= txtN.Text
txtN.Clear()
txtN.Focus()
End Sub
Public Function Bubblesort(ByVal a() As String, ByVal asc As Boolean) As String()
Dim n As Integer = a.Length
Dim i As Integer, j As Integer
Dim iMin As Integer

For j = 0 To n - 2
iMin = j
For i = j + 1 To n - 1
If asc Then
If CInt(a(i)) < CInt(a(iMin)) Then
iMin = i
End If
Else
If CInt(a(i)) > CInt(a(iMin)) Then
iMin = i
End If
End If
Next
If iMin <> j Then
Dim tap As Integer = CInt(a(j))
a(j) = a(iMin)
a(iMin) = CStr(tap)
ListBox1.Items.Add(String.Join(",", a))
End If
Next
Return a
End Function
Public Function insertionsort(ByRef data() As String, ByVal asc As Boolean) As String()
For i As Integer = 1 To data.Length - 1
Dim j As Integer = i
While (j > 0)
If asc Then
If CInt(data(j - 1)) > CInt(data(j)) Then
Dim tmp As Integer = CInt(data(j - 1))
data(j - 1) = data(j)
data(j) = CStr(tmp)
j -= 1
ListBox1.Items.Add(String.Join(",", data))
Else
Exit While
End If
Else
If CInt(data(j - 1)) < CInt(data(j)) Then
Dim tmp As Integer = CInt(data(j - 1))
data(j - 1) = data(j)
data(j) = CStr(tmp)
j -= 1
ListBox1.Items.Add(String.Join(",", data))
Else
Exit While
End If
End If
End While
Next
Return data
End Function
Public Function selectionsort(ByRef data() As String, ByVal asc As Boolean) As String()
Dim n As Integer = data.Length
For i As Integer = 0 To n - 2
Dim minIndex As Integer = i
For j As Integer = i + 1 To n - 1
If asc Then

If CInt(data(j)) < CInt(data(minIndex)) Then


minIndex = j
End If
Else

If CInt(data(j)) > CInt(data(minIndex)) Then


minIndex = j
End If
End If
Next
Dim temp As String = data(minIndex)
data(minIndex) = data(i)
data(i) = temp
ListBox1.Items.Add(String.Join(",", data))
Next
Return data
End Function
Private Sub btnProses_Click(sender As Object, e As EventArgs) Handles btnProses.Click
ListBox1.Items.Clear()
Dim arr() As String = txtNilai.Text.Split(","c)
ListBox1.Items.Add(txtNilai.Text)
If CheckBox1.Checked = True Then
Bubblesort(arr, True)
End If
If CheckBox2.Checked = True Then
insertionsort(arr, True)
End If
If CheckBox3.Checked = True Then
selectionsort(arr, True)
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
If CheckBox1.Checked = True Then
CheckBox2.Checked = False
CheckBox3.Checked = False
End If
End Sub
Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs)
If CheckBox2.Checked = True Then
CheckBox1.Checked = False
CheckBox3.Checked = False
End If
End Sub
Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs)
If CheckBox3.Checked = True Then
CheckBox1.Checked = False
CheckBox2.Checked = False
End If
End Sub
Private Sub btnKembali_Click(sender As Object, e As EventArgs) Handles
btnKembali.Click
ListBox1.Items.Clear()
CheckBox1.Checked = False
CheckBox2.Checked = False
CheckBox3.Checked = False
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtN.Clear()
txtNilai.Clear()
ListBox1.Items.Clear()
CheckBox1.Checked = False
CheckBox2.Checked = False
CheckBox3.Checked = False
End Sub
End Class

You might also like