SlideShare una empresa de Scribd logo
1 de 49
Descargar para leer sin conexión
Struktur Data dan Algoritma

Sorting
Suryana Setiawan, Ruli Manurung & Ade Azurat
(acknowledgments: Denny)

Fasilkom UI

SUR – HMM – AA

Fasilkom UI - IKI20100/ IKI80110P

2009/2010 – Ganjil – Minggu 5
Outline


Beberapa algoritma untuk melakukan
sorting:









Bubble sort
Selection sort
Insertion sort
Shell sort
Merge sort
Quick sort

Untuk masing-masing algoritma:





Ide dasar
Contoh eksekusi
Algoritma
Analisa running time/kompleksitas

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

2
Sort





Sorting = pengurutan
Sorted = terurut menurut kaidah/aturan
tertentu
Data pada umumnya disajikan dalam bentuk
sorted.
Contoh:









Nama di buku telpon
Kata-kata dalam kamus
File-file di dalam sebuah directory
Indeks sebuah buku
Data mutasi rekening tabungan
CD di toko musik

Bayangkan jika data di atas tidak terurut!
SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

3
Bubble sort: Ide dasar


Bubble = busa/udara dalam air – apa yang
terjadi?





Busa dalam air akan naik ke atas. Mengapa?
Ketika busa naik ke atas, maka air yang di
atasnya akan turun memenuhi tempat bekas busa
tersebut.

Pada setiap iterasi, bandingkan elemen
dengan sebelahnya: yang busa naik, yang air
turun!

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

4
Bubble sort: Sebuah iterasi

40 40 40 43 43 65 65 65 58 65 42 65
2 2 1 3 3 0 -1 58 65 42 65 4
1
0 -1 3 3 4

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

5
Bubble sort: Iterasi berikutnya
1

40

2

1

43

3

65

0

-1

58

3

42

4

2

2

1

40

3

43

0

-1 58

3

42

4

65

3

1

2

3

40

0

-1 43

3

42

4

58 65

4

1

2

3

0

-1

40

42

4

43 58 65



3

Perhatikan bahwa pada setiap iterasi, dapat
dipastikan satu elemen akan menempati tempat
yang benar
SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

6
Bubble sort: Terminasi algoritma
5

1

2

0

-1

3

3

40

4

6

1

0

-1

2

3

3

4

40 42 43 58 65

7

0

-1

1

2

3

3

4

40 42 43 58 65

8

-1

0

1

2

3

3

4

40 42 43 58 65

42 43 58 65

Berhenti di sini! Mengapa?

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

7
Bubble sort: Algoritma
void sort(int a[]) throws Exception
{
for (int i = a.length; i>=0; i--) {
boolean swapped = false;
for (int j = 0; j<i; j++) {
...
if (a[j] > a[j+1]) {
int T = a[j];
a[j] = a[j+1];
a[j+1] = T;
swapped = true;
}
...
}
if (!swapped)
return;
}
}
SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

8
Bubble sort


Running time:





Worst case: O(n2)
Best case: O(n) – kapan? Mengapa?

Variasi:


bi-directional bubble sort
• original bubble sort: hanya bergerak ke satu
arah
• bi-directional bubble sort bergerak dua arah
(bolak balik)

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

9
Selection sort: Ide dasar


Kondisi awal:






Unsorted list = data
Sorted list = kosong

Ambil yang terbaik (select) dari unsorted list,
tambahkan di belakang sorted list.
Lakukan terus sampai unsorted list habis.

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

10
Selection sort: Contoh
40

2

1

43

3

65

0

-1 58

3

42

40

2

1

43

3

4

0

-1 58

3

42 65

40

2

1

43

3

4

0

-1 42

3

58 65

40

2

1

3

3

4

0

-1 42 43 58 65

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

4

2009/2010 – Ganjil – Minggu 5

11
Selection sort: Contoh (lanj.)

40

2

1

3

3

4

0

-1 42 43 58 65

-1

2

1

3

3

4

0

40 42 43 58 65

-1

2

1

3

3

0

4

40 42 43 58 65

-1

2

1

0

3

3

4

40 42 43 58 65

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

12
Selection sort: Contoh (lanj.)
-1

2

1

0

3

3

4

40 42 43 58 65

-1

0

1

2

3

3

4

40 42 43 58 65

-1

0

1

2

3

3

4

40 42 43 58 65

-1

0

1

2

3

3

4

40 42 43 58 65

-1

0

1

2

3

3

4

40 42 43 58 65

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

13
Selection sort: Algoritma
void sort(int a[]) throws Exceptionelemen
Cari
terkecil dari
{
unsorted {
for (int i = 0; i < a.length; i++) list.
int min = i;
for (int j = i + 1; j < a.length; j++)
if (a[j] < a[min])
min = j;
int T = a[min];
a[min] = a[i];
a[i] = T;
}
}
SUR – HMM – AA

Pindahkan ke
akhir sorted
list.

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

14
Selection sort: Analisis


Running time:






Worst case: O(n2)
Best case: O(n2)

Berdasarkan analisis big-oh, apakah
selection sort lebih baik dari bubble sort?
Apakah running time yang sebenarnya
merefleksikan analisis tersebut?

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

15
Insertion sort: Ide dasar


Kondisi awal:








Unsorted list = data
Sorted list = kosong

Ambil sembarang elemen dari unsorted list,
sisipkan (insert) pada posisi yang benar
dalam sorted list.
Lakukan terus sampai unsorted list habis.
Bayangkan anda mengurutkan kartu.

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

16
Insertion sort: Contoh

40

2

1

43

3

65

0

-1 58

3

42

4

2

40

1

43

3

65

0

-1 58

3

42

4

1

2

40 43

3

65

0

-1 58

3

42

4

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

17
Insertion sort: Contoh (lanj.)

1

2

40 43

1

2

1

2

SUR – HMM – AA

3

65

0

-1 58

3

42

4

3

40 43 65

0

-1 58

3

42

4

3

40 43 65

0

-1 58

3

42

4

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

18
Insertion sort: Contoh (lanj.)

1

2

3

40 43 65

0

1

2

-1
0

0
1

1
2

SUR – HMM – AA

0

-1 58

3

42

4

3

40 43 65 -1 58

3

42

4

2
3

40 43 65 65 58
3 40 43

3

42

4

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

19
Insertion sort: Contoh (lanj.)
-1
0

0
1

1
2

2
3

40 43 65 58 65
3 40 43

42

4

-1
0

0
1

1
2

2
3

40 43 65 58 65 65 42
3 3 43 43 58
40

4

-1
0

0
1

1
2

2
3

40 43 65 42 65 58 65
3 3 43
40
43

4

-1
0

0
1

1
2

2
3

40 43 65 42 65 58 65 65
3 3 43 40 43 43 58
4
42

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

3

2009/2010 – Ganjil – Minggu 5

20
Insertion sort: Algoritma


Insertion sort untuk mengurutkan array integer

Ambil
elemen
pertama
dalam
unsorted
while (( jj > 0) && (a[jj] < a[jj - 1])) {
list.
int temp = a[jj];
a[jj] = a[jj - 1];
Sisipkan ke
a[jj - 1] = temp;
dalam sorted
jj--;
list.
}

public static void insertionSort (int[] a)
{
for (int ii = 1; ii < a.length; ii++) {
int jj = ii;

}
}



Perhatikan: ternyata nilai di a[jj] selalu
sama ⇒ kita dapat melakukan efisiensi di
sini! – AA
2009/2010 – Ganjil – Minggu 5
SUR – HMM
Fasilkom UI – IKI20100/IKI80110P

21
Insertion sort: Algoritma (modif.)


Insertion sort yang lebih efisien:

public static void insertionSort2 (int[] a)
{
for (int ii = 1; ii < a.length; ii++) {
int temp = a[ii];
int jj = ii;
while (( jj > 0) && (temp < a[jj - 1])) {
a[jj] = a[jj - 1];
jj--;
}
a[jj] = temp;
}
}

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

22
Insertion sort: Analisis


Running time analysis:






Worst case: O(n2)
Best case: O(n)

Apakah insertion sort lebih cepat dari
selection sort?
Perhatikan persamaan dan perbedaan antara
insertion sort dan selection sort.

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

23
Terhantam tembok kompleksitas…






Bubble sort, Selection sort, dan Insertion sort
semua memiliki worst case sama: O(N2).
Ternyata, untuk algoritma manapun yang
pada dasarnya menukar elemen
bersebelahan (adjacent items), ini adalah
“best worst case”: Ω(N2)
Dengan kata lain, disebut lower bound
Bukti: Section 8.3 buku Weiss

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

24
Shell sort
Ide Donald Shell (1959): Tukarlah elemen yang
berjarak jauh!
Original:
40 2

1 43 3 65 0

-1 58 3 42 4

5-sort: Sort setiap item yang
berjarak 5:
40 2 1 43 3 65 0 -1 58 3 42 4

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

25
Shell sort
Original:
40 2

1 43 3 65 0

-1 58 3 42 4

-1 43 3 42 2

1 58 3 65 4

After 5-sort:
40 0
After 3-sort:
2

0

-1

3

1

4 40 3 42 43 65 58

1
2

2 3 3 43 40 43 43 58
3 40 43 65 42 65 58 65 65
4
42

After 1-sort:
-1
0

0
1

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

26
Shell sort: Gap values
Gap: jarak antara elemen yang di-sort.
 Seiring berjalannya waktu, gap
diperkecil. Shell sort juga dikenal
sebagai Diminishing Gap Sort.
 Shell mengusulkan mulai dengan
ukuran awal gap = N/2, dan dibagi 2
setiap langkah.
 Ada banyak variasi pemilihan gap.


SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

27
Kinerja Shell sort
Insertion
N
sort
1000
122
2000
483
4000
1936
8000
7950
16000
32560
32000
131911
64000
520000

Shell's
11
26
61
153
358
869
2091

Shell sort
Nilai gap ganjil
11
21
59
141
322
752
1705

O(N3/2 ) O(N5/4 )

Dibagi 2.2
9
23
54
114
269
575
1249

O(N7/6 )

Ada 3 “nested loop”, tetapi Shell sort masih lebih
baik dari Insertion sort. Mengapa?
SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

28
Merge sort: Ide dasar



Divide and conquer approach
Idenya:



1

Menggabungkan (merge) 2 sorted array butuh waktu O(n)
Membagi sebuah array jadi 2 hanya butuh waktu O(1)

2

3 40 43 65

CounterA

-1

1

-1

0

3

4 42 58

CounterB

0

2

3

3

4 40 42 43 58 65

Counterc
SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

29
Merge sort: Implementasi operasi
merge



Implementasikan method yang me-merge 2 sorted
array ke dalam 1 sorted array!
Asumsi: a dan b sudah ter-sort, |c| = |a|+|b|

public static void merge (int[] a, int[] b, int[] c)
{

}



Bisakah anda implementasikan
tanpa perlu temporary space?
SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

30
Merge sort: Algoritma rekursif




Base case: jika jumlah elemen dalam array
yang perlu di-sort adalah 0 atau 1.
Recursive case: secara rekursif, sort bagian
pertama dan kedua secara terpisah.
Penggabungan: Merge 2 bagian yang sudah
di-sort menjadi satu.

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

31
Merge sort: Contoh
40

2

1

43

3

65

0

-1 58

3

42

4

40

2

1

43

3

65

0

-1 58

3

42

4
split

40

2

1

43

3

65

0

-1 58

3

42

4

40

2

1

43

3

65

0

-1 58

3

42

4

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

32
Merge sort: Contoh (lanj.)

40

2

1

2

1

40

1

2

1

2

40

SUR – HMM – AA

43

3

65

0

3

65

43

3

65

3

43 65 -1

-1 58

3

0

4

42

-1 58

42

4

-1 58

3

4

42

0

3

4

split

42

58

Fasilkom UI – IKI20100/IKI80110P

merge

2009/2010 – Ganjil – Minggu 5

33
Merge sort: Contoh (lanj.)

1

2

40

3

43 65 -1

0

58

3

4

1

2

3

40 43 65 -1

0

3

4

42 58

-1

0

1

2

40 42 43 58 62

SUR – HMM – AA

3

3

4

Fasilkom UI – IKI20100/IKI80110P

42

merge

2009/2010 – Ganjil – Minggu 5

34
Merge sort: Analisis



Running Time: O(n log n)
Mengapa?

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

35
Quick sort: Ide dasar



Divide and conquer approach
Algoritma quickSort(S):






Jika jumlah elemen dalam S = 0 atau 1, return.
Pilih sembarang elemen v ∈ S – sebutlah pivot.
Partisi S – {v} ke dalam 2 bagian:
• L = {x ∈ S – {v} | x ≤ v}
• R = {x ∈ S – {v} | x ≥ v}
Kembalikan nilai quickSort(S), diikuti v, diikuti
quickSort(S).

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

36
Quick sort: Pilih elemen pivot

58

4

2

3
0

-1
42
43

40
1
65

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

3

2009/2010 – Ganjil – Minggu 5

37
Quick sort: Partition

65

2
0
-1

4

3

40

3

42
58

43

1

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

38
Quick sort: Sort scr. rekursif,
gabungkan

-1 0 1 2 3 3 4

40

42 43 58 65

-1 0 1 2 3 3 4 40 42 43 58 65

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

39
Quick sort: Partition algorithm 1

40

2

1

43

3

65

0

-1 58

3

42

4

left

right
4

2

1

43

3

65

0

-1 58

3

42 40

4

2

1

40

3

65

0

-1 58

3

42 43

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

40
Quick sort: Partition algorithm 1

4

2

1

3

3

65

0

-1 58 40 42 43

4

2

1

3

3

40

0

-1 58 65 42 43

4

2

1

3

3

-1

0

40 58 65 42 43

4

2

1

3

3

-1

0

40 58 65 42 43

left
SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

right
2009/2010 – Ganjil – Minggu 5

41
Quick sort: Partition algorithm 1
40

2

1

43

3

65

0

-1 58

4

2

1

3

3

-1

0

40 58 65 42 43

0

2

1

3

3

-1

4

43 42 58 65

-1

0

1

3

3

2

42 43

1

3

3

2

42

2

3

3

2

3

-1

3

42

4

65

3
-1

0

SUR – HMM – AA

1

2

3

3

4

40 42 43 58 65

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

42
Quick sort: Partition algorithm 2
Original: 40

2

1

pivot =
40

43

3

65

0

-1 58

3

42

4

3

42 40

“buang” pivot sementara

4

2

1

43

3

65

0

while < pivot left++

4

2

1

-1 58

while >= pivot right++

43

3

65

0

-1

left
4

2

1

3

42 40
right

3

3

65

0

left
SUR – HMM – AA

58

Fasilkom UI – IKI20100/IKI80110P

-1

58 43 42 40
right

2009/2010 – Ganjil – Minggu 5

43
Quick sort: Partition algorithm 2
4

2

1

3

3

65

0

-1

left
4

2

1

3

3

right
-1

0

left
4

2

1

3

3

sort (rekursif)

SUR – HMM – AA

58 43 42 40

-1

65 58 43 42 40
right

0

right

65 58 43 42 40
left

CROSSING!

Fasilkom UI – IKI20100/IKI80110P

sort (rekursif)

2009/2010 – Ganjil – Minggu 5

44
Quick sort: Implementasi
static void quickSort(int a[], int low, int high)
{
if(high <= low) return; // base case
pivot = choosePivot(a); // select “best” pivot
int i=low, j=high-1;
swap(a,pivot,a[j]);
// move pivot out of the way
while(i <= j)
{
// find large element starting from left
while(i<high && a[i]<pivot) i++;
// find small element starting from right
while(j>low && a[j]>=pivot) j--;
// if the indexes have not crossed, swap
if(i>j) swap(a, i, j);
}
swap(a,i,high-1);
quickSort(a,low,i-1);
quickSort(a,i+1,high);

// restore pivot
// sort small elements
// sort large elements

}
SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

45
Quick sort: Analisis





Proses partitioning butuh waktu: O(n)
Proses merging butuh waktu: O(1)
Untuk setiap recursive call, algoritma
quicksort butuh waktu: O(n)
Pertanyaannya: berapa recursive call
dibutuhkan untuk men-sort sebuah array?

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

46
Quick sort: Memilih pivot


Pivot ideal:




Elemen media

Yang biasa dijadikan calon pivot:





Elemen
Elemen
Elemen
Median

SUR – HMM – AA

pertama
terakhir
di tengah-tengah
dari ketiga elemen tsb.

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

47
Generic sort







Semua algoritma yang telah kita lihat mensort int.
Bagaimana jika kita ingin sort String?
DataMhs? CD?
Apa perlu dibuat method untuk setiap class?
Tidak!
Agar object bisa di-sort, mereka harus bisa
dibandingkan dengan object lainnya
(comparable).
Solusinya:


Gunakan interface yang mengandung method
yang dapat membandingkan dua buah object.

SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

48
Interface java.lang.comparable


Dalam Java, sifat generik “comparable” didefinisikan
dalam interface java.lang.comparable:
public interface Comparable
{
public int compareTo (Object ob);
}



Method compareTo returns:
 <0: object (this) “lebih kecil” dari
parameter ‘ob’
 0: object (this) “sama dengan”
parameter ‘ob’
 >0: object (this) “lebih besar” dari
SUR – HMM – AA

Fasilkom UI – IKI20100/IKI80110P

2009/2010 – Ganjil – Minggu 5

49

Más contenido relacionado

Destacado

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 HubspotMarius Sescu
 
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 ChatGPTExpeed Software
 
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 EngineeringsPixeldarts
 
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 HealthThinkNow
 
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.pdfmarketingartwork
 
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 2024Neil Kimberley
 
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)contently
 
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 2024Albert Qian
 
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 InsightsKurio // The Social Media Age(ncy)
 
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 2024Search Engine Journal
 
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 summarySpeakerHub
 
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 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 Tessa Mero
 
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 IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
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 managementMindGenius
 
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...RachelPearson36
 

Destacado (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...
 

Algoritma Sorting Data

  • 1. Struktur Data dan Algoritma Sorting Suryana Setiawan, Ruli Manurung & Ade Azurat (acknowledgments: Denny) Fasilkom UI SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P 2009/2010 – Ganjil – Minggu 5
  • 2. Outline  Beberapa algoritma untuk melakukan sorting:        Bubble sort Selection sort Insertion sort Shell sort Merge sort Quick sort Untuk masing-masing algoritma:     Ide dasar Contoh eksekusi Algoritma Analisa running time/kompleksitas SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 2
  • 3. Sort     Sorting = pengurutan Sorted = terurut menurut kaidah/aturan tertentu Data pada umumnya disajikan dalam bentuk sorted. Contoh:        Nama di buku telpon Kata-kata dalam kamus File-file di dalam sebuah directory Indeks sebuah buku Data mutasi rekening tabungan CD di toko musik Bayangkan jika data di atas tidak terurut! SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 3
  • 4. Bubble sort: Ide dasar  Bubble = busa/udara dalam air – apa yang terjadi?    Busa dalam air akan naik ke atas. Mengapa? Ketika busa naik ke atas, maka air yang di atasnya akan turun memenuhi tempat bekas busa tersebut. Pada setiap iterasi, bandingkan elemen dengan sebelahnya: yang busa naik, yang air turun! SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 4
  • 5. Bubble sort: Sebuah iterasi 40 40 40 43 43 65 65 65 58 65 42 65 2 2 1 3 3 0 -1 58 65 42 65 4 1 0 -1 3 3 4 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 5
  • 6. Bubble sort: Iterasi berikutnya 1 40 2 1 43 3 65 0 -1 58 3 42 4 2 2 1 40 3 43 0 -1 58 3 42 4 65 3 1 2 3 40 0 -1 43 3 42 4 58 65 4 1 2 3 0 -1 40 42 4 43 58 65  3 Perhatikan bahwa pada setiap iterasi, dapat dipastikan satu elemen akan menempati tempat yang benar SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 6
  • 7. Bubble sort: Terminasi algoritma 5 1 2 0 -1 3 3 40 4 6 1 0 -1 2 3 3 4 40 42 43 58 65 7 0 -1 1 2 3 3 4 40 42 43 58 65 8 -1 0 1 2 3 3 4 40 42 43 58 65 42 43 58 65 Berhenti di sini! Mengapa? SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 7
  • 8. Bubble sort: Algoritma void sort(int a[]) throws Exception { for (int i = a.length; i>=0; i--) { boolean swapped = false; for (int j = 0; j<i; j++) { ... if (a[j] > a[j+1]) { int T = a[j]; a[j] = a[j+1]; a[j+1] = T; swapped = true; } ... } if (!swapped) return; } } SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 8
  • 9. Bubble sort  Running time:    Worst case: O(n2) Best case: O(n) – kapan? Mengapa? Variasi:  bi-directional bubble sort • original bubble sort: hanya bergerak ke satu arah • bi-directional bubble sort bergerak dua arah (bolak balik) SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 9
  • 10. Selection sort: Ide dasar  Kondisi awal:     Unsorted list = data Sorted list = kosong Ambil yang terbaik (select) dari unsorted list, tambahkan di belakang sorted list. Lakukan terus sampai unsorted list habis. SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 10
  • 11. Selection sort: Contoh 40 2 1 43 3 65 0 -1 58 3 42 40 2 1 43 3 4 0 -1 58 3 42 65 40 2 1 43 3 4 0 -1 42 3 58 65 40 2 1 3 3 4 0 -1 42 43 58 65 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 4 2009/2010 – Ganjil – Minggu 5 11
  • 12. Selection sort: Contoh (lanj.) 40 2 1 3 3 4 0 -1 42 43 58 65 -1 2 1 3 3 4 0 40 42 43 58 65 -1 2 1 3 3 0 4 40 42 43 58 65 -1 2 1 0 3 3 4 40 42 43 58 65 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 12
  • 13. Selection sort: Contoh (lanj.) -1 2 1 0 3 3 4 40 42 43 58 65 -1 0 1 2 3 3 4 40 42 43 58 65 -1 0 1 2 3 3 4 40 42 43 58 65 -1 0 1 2 3 3 4 40 42 43 58 65 -1 0 1 2 3 3 4 40 42 43 58 65 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 13
  • 14. Selection sort: Algoritma void sort(int a[]) throws Exceptionelemen Cari terkecil dari { unsorted { for (int i = 0; i < a.length; i++) list. int min = i; for (int j = i + 1; j < a.length; j++) if (a[j] < a[min]) min = j; int T = a[min]; a[min] = a[i]; a[i] = T; } } SUR – HMM – AA Pindahkan ke akhir sorted list. Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 14
  • 15. Selection sort: Analisis  Running time:     Worst case: O(n2) Best case: O(n2) Berdasarkan analisis big-oh, apakah selection sort lebih baik dari bubble sort? Apakah running time yang sebenarnya merefleksikan analisis tersebut? SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 15
  • 16. Insertion sort: Ide dasar  Kondisi awal:      Unsorted list = data Sorted list = kosong Ambil sembarang elemen dari unsorted list, sisipkan (insert) pada posisi yang benar dalam sorted list. Lakukan terus sampai unsorted list habis. Bayangkan anda mengurutkan kartu. SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 16
  • 17. Insertion sort: Contoh 40 2 1 43 3 65 0 -1 58 3 42 4 2 40 1 43 3 65 0 -1 58 3 42 4 1 2 40 43 3 65 0 -1 58 3 42 4 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 17
  • 18. Insertion sort: Contoh (lanj.) 1 2 40 43 1 2 1 2 SUR – HMM – AA 3 65 0 -1 58 3 42 4 3 40 43 65 0 -1 58 3 42 4 3 40 43 65 0 -1 58 3 42 4 Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 18
  • 19. Insertion sort: Contoh (lanj.) 1 2 3 40 43 65 0 1 2 -1 0 0 1 1 2 SUR – HMM – AA 0 -1 58 3 42 4 3 40 43 65 -1 58 3 42 4 2 3 40 43 65 65 58 3 40 43 3 42 4 Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 19
  • 20. Insertion sort: Contoh (lanj.) -1 0 0 1 1 2 2 3 40 43 65 58 65 3 40 43 42 4 -1 0 0 1 1 2 2 3 40 43 65 58 65 65 42 3 3 43 43 58 40 4 -1 0 0 1 1 2 2 3 40 43 65 42 65 58 65 3 3 43 40 43 4 -1 0 0 1 1 2 2 3 40 43 65 42 65 58 65 65 3 3 43 40 43 43 58 4 42 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 3 2009/2010 – Ganjil – Minggu 5 20
  • 21. Insertion sort: Algoritma  Insertion sort untuk mengurutkan array integer Ambil elemen pertama dalam unsorted while (( jj > 0) && (a[jj] < a[jj - 1])) { list. int temp = a[jj]; a[jj] = a[jj - 1]; Sisipkan ke a[jj - 1] = temp; dalam sorted jj--; list. } public static void insertionSort (int[] a) { for (int ii = 1; ii < a.length; ii++) { int jj = ii; } }  Perhatikan: ternyata nilai di a[jj] selalu sama ⇒ kita dapat melakukan efisiensi di sini! – AA 2009/2010 – Ganjil – Minggu 5 SUR – HMM Fasilkom UI – IKI20100/IKI80110P 21
  • 22. Insertion sort: Algoritma (modif.)  Insertion sort yang lebih efisien: public static void insertionSort2 (int[] a) { for (int ii = 1; ii < a.length; ii++) { int temp = a[ii]; int jj = ii; while (( jj > 0) && (temp < a[jj - 1])) { a[jj] = a[jj - 1]; jj--; } a[jj] = temp; } } SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 22
  • 23. Insertion sort: Analisis  Running time analysis:     Worst case: O(n2) Best case: O(n) Apakah insertion sort lebih cepat dari selection sort? Perhatikan persamaan dan perbedaan antara insertion sort dan selection sort. SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 23
  • 24. Terhantam tembok kompleksitas…     Bubble sort, Selection sort, dan Insertion sort semua memiliki worst case sama: O(N2). Ternyata, untuk algoritma manapun yang pada dasarnya menukar elemen bersebelahan (adjacent items), ini adalah “best worst case”: Ω(N2) Dengan kata lain, disebut lower bound Bukti: Section 8.3 buku Weiss SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 24
  • 25. Shell sort Ide Donald Shell (1959): Tukarlah elemen yang berjarak jauh! Original: 40 2 1 43 3 65 0 -1 58 3 42 4 5-sort: Sort setiap item yang berjarak 5: 40 2 1 43 3 65 0 -1 58 3 42 4 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 25
  • 26. Shell sort Original: 40 2 1 43 3 65 0 -1 58 3 42 4 -1 43 3 42 2 1 58 3 65 4 After 5-sort: 40 0 After 3-sort: 2 0 -1 3 1 4 40 3 42 43 65 58 1 2 2 3 3 43 40 43 43 58 3 40 43 65 42 65 58 65 65 4 42 After 1-sort: -1 0 0 1 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 26
  • 27. Shell sort: Gap values Gap: jarak antara elemen yang di-sort.  Seiring berjalannya waktu, gap diperkecil. Shell sort juga dikenal sebagai Diminishing Gap Sort.  Shell mengusulkan mulai dengan ukuran awal gap = N/2, dan dibagi 2 setiap langkah.  Ada banyak variasi pemilihan gap.  SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 27
  • 28. Kinerja Shell sort Insertion N sort 1000 122 2000 483 4000 1936 8000 7950 16000 32560 32000 131911 64000 520000 Shell's 11 26 61 153 358 869 2091 Shell sort Nilai gap ganjil 11 21 59 141 322 752 1705 O(N3/2 ) O(N5/4 ) Dibagi 2.2 9 23 54 114 269 575 1249 O(N7/6 ) Ada 3 “nested loop”, tetapi Shell sort masih lebih baik dari Insertion sort. Mengapa? SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 28
  • 29. Merge sort: Ide dasar   Divide and conquer approach Idenya:   1 Menggabungkan (merge) 2 sorted array butuh waktu O(n) Membagi sebuah array jadi 2 hanya butuh waktu O(1) 2 3 40 43 65 CounterA -1 1 -1 0 3 4 42 58 CounterB 0 2 3 3 4 40 42 43 58 65 Counterc SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 29
  • 30. Merge sort: Implementasi operasi merge   Implementasikan method yang me-merge 2 sorted array ke dalam 1 sorted array! Asumsi: a dan b sudah ter-sort, |c| = |a|+|b| public static void merge (int[] a, int[] b, int[] c) { }  Bisakah anda implementasikan tanpa perlu temporary space? SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 30
  • 31. Merge sort: Algoritma rekursif    Base case: jika jumlah elemen dalam array yang perlu di-sort adalah 0 atau 1. Recursive case: secara rekursif, sort bagian pertama dan kedua secara terpisah. Penggabungan: Merge 2 bagian yang sudah di-sort menjadi satu. SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 31
  • 32. Merge sort: Contoh 40 2 1 43 3 65 0 -1 58 3 42 4 40 2 1 43 3 65 0 -1 58 3 42 4 split 40 2 1 43 3 65 0 -1 58 3 42 4 40 2 1 43 3 65 0 -1 58 3 42 4 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 32
  • 33. Merge sort: Contoh (lanj.) 40 2 1 2 1 40 1 2 1 2 40 SUR – HMM – AA 43 3 65 0 3 65 43 3 65 3 43 65 -1 -1 58 3 0 4 42 -1 58 42 4 -1 58 3 4 42 0 3 4 split 42 58 Fasilkom UI – IKI20100/IKI80110P merge 2009/2010 – Ganjil – Minggu 5 33
  • 34. Merge sort: Contoh (lanj.) 1 2 40 3 43 65 -1 0 58 3 4 1 2 3 40 43 65 -1 0 3 4 42 58 -1 0 1 2 40 42 43 58 62 SUR – HMM – AA 3 3 4 Fasilkom UI – IKI20100/IKI80110P 42 merge 2009/2010 – Ganjil – Minggu 5 34
  • 35. Merge sort: Analisis   Running Time: O(n log n) Mengapa? SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 35
  • 36. Quick sort: Ide dasar   Divide and conquer approach Algoritma quickSort(S):     Jika jumlah elemen dalam S = 0 atau 1, return. Pilih sembarang elemen v ∈ S – sebutlah pivot. Partisi S – {v} ke dalam 2 bagian: • L = {x ∈ S – {v} | x ≤ v} • R = {x ∈ S – {v} | x ≥ v} Kembalikan nilai quickSort(S), diikuti v, diikuti quickSort(S). SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 36
  • 37. Quick sort: Pilih elemen pivot 58 4 2 3 0 -1 42 43 40 1 65 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 3 2009/2010 – Ganjil – Minggu 5 37
  • 38. Quick sort: Partition 65 2 0 -1 4 3 40 3 42 58 43 1 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 38
  • 39. Quick sort: Sort scr. rekursif, gabungkan -1 0 1 2 3 3 4 40 42 43 58 65 -1 0 1 2 3 3 4 40 42 43 58 65 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 39
  • 40. Quick sort: Partition algorithm 1 40 2 1 43 3 65 0 -1 58 3 42 4 left right 4 2 1 43 3 65 0 -1 58 3 42 40 4 2 1 40 3 65 0 -1 58 3 42 43 SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 40
  • 41. Quick sort: Partition algorithm 1 4 2 1 3 3 65 0 -1 58 40 42 43 4 2 1 3 3 40 0 -1 58 65 42 43 4 2 1 3 3 -1 0 40 58 65 42 43 4 2 1 3 3 -1 0 40 58 65 42 43 left SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P right 2009/2010 – Ganjil – Minggu 5 41
  • 42. Quick sort: Partition algorithm 1 40 2 1 43 3 65 0 -1 58 4 2 1 3 3 -1 0 40 58 65 42 43 0 2 1 3 3 -1 4 43 42 58 65 -1 0 1 3 3 2 42 43 1 3 3 2 42 2 3 3 2 3 -1 3 42 4 65 3 -1 0 SUR – HMM – AA 1 2 3 3 4 40 42 43 58 65 Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 42
  • 43. Quick sort: Partition algorithm 2 Original: 40 2 1 pivot = 40 43 3 65 0 -1 58 3 42 4 3 42 40 “buang” pivot sementara 4 2 1 43 3 65 0 while < pivot left++ 4 2 1 -1 58 while >= pivot right++ 43 3 65 0 -1 left 4 2 1 3 42 40 right 3 3 65 0 left SUR – HMM – AA 58 Fasilkom UI – IKI20100/IKI80110P -1 58 43 42 40 right 2009/2010 – Ganjil – Minggu 5 43
  • 44. Quick sort: Partition algorithm 2 4 2 1 3 3 65 0 -1 left 4 2 1 3 3 right -1 0 left 4 2 1 3 3 sort (rekursif) SUR – HMM – AA 58 43 42 40 -1 65 58 43 42 40 right 0 right 65 58 43 42 40 left CROSSING! Fasilkom UI – IKI20100/IKI80110P sort (rekursif) 2009/2010 – Ganjil – Minggu 5 44
  • 45. Quick sort: Implementasi static void quickSort(int a[], int low, int high) { if(high <= low) return; // base case pivot = choosePivot(a); // select “best” pivot int i=low, j=high-1; swap(a,pivot,a[j]); // move pivot out of the way while(i <= j) { // find large element starting from left while(i<high && a[i]<pivot) i++; // find small element starting from right while(j>low && a[j]>=pivot) j--; // if the indexes have not crossed, swap if(i>j) swap(a, i, j); } swap(a,i,high-1); quickSort(a,low,i-1); quickSort(a,i+1,high); // restore pivot // sort small elements // sort large elements } SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 45
  • 46. Quick sort: Analisis     Proses partitioning butuh waktu: O(n) Proses merging butuh waktu: O(1) Untuk setiap recursive call, algoritma quicksort butuh waktu: O(n) Pertanyaannya: berapa recursive call dibutuhkan untuk men-sort sebuah array? SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 46
  • 47. Quick sort: Memilih pivot  Pivot ideal:   Elemen media Yang biasa dijadikan calon pivot:     Elemen Elemen Elemen Median SUR – HMM – AA pertama terakhir di tengah-tengah dari ketiga elemen tsb. Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 47
  • 48. Generic sort      Semua algoritma yang telah kita lihat mensort int. Bagaimana jika kita ingin sort String? DataMhs? CD? Apa perlu dibuat method untuk setiap class? Tidak! Agar object bisa di-sort, mereka harus bisa dibandingkan dengan object lainnya (comparable). Solusinya:  Gunakan interface yang mengandung method yang dapat membandingkan dua buah object. SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 48
  • 49. Interface java.lang.comparable  Dalam Java, sifat generik “comparable” didefinisikan dalam interface java.lang.comparable: public interface Comparable { public int compareTo (Object ob); }  Method compareTo returns:  <0: object (this) “lebih kecil” dari parameter ‘ob’  0: object (this) “sama dengan” parameter ‘ob’  >0: object (this) “lebih besar” dari SUR – HMM – AA Fasilkom UI – IKI20100/IKI80110P 2009/2010 – Ganjil – Minggu 5 49