SlideShare a Scribd company logo
1 of 57
SORTING DATA (2)
Sesi 11
Outline
Metode Pohon Biner
Metode Radix
Metode Cepat
Metode Heap
2
Metode Pohon Biner (Binary Sort)
a. Ambil data pertama dan tempatkan sebagai root
b. Ambil data ke dua dan bandingkan dengan root, bila
nilai data kedua lebih kecil dari root maka tempatkan
sebagai anak kiri root, bila lebih besar tempatkan
sebagai anak kanan root
c. Ambil data berikutnya, bandingkan dengan root bila
lebih kecil dari root bandingkan dengan anak kiri , bila
lebih kecil maka akan menjadi anak kiri dari anak kiri
root. begitu juga untuk anak kanannya
d. Lakukan langkah c sampai data terakhir
e. Urutan pembacaannya (bila ascending ) adalah anak
kiri yang paling kiri, root, anak kanan paling kiri, anak
kanan
3
Metode Pohon Biner: Pseudocode
def treesort ()
read item
root = makebt(null, item, null)
while not end of data
read item
insert(root, item)
end while
inorder(root)
end
4
Urutkan secara ascending data : 8,6,4,5, 2,10,7
8
6
4
2
10
7
Hasil : 2, 4, 5, 7, 8, 10
Anak kiri yang paling kiri – root – anak kanan yang
paling kiri – root – anak kanan – root - dst
5
Metode Pohon Biner: Contoh 1
Metode Pohon Biner: Contoh 2
Urutkan huruf M, A, R, J, K, S, V.
Penyisipan menghasilkan
M
/ 
A R
 
J S
 
K V
Traverse in order => A J K M R S V 6
Metode Radix (Radix Sort)
• Radix sort biasa digunakan pada basis data
yang akan diurutkan melalui kunci yang berantai
• misalnya akan diurutkan menurut tanggal lahir, maka
harus dilakukan sort 3 langkah, pertama menurut
tahun, kemudian menurut bulan, dan terakhir menurut
tanggal
• Pada sortir angka, maka radix sort diterapkan
pada setiap digit
7
Metode Radix
• Radix sort mengurutkan data berdasarkan posisi
digit dalam angka atau karakter dalam string.
• Pengurutan bisa dilakukan dari posisi digit
terakhir/terkanan (LSD-least significant digit)
atau digit awal/terkiri (MSD-Most significant
digit).
• Radix sort digunakan untuk mengurutkan data
sebagai kelompok karakter atau string.
8
Metode Radix: Contoh 1
• Sebagai gambaran radix sort dapat digunakan untuk
mengurutkan string sebagai berikut:
ABC, XYZ, BWZ, AAC, RLT, JBX, RDT, KLT, AEO, TLJ
• Untuk mengurutkannya pertama diurutkan
berdasarkan karakter terakhir pada string dan
dikelompokkan berdasarkan karakter tersebut.
(ABC, AAC) (TLJ) (AEO) (RLT, RDT, KLT) (JBX) (XYZ,
BWZ)
• Kombinasikan hasil pengelompokan:
ABC, AAC, TLJ, AEO, RLT, RDT, KLT, JBX, XYZ, BWZ
9
Metode Radix: Contoh 1
• Selanjutnya kelompokkan berdasarkan urutan
abjad karater yang tengah
(AAC) (ABC, JBX) (RDT) (AEO) (TLJ, RLT, KLT)
(BWZ) (XYZ)
• Kombinasikan kembali hasilnya:
AAC, ABC, JBX, RDT, AEO, TLJ, RLT, KLT, BWZ, XYZ
10
Metode Radix: Contoh 1
• Kemudian kelompokan berdasarkan abjad
pertama dari string, diperoleh:
(AAC, ABC, AEO) (BWZ) (JBX) (KLT) (RLT, RDT)
(TLJ) (XYZ)
• Terakhir gabungkan hasil pengelompokan
sehingga diperoleh:
AAC, ABC, AEO, BWZ, JBX, KLT, RLT, RDT, TLJ, XYZ
11
Metode Radix: Contoh 2
• Disamping itu radix sort dapat juga digunakan untuk
mengurutkan bilangan.
– Contoh: Radix sort pada array suatu bilangan 4 digit
bilangan interger:
• Data awal:
0123, 2154, 0222, 0004, 0283, 1560, 1061, 2150
12
Metode Radix: Contoh 2
Sort berdasarkan digit terakhir:
[0] – (1560, 2150)
[1] – (1061)
[2] – (0222)
[3] – (0123, 0283)
[4] – (2154, 0004)
[5] – ( )
[6] – ( )
[7] – ( )
[8] – ( )
[9] – ( )
Kombinasikan hasilnya dan tulis ke penampung awal:
1560, 2150, 1061, 0222, 0123, 0283, 2154, 0004
13
Metode Radix: Contoh 2
Sort berdasarkan digit kedua dari kanan:
[0] – (0004)
[1] – ( )
[2] – (0222, 0123)
[3] – ( )
[4] – ( )
[5] – (2150, 2154)
[6] – (1560, 1061)
[7] – ( )
[8] – (0283)
[9] – ( )
Kombinasikan hasilnya dan tulis ke penampung awal:
0004, 0222, 0123, 2150, 2154, 1560, 1061, 0283
14
Metode Radix: Contoh 2
Sort berdasarkan digit ketiga dari kanan:
[0] – (0004, 1061)
[1] – (0123, 2150, 2154)
[2] – (0222, 0283)
[3] – ( )
[4] – ( )
[5] – (1560)
[6] – ( )
[7] – ( )
[8] – ( )
[9] – ( )
Kombinasikan hasilnya dan tulis ke penampung awal:
0004, 1061, 0123, 2150, 2154, 0222, 0283, 1560
15
Metode Radix: Contoh 2
Sort berdasarkan digit ke empat dari kanan:
[0] – (0004, 0123, 0222, 0283)
[1] – (1061, 1560)
[2] – (2150, 2154)
[3] – ( )
[4] – ( )
[5] – ( )
[6] – ( )
[7] – ( )
[8] – ( )
[9] – ( )
Kombinasikan hasilnya dan tulis ke penampung awal:
0004, 0123, 0222, 0283, 1061, 1560, 2150, 2154
16
Metode Cepat (Quick Short)
 Teknik Quick-sort berbasis teknik divide-and-
conquer, sbb:
◦ Divide : array A[p..r] dibagi menjadi A[p..q] dan A[q+1
..r] sehingga setiap elemen A[p..q] lebih kecil dari atau
sama dengan setiap elemen A[q+1..r]
◦ Conquer : subarray A[p..q] dan A[q+1..r] juga di-sort
menurut Quick-Sort
◦ Combine : pada akhirnya A[p..r] akan ber-urutan
17
Metode Cepat: Algoritma
Diberikan sebuah array dengan n elemen (misal
integer) :
• If array hanya berisi satu elemen, return
• Else
– Ambil satu elemen untuk digunakan sebagai pivot
(poros).
– Partisi elemen menjadi dua sub-array:
• Elemen kurang dari atau sama dengan nilai pivot
• Elemen lebih dari nilai pivot
– Quicksort dua sub-array
– Return hasil
18
Contoh
• Array dari n integer berikut ini akan diurutkan:
19
40 20 10 80 60 50 7 30 100
Pilih Elemen Pivot
Elemen pertama akan dipilih sebagai pivot
20
40 20 10 80 60 50 7 30 100
Partisi Array
Berdasarkan pivot yang dipilih, partisi elemen
array sehingga menjadi:
1. Satu sub-array yang berisi elemen-elemen >= pivot
2. Satu sub-array yang berisi elemen-elemen < pivot
Sub-array disimpan dalam array data asli.
Akan dilakukan looping dalam partisi, tukar elemen
di bawah/di atas nilai pivot.
21
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
40 20 10 80 60 50 7 30 100poros_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[poros]
++too_big_index
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
7 10 20 30 40 50 60 80 100
<= data[pivot] > data[pivot]
Hasil Partisi
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Metode Heap
 Proses Heap-Sort dapat dijelaskan sbb:
1. Representasikan Heap dengan n elemen dalam
sebuah array A[n]
2. Elemen root tempatkan pada A[1]
3. Elemen A[2i] adalah node kiri dibawah A[i]
4. Elemen A[2i+1] adalah node kanan dibawah A[i]
5. Contoh: array A=[16 14 10 8 7 9 3 2 4 1]
mewakili binary-heap sbb:
6. Ambil nilai root (terbesar) A[1..n-1] dan pertukarkan
dengan elemen terakhir dalam array, A[n]
7. Bentuk Heap dari (n-1) elemen, dari A[1] hingga A[n-1]
8. Ulangi langkah 6 dimana indeks terakhir berkurang
setiap langkah. 48
Metode Heap (Heap Sort)
• Binary heap digunakan sebagai struktur data dalam
algoritma Heap-Sort
• Sebagaimana diketahui, ketika suatu Heap
dibangun maka kunci utamanya adalah: node atas
selalu mengandung elemen lebih besar dari kedua
node dibawahnya
• Apabila elemen berikutnya ternyata lebih besar dari
elemen root, maka harus di “swap” dan lakukan:
proses “heapify” lagi
• Root dari suatu Heap sort mengandung elemen
terbesar dari semua elemen dalam Heap
49
Metode Heap: Algoritma
Heapsort terdiri dari 2 langkah algoritma:
• Step 1. Bentuk Heap – O(n)
– Bentuk pohon biner dengan N elemen sebagai input,
pastikan properti struktur heap tersimpan, dengan
kata lain buat pohon biner komplit.
– Heapify pohon biner dan pastikan memenuhi properti
Heap Order.
• Step 2. Lakukan operasi n deleteMax– O(log(n))
– Hapus elemen maksimum pada heap – yang berada
pada root, dan pindahkan elemen ini ke bagian akhir
array yang sudah terurut.
50
Metode Heap: Contoh 1
• Mulai dengan data array yang belum terurut
Representasi array:
Bentuk pohon biner:
21 15 25 3 5 12 7 19 45 2 9
21
15 25
3 5 12 7
19 45 2 9
Metode Heap: Contoh (Ascending)
Step 1: Heapify the binary tree
21
15 25
3 5 12 7
19 45 2 9
21
15 25
3 9 12 7
19 45 2 5
21
15 25
45 9 12 7
19 3 2 5
21
15 25
45 9 12 7
19 3 2 5
21
45 25
19 9 12 7
15 3 2 5
45
21 25
19 9 12 7
15 3 2 5
Step 2 – lakukan n – 1 deleteMax, dan ganti elemen terakhir pada heap dengan
yang pertama, kemudian lakukan re-heapify. Tempatkan elemen yang dihapus pada
node posisi akhir.
45
21 25
19 9 12 7
15 3 2 5
45 21 25 19 9 12 7 15 3 2 5
25
21 12
19 9 5 7
15 3 2
25 21 12 19 9 5 7 15 3 2 45
25
21 12
19 9 5 7
15 3 2
25 21 12 19 9 5 7 15 3 2 45
21
19 12
15 9 5 7
2 3
21 19 12 15 9 5 7 2 3 25 45
21
19 12
15 9 5 7
2 3
21 19 12 15 9 5 7 2 3 25 45 19 15 12 3 9 5 7 2 21 25 45
19 15 12 3 9 5 7 2 21 25 45 15 9 12 3 2 5 7 19 21 25 45
19
15 12
3 9 5 7
2
19
15 12
3 9 5 7
2
15
9 12
3 2 5 7
15
9 12
3 2 5 7
15 9 12 3 2 5 7 19 21 25 45 12 9 7 3 2 5 15 19 21 25 45
12
9 7
3 2 5
12
9 7
3 2 5
12 9 7 3 2 5 15 19 21 25 45 9 5 7 3 2 12 15 19 21 25 45
9
5 7
3 2
2 3 5 7 9 12 15 19 21 25 45
2
… dan seterusnya hingga diperoleh nilai akhir 
Metode Heap: Contoh 2
56
Referensi
• http://repository.binus.ac.id/content/T0454/T045428784.
ppt
• http://dosen.stiki.ac.id/anita/Algoritma/8%20PENGURUT
AN.ppt
• http://siraits.files.wordpress.com/2008/04/algodanstruktu
rdata8.ppt
• http://www.cs.bu.edu/fac/gkollios/cs113/Slides/quicksort.
ppt
• http://dosen.stiki.ac.id/anita/Algoritma/8%20PENGURUT
AN.ppt
• http://en.wikipedia.org/wiki/Heapsort
• http://www.cs.sjsu.edu/~lee/cs146/Heapsort.ppt
57

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Asd sesi sorting part2

  • 2. Outline Metode Pohon Biner Metode Radix Metode Cepat Metode Heap 2
  • 3. Metode Pohon Biner (Binary Sort) a. Ambil data pertama dan tempatkan sebagai root b. Ambil data ke dua dan bandingkan dengan root, bila nilai data kedua lebih kecil dari root maka tempatkan sebagai anak kiri root, bila lebih besar tempatkan sebagai anak kanan root c. Ambil data berikutnya, bandingkan dengan root bila lebih kecil dari root bandingkan dengan anak kiri , bila lebih kecil maka akan menjadi anak kiri dari anak kiri root. begitu juga untuk anak kanannya d. Lakukan langkah c sampai data terakhir e. Urutan pembacaannya (bila ascending ) adalah anak kiri yang paling kiri, root, anak kanan paling kiri, anak kanan 3
  • 4. Metode Pohon Biner: Pseudocode def treesort () read item root = makebt(null, item, null) while not end of data read item insert(root, item) end while inorder(root) end 4
  • 5. Urutkan secara ascending data : 8,6,4,5, 2,10,7 8 6 4 2 10 7 Hasil : 2, 4, 5, 7, 8, 10 Anak kiri yang paling kiri – root – anak kanan yang paling kiri – root – anak kanan – root - dst 5 Metode Pohon Biner: Contoh 1
  • 6. Metode Pohon Biner: Contoh 2 Urutkan huruf M, A, R, J, K, S, V. Penyisipan menghasilkan M / A R J S K V Traverse in order => A J K M R S V 6
  • 7. Metode Radix (Radix Sort) • Radix sort biasa digunakan pada basis data yang akan diurutkan melalui kunci yang berantai • misalnya akan diurutkan menurut tanggal lahir, maka harus dilakukan sort 3 langkah, pertama menurut tahun, kemudian menurut bulan, dan terakhir menurut tanggal • Pada sortir angka, maka radix sort diterapkan pada setiap digit 7
  • 8. Metode Radix • Radix sort mengurutkan data berdasarkan posisi digit dalam angka atau karakter dalam string. • Pengurutan bisa dilakukan dari posisi digit terakhir/terkanan (LSD-least significant digit) atau digit awal/terkiri (MSD-Most significant digit). • Radix sort digunakan untuk mengurutkan data sebagai kelompok karakter atau string. 8
  • 9. Metode Radix: Contoh 1 • Sebagai gambaran radix sort dapat digunakan untuk mengurutkan string sebagai berikut: ABC, XYZ, BWZ, AAC, RLT, JBX, RDT, KLT, AEO, TLJ • Untuk mengurutkannya pertama diurutkan berdasarkan karakter terakhir pada string dan dikelompokkan berdasarkan karakter tersebut. (ABC, AAC) (TLJ) (AEO) (RLT, RDT, KLT) (JBX) (XYZ, BWZ) • Kombinasikan hasil pengelompokan: ABC, AAC, TLJ, AEO, RLT, RDT, KLT, JBX, XYZ, BWZ 9
  • 10. Metode Radix: Contoh 1 • Selanjutnya kelompokkan berdasarkan urutan abjad karater yang tengah (AAC) (ABC, JBX) (RDT) (AEO) (TLJ, RLT, KLT) (BWZ) (XYZ) • Kombinasikan kembali hasilnya: AAC, ABC, JBX, RDT, AEO, TLJ, RLT, KLT, BWZ, XYZ 10
  • 11. Metode Radix: Contoh 1 • Kemudian kelompokan berdasarkan abjad pertama dari string, diperoleh: (AAC, ABC, AEO) (BWZ) (JBX) (KLT) (RLT, RDT) (TLJ) (XYZ) • Terakhir gabungkan hasil pengelompokan sehingga diperoleh: AAC, ABC, AEO, BWZ, JBX, KLT, RLT, RDT, TLJ, XYZ 11
  • 12. Metode Radix: Contoh 2 • Disamping itu radix sort dapat juga digunakan untuk mengurutkan bilangan. – Contoh: Radix sort pada array suatu bilangan 4 digit bilangan interger: • Data awal: 0123, 2154, 0222, 0004, 0283, 1560, 1061, 2150 12
  • 13. Metode Radix: Contoh 2 Sort berdasarkan digit terakhir: [0] – (1560, 2150) [1] – (1061) [2] – (0222) [3] – (0123, 0283) [4] – (2154, 0004) [5] – ( ) [6] – ( ) [7] – ( ) [8] – ( ) [9] – ( ) Kombinasikan hasilnya dan tulis ke penampung awal: 1560, 2150, 1061, 0222, 0123, 0283, 2154, 0004 13
  • 14. Metode Radix: Contoh 2 Sort berdasarkan digit kedua dari kanan: [0] – (0004) [1] – ( ) [2] – (0222, 0123) [3] – ( ) [4] – ( ) [5] – (2150, 2154) [6] – (1560, 1061) [7] – ( ) [8] – (0283) [9] – ( ) Kombinasikan hasilnya dan tulis ke penampung awal: 0004, 0222, 0123, 2150, 2154, 1560, 1061, 0283 14
  • 15. Metode Radix: Contoh 2 Sort berdasarkan digit ketiga dari kanan: [0] – (0004, 1061) [1] – (0123, 2150, 2154) [2] – (0222, 0283) [3] – ( ) [4] – ( ) [5] – (1560) [6] – ( ) [7] – ( ) [8] – ( ) [9] – ( ) Kombinasikan hasilnya dan tulis ke penampung awal: 0004, 1061, 0123, 2150, 2154, 0222, 0283, 1560 15
  • 16. Metode Radix: Contoh 2 Sort berdasarkan digit ke empat dari kanan: [0] – (0004, 0123, 0222, 0283) [1] – (1061, 1560) [2] – (2150, 2154) [3] – ( ) [4] – ( ) [5] – ( ) [6] – ( ) [7] – ( ) [8] – ( ) [9] – ( ) Kombinasikan hasilnya dan tulis ke penampung awal: 0004, 0123, 0222, 0283, 1061, 1560, 2150, 2154 16
  • 17. Metode Cepat (Quick Short)  Teknik Quick-sort berbasis teknik divide-and- conquer, sbb: ◦ Divide : array A[p..r] dibagi menjadi A[p..q] dan A[q+1 ..r] sehingga setiap elemen A[p..q] lebih kecil dari atau sama dengan setiap elemen A[q+1..r] ◦ Conquer : subarray A[p..q] dan A[q+1..r] juga di-sort menurut Quick-Sort ◦ Combine : pada akhirnya A[p..r] akan ber-urutan 17
  • 18. Metode Cepat: Algoritma Diberikan sebuah array dengan n elemen (misal integer) : • If array hanya berisi satu elemen, return • Else – Ambil satu elemen untuk digunakan sebagai pivot (poros). – Partisi elemen menjadi dua sub-array: • Elemen kurang dari atau sama dengan nilai pivot • Elemen lebih dari nilai pivot – Quicksort dua sub-array – Return hasil 18
  • 19. Contoh • Array dari n integer berikut ini akan diurutkan: 19 40 20 10 80 60 50 7 30 100
  • 20. Pilih Elemen Pivot Elemen pertama akan dipilih sebagai pivot 20 40 20 10 80 60 50 7 30 100
  • 21. Partisi Array Berdasarkan pivot yang dipilih, partisi elemen array sehingga menjadi: 1. Satu sub-array yang berisi elemen-elemen >= pivot 2. Satu sub-array yang berisi elemen-elemen < pivot Sub-array disimpan dalam array data asli. Akan dilakukan looping dalam partisi, tukar elemen di bawah/di atas nilai pivot. 21
  • 22. 40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 23. 40 20 10 80 60 50 7 30 100poros_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[poros] ++too_big_index
  • 24. 40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index
  • 25. 40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index
  • 26. 40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index
  • 27. 40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index
  • 28. 40 20 10 80 60 50 7 30 100pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
  • 29. 40 20 10 30 60 50 7 80 100pivot_index = 0 too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 30. 40 20 10 30 60 50 7 80 100pivot_index = 0 too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 31. 40 20 10 30 60 50 7 80 100pivot_index = 0 too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 32. 40 20 10 30 60 50 7 80 100pivot_index = 0 too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 33. 40 20 10 30 60 50 7 80 100pivot_index = 0 too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 34. 40 20 10 30 60 50 7 80 100pivot_index = 0 too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 35. 40 20 10 30 60 50 7 80 100pivot_index = 0 too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 36. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 37. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 38. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 39. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 40. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 41. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 42. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 43. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 44. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 45. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index] 40 20 10 30 7 50 60 80 100pivot_index = 0 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 46. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index] 7 20 10 30 40 50 60 80 100pivot_index = 4 too_big_index too_small_index [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 47. 7 10 20 30 40 50 60 80 100 <= data[pivot] > data[pivot] Hasil Partisi [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 48. Metode Heap  Proses Heap-Sort dapat dijelaskan sbb: 1. Representasikan Heap dengan n elemen dalam sebuah array A[n] 2. Elemen root tempatkan pada A[1] 3. Elemen A[2i] adalah node kiri dibawah A[i] 4. Elemen A[2i+1] adalah node kanan dibawah A[i] 5. Contoh: array A=[16 14 10 8 7 9 3 2 4 1] mewakili binary-heap sbb: 6. Ambil nilai root (terbesar) A[1..n-1] dan pertukarkan dengan elemen terakhir dalam array, A[n] 7. Bentuk Heap dari (n-1) elemen, dari A[1] hingga A[n-1] 8. Ulangi langkah 6 dimana indeks terakhir berkurang setiap langkah. 48
  • 49. Metode Heap (Heap Sort) • Binary heap digunakan sebagai struktur data dalam algoritma Heap-Sort • Sebagaimana diketahui, ketika suatu Heap dibangun maka kunci utamanya adalah: node atas selalu mengandung elemen lebih besar dari kedua node dibawahnya • Apabila elemen berikutnya ternyata lebih besar dari elemen root, maka harus di “swap” dan lakukan: proses “heapify” lagi • Root dari suatu Heap sort mengandung elemen terbesar dari semua elemen dalam Heap 49
  • 50. Metode Heap: Algoritma Heapsort terdiri dari 2 langkah algoritma: • Step 1. Bentuk Heap – O(n) – Bentuk pohon biner dengan N elemen sebagai input, pastikan properti struktur heap tersimpan, dengan kata lain buat pohon biner komplit. – Heapify pohon biner dan pastikan memenuhi properti Heap Order. • Step 2. Lakukan operasi n deleteMax– O(log(n)) – Hapus elemen maksimum pada heap – yang berada pada root, dan pindahkan elemen ini ke bagian akhir array yang sudah terurut. 50
  • 51. Metode Heap: Contoh 1 • Mulai dengan data array yang belum terurut Representasi array: Bentuk pohon biner: 21 15 25 3 5 12 7 19 45 2 9 21 15 25 3 5 12 7 19 45 2 9
  • 52. Metode Heap: Contoh (Ascending) Step 1: Heapify the binary tree 21 15 25 3 5 12 7 19 45 2 9 21 15 25 3 9 12 7 19 45 2 5 21 15 25 45 9 12 7 19 3 2 5 21 15 25 45 9 12 7 19 3 2 5 21 45 25 19 9 12 7 15 3 2 5 45 21 25 19 9 12 7 15 3 2 5
  • 53. Step 2 – lakukan n – 1 deleteMax, dan ganti elemen terakhir pada heap dengan yang pertama, kemudian lakukan re-heapify. Tempatkan elemen yang dihapus pada node posisi akhir. 45 21 25 19 9 12 7 15 3 2 5 45 21 25 19 9 12 7 15 3 2 5 25 21 12 19 9 5 7 15 3 2 25 21 12 19 9 5 7 15 3 2 45 25 21 12 19 9 5 7 15 3 2 25 21 12 19 9 5 7 15 3 2 45 21 19 12 15 9 5 7 2 3 21 19 12 15 9 5 7 2 3 25 45
  • 54. 21 19 12 15 9 5 7 2 3 21 19 12 15 9 5 7 2 3 25 45 19 15 12 3 9 5 7 2 21 25 45 19 15 12 3 9 5 7 2 21 25 45 15 9 12 3 2 5 7 19 21 25 45 19 15 12 3 9 5 7 2 19 15 12 3 9 5 7 2 15 9 12 3 2 5 7
  • 55. 15 9 12 3 2 5 7 15 9 12 3 2 5 7 19 21 25 45 12 9 7 3 2 5 15 19 21 25 45 12 9 7 3 2 5 12 9 7 3 2 5 12 9 7 3 2 5 15 19 21 25 45 9 5 7 3 2 12 15 19 21 25 45 9 5 7 3 2 2 3 5 7 9 12 15 19 21 25 45 2 … dan seterusnya hingga diperoleh nilai akhir 
  • 57. Referensi • http://repository.binus.ac.id/content/T0454/T045428784. ppt • http://dosen.stiki.ac.id/anita/Algoritma/8%20PENGURUT AN.ppt • http://siraits.files.wordpress.com/2008/04/algodanstruktu rdata8.ppt • http://www.cs.bu.edu/fac/gkollios/cs113/Slides/quicksort. ppt • http://dosen.stiki.ac.id/anita/Algoritma/8%20PENGURUT AN.ppt • http://en.wikipedia.org/wiki/Heapsort • http://www.cs.sjsu.edu/~lee/cs146/Heapsort.ppt 57