(klik on the image if want to see real image)
Sorting adalah proses mengurutkan data sesuai aturan tertentu, tujuannya mengurutkan data dan mempercepat pencarian / searching
- Ascending atau nondecreasing (dari yang paling kecil ke yang paling besar)
- Descendng atau nonincreasing (dari yang paling besar ke yang paling kecil)
- Internal sorting, seluruh data diletakkan di memory
- External sorting, seluruh data berada pada secondary storage
- Bubble Sort, kompleksitas algoritma O(n²)
- Selection Sort, kompleksitas algoritma 0,5n²+O(n)
- Insertion Sort, kompleksitas algoritma O(n²)
- Quick Sort, kompleksitas algoritma O(n log n)
Kesimpulan :
- Quick Sort merupakan teknik sorting paling cepat
Main
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
ListBox2.Items.Clear()
Dim angka() As Long
Dim n, i As Integer
n = InputBox("Masukkan Banyaknya Angka ? ", "Sorting")
ReDim angka(n - 1)
For i = 0 To angka.Length - 1
angka(i) = InputBox("Masukkan Angka Ke-" & i + 1, "Sorting")
ListBox1.Items.Add(angka(i))
Next
'Aktifkan function yg ingin dipakai
'==================
'Bubble_sort(angka)
'Selection_Sort(angka)
'Insertion_sort(angka)
'Quick_sort(angka, 0, angka.Length - 1)
'==================
For i = 0 To angka.Length - 1
ListBox2.Items.Add(angka(i))
Next
End Sub
Function Bubble Sort
Function Bubble_sort(ByRef angka() As Long)
Dim i, j As Integer
Dim temp As Long
For i = 0 To angka.Length - 1
For j = angka.Length - 1 To i + 1 Step -1
If angka(j - 1) > angka(j) Then
'tukar
temp = angka(j)
angka(j) = angka(j - 1)
angka(j - 1) = temp
End If
Next
Next
End Function
Function Selection Sort
Function Selection_Sort(ByVal angka() As Long)
Dim i, j, temp As Integer
Dim bucket As Long
For i = 0 To angka.Length - 2
temp = i
For j = i + 1 To angka.Length - 1
If (angka(j) < temp =" j"> i) Then
'tukar
bucket = angka(temp)
angka(temp) = angka(i)
angka(i) = bucket
End If
Next
End Function
Function Insertion Sort
Function Insertion_sort(ByVal angka() As Long)
Dim i, j, temp As Integer
For i = 1 To angka.Length - 1
temp = angka(i)
j = i
Do While (j > 0 AndAlso (angka(j - 1) > temp))
angka(j) = angka(j - 1)
j = j - 1
Loop
angka(j) = temp
Next
End Function
Function Quick Sort
Function Quick_sort(ByVal angka() As Long, ByVal kiri As Integer, ByVal kanan As Integer)
Dim j, k As Integer
Dim temp As Long
If (kiri < kanan) Then
j = kiri
k = kanan + 1
Do
Do
j = j + 1
If j = k Then
GoTo keluar
End If
Loop While (angka(j) < angka(kiri) AndAlso j < k)
keluar:
Do
k = k - 1
Loop While (angka(k) > angka(kiri))
If (j < k) Then
'tukar
temp = angka(j)
angka(j) = angka(k)
angka(k) = temp
End If
'Console.WriteLine(angka(j) & " " & angka(k))
Loop While (j < k + 1)
'tukar
temp = angka(kiri)
angka(kiri) = angka(k)
angka(k) = temp
'recursive
Quick_sort(angka, kiri, k - 1)
Quick_sort(angka, k + 1, kanan)
End If
End Function
No comments:
Post a Comment