(Advanced Sorting Method) - Informatika Unsyiah · 2016-06-26 · Algoritma Quick Sort Jika...

Preview:

Citation preview

Quick Sort (Advanced Sorting Method)

Dr. Taufik Fuadi Abidin, S.Si., M.Tech Irvanizam Zamanhuri, M.Sc

Jurusan Informatika

FMIPA Universitas Syiah Kuala

www.informatika.unsyiah.ac.id/tfa www.informatika.unsyiah.ac.id/irvanizam

Bahan Ajar Struktur Data dan Algoritma

Outline Pertemuan Algoritma Quick Sort

Pivot dan Partisi

Ulangi Quick Sort Secara Rekursif

Analisis Kompleksitas Quick Sort

Kasus Terbaik (Best Case)

Kasus Terjelek (Worse Case)

Sumber: http://www.cs.bu.edu/fac/gkollios/cs113/Slides/quicksort.ppt

Algoritma Quick Sort Jika diketahui n buah data integer (dalam array)

Jika n=1, selesai

Else, pilih satu elemen sebagai pivot dan partisi data menjadi dua bagian sedemikian hingga elemen-elemen yang lebih besar atau sama dengan pivot berada di bagian sebelah kanan dan elemen-elemen yang lebih kecil berada dibagian sebelah kiri

Ulangi Quick Sort secara rekursif terhadap kedua sub bagian tersebut

Contoh Kasus Quick Sort

40 20 10 80 60 50 7 30 100

Tentukan Elemen Pivot

40 20 10 80 60 50 7 30 100

Partisi Array Berdasarkan Pivot Berdasarkan pivot, partisi data sedemikian hingga elemen-elemen yang lebih besar atau sama dengan pivot berada di bagian sebelah kanan dan elemen-elemen yang lebih kecil dari pivot berada dibagian sebelah kiri

40 20 10 80 60 50 7 30 100 pivot_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 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

While data[too_big_index] <= data[pivot]

++too_big_index

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

While data[too_big_index] <= data[pivot]

++too_big_index

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

While data[too_big_index] <= data[pivot]

++too_big_index

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

While data[too_big_index] <= data[pivot]

++too_big_index

While data[too_small_index] > data[pivot]

--too_small_index

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

While data[too_big_index] <= data[pivot]

++too_big_index

While data[too_small_index] > data[pivot]

--too_small_index

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

While data[too_big_index] <= data[pivot]

++too_big_index

While data[too_small_index] > data[pivot]

--too_small_index

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 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

While data[too_big_index] <= data[pivot]

++too_big_index

While data[too_small_index] > data[pivot]

--too_small_index

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 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

While data[too_big_index] <= data[pivot]

++too_big_index

While data[too_small_index] > data[pivot]

--too_small_index

If too_big_index < too_small_index

swap data[too_big_index] and data[too_small_index]

While too_small_index > too_big_index, go to 1.

40 20 10 30 60 50 7 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 60 50 7 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 60 50 7 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 60 50 7 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 60 50 7 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

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 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_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]

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 100 pivot_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]

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 100 pivot_index = 4

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

Hasil Partisi

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

Secara Rekursif: Quick Sort Kembali Sub Bagian

Analisis Quick Sort

Jika data random (acak) terdistribusi secara uniform maka pada kasus terbaik kompleksitas Quicksort adalah:

Kompleksitas mempartisi bagian array

Kompleksitas melakukan Quick Sort untuk setiap bagian

Kedalaman rekursif O(log2n)

Jumlak penentuan partisi O(n)

Jadi kompleksitas kasus terbaik adalah O(n log2n)

2 4 10 12 13 50 57 63 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Kasus Terjelek?

Data yang telah terurut, O(n2)

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]

2 4 10 12 13 50 57 63 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_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]

4. While too_small_index > too_big_index, go to 1.

5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

> data[pivot] <= data[pivot]

Analisis Quick Sort

Jika data random (acak) terdistribusi secara uniform maka pada kasus terjelek (data telah terurut) kompleksitas Quicksort adalah:

Kompleksitas mempartisi bagian array

Kompleksitas melakukan Quick Sort untuk setiap bagian

Kedalaman rekursif O(n)

Jumlak penentuan partisi O(n)

Jadi kompleksitas kasus terjelek adalah O(n2)

Cara Menghindari Kasus Terjelek

Pilih Pivot yang merupakan median dari data di posisi data[0], data[n/2], and data[n-1]

Silahkan dicoba pada contoh yang lain!

Questions & Discussion