Professional Documents
Culture Documents
Sorting merupakan pengurutan data dalam struktur data sangat penting untuk data yang beripe data numerik ataupun karakter. Pengurutan dapat dilakukan secara ascending (urut naik) dan descending (urut turun), Pengurutan (Sorting) adalah proses menyusun kembali data yang sebelumnya telah disusun dengan suatu pola tertentu, sehingga tersusun secara teratur menurut aturan tertentu.
Anda diminta untuk melakukan proses pengurutan tersebut dapat digunakan berbagai macam cara/metoda dan kemudian membuat sebuah program sorting dengan metode buble, selection, insertion, quick sort. User akan diminta menginputkan 10 bilangan yang kemudian akan disort.
program file
tersebut
Langkah-Langkah Untuk Menyusun Praktikum 1, adalah : 1. Click Start | Program | Microsoft Visual Studio | Microsoft Visual Basic 6.0 2. Pilih Standard, kemudian pilih Open 3. Click File | Project, beri nama File seperti diatas 4. Output yang dihasilkan : 5. Disain Form Sbb :
6.
Setting Properties sbb : Nama Component Forms Text1 Properties Caption = Program Pengurutan (Sorting) Name = TxtData(1) Index = 1 Text = spaces Enable = false Name = TxtData(2) Text = spaces Index = 2 Enable = false Name = TxtData(3) Index = 3 Text = spaces Enable = false Name = TxtData(4) Index = 4 Text = spaces Enable = false Name = TxtData(5) Index = 5 Text = spaces Enable = false Name = TxtData(6) Index = 6 Text = spaces Enable = false Name = TxtData(7) Index = 7 Text = spaces Enable = false Name = TxtData(8) Index = 8 Text = spaces Enable = false Name = TxtData(9) Index = 9 Text = spaces Enable = false Name = TxtData(10) Index = 10 Text = spaces Enable = false Name = TxtHasil(1) Index = 1 Text = spaces
Text2
Text3
Text4
Text5
Text6
Text7
Text8
Text9
Text10
Text11
Text12
Text13
Text14
Text15
Text16
Text17
Text18
Text19
Text20
Enable = false Name = TxtHasil(2) Index = 2 Text = spaces Enable = false Name = TxtHasil(3) Index = 3 Text = spaces Enable = false Name = TxtHasil(4) Index = 4 Text = spaces Enable = false Name = TxtHasil(5) Index = 5 Text = spaces Enable = false Name = TxtHasil(6) Index = 6 Text = spaces Enable = false Name = TxtHasil(7) Index = 7 Text = spaces Enable = false Name = TxtHasil(8) Index = 8 Text = spaces Enable = false Name = TxtHasil(9) Index = 9 Text = spaces Enable = false Name = TxtHasil(10) Index = 10 Text = spaces Enable = false Name = Command1 Caption =& Masukan data secara acak Name = Command2 Caption = & Sorting secara Ascending Name = Command3 Caption =& S e l e s a i
7.
Pada general ketik source program Const maks = 10 Dim sort(maks) As Integer
8.
Double clicking Masukan data secara acak isi source program : Private Sub Command1_Click() Dim i, data As Integer For i = 1 To maks data = InputBox("Masukan data !!", "DATA", 0) sort(i) = data txtdata(i).Text = data Next i For i = 1 To maks txthasil(i).Text = "" Next i End Sub
9.
Double clicking pada Sorting secara Ascending isi source program : Private Sub Command2_Click() Dim i As Integer If optbubble = True Then Call bubblesort(sort(), maks) ElseIf optselection = True Then Call selectionsort(sort(), maks) ElseIf optinsertion = True Then Call insertionsort(sort(), maks) ElseIf optshell = True Then Call shellsort(sort(), maks) Else MsgBox "Tentukan metode sorting yang digunakan", vbOKOnly + vbCritical, "ERROR" End If For i = 1 To maks txthasil(i).Text = sort(i) Next i End Sub
10. Double clicking pada Sorting secara Descending isi source program : Private Sub Command3_Click() Dim i As Integer If optbubble = True Then Call bubblesortDesc(sort(), maks) ElseIf optselection = True Then Call selectionsortDesc (sort(), maks) ElseIf optinsertion = True Then Call insertionsortDesc (sort(), maks) ElseIf optshell = True Then Call shellsortDesc (sort(), maks) Else MsgBox "Tentukan metode sorting yang digunakan", vbOKOnly + vbCritical, "ERROR" End If For i = 1 To maks txthasil(i).Text = sort(i) Next i End Sub
11. Double clicking pada Selesai isi source program : Private Sub Command3_Click() Unload Me End Sub
12. Klik kanan pada project ~ add ~ module isi source program : Sub bubblesort(ByRef data() As Integer, ByVal n As Integer) Dim i, j, bubble As Integer For i = 1 To (n - 1) For j = n To (i + 1) Step -1 If data(j) < data(j - 1) Then bubble = data(j) data(j) = data(j - 1) data(j - 1) = bubble End If Next j Next i End Sub
Sub bubblesortDesc(ByRef data() As Integer, ByVal n As Integer) Dim i, j, bubble As Integer For i = 1 To (n - 1) For j = n To (i + 1) Step -1 If data(j) > data(j - 1) Then bubble = data(j) data(j) = data(j - 1) data(j - 1) = bubble End If Next j Next i End Sub
Sub selectionsort(ByRef data() As Integer, ByVal n As Integer) Dim i, j, kecil, temp As Integer For i = 1 To (n - 1) kecil = i For j = (i + 1) To n If data(j) < data(kecil) Then kecil = j End If Next j temp = data(i) data(i) = data(kecil) data(kecil) = temp Next i End Sub
Sub selectionsortDesc(ByRef data() As Integer, ByVal n As Integer) Dim i, j, kecil, temp As Integer For i = 1 To (n - 1) kecil = i For j = (i + 1) To n If data(j) > data(kecil) Then kecil = j End If Next j temp = data(i) data(i) = data(kecil) data(kecil) = temp Next i End Sub
Sub insertionsort(ByRef data() As Integer, ByVal n As Integer) Dim i, j, temp As Integer For i = 2 To n temp = data(i) j=i-1 data(0) = temp While temp < data(j) data(j + 1) = data(j) j=j-1 Wend data(j + 1) = temp Next i End Sub
Sub insertionsortDesc(ByRef data() As Integer, ByVal n As Integer) Dim i, j, temp As Integer For i = 2 To n temp = data(i) j=i-1 data(0) = temp While temp <>data(j) And j > 0 data(j + 1) = data(j) j=j-1 Wend data(j + 1) = temp Next i End Sub
Sub shellsort(ByRef data() As Integer, ByVal n As Integer) Dim i, j, jarak, temp As Integer jarak = Int(n / 2) While jarak > 0 For i = 1 To (n - jarak) j = i + jarak If data(i) > data(j) Then temp = data(i) data(i) = data(j) data(j) = temp End If Next i jarak = Int(jarak / 2) Wend End Sub
Sub shellsort(ByRef data() As Integer, ByVal n As Integer) Dim i, j, jarak, temp As Integer jarak = Int(n / 2) While jarak > 0 For i = 1 To (n - jarak) j = i + jarak If data(i) < data(j) Then temp = data(i) data(i) = data(j) data(j) = temp End If Next i jarak = Int(jarak / 2) Wend End Sub
Soal Tugas : 1. Analisa kode sumber sorting (fungsi yang atau telah Anda tulis, dan jelaskan
masing-masing method
prosedur)
dengan
menggambarkan
flowchart masing-masing method tersebut. 2. Bagaimana prinsip dasar selection sort ? 3. Jelaskan mekanisme pemanggilan suatu fungsi atau method secara rekursif ! 4. Sebutkan mekanisme sorting yang lain ! Jelaskan !
10