Struktur Data & Algoritme
(
Data Structures & Algorithms)
Denny (denny@cs.ui.ac.id) Suryana Setiawan (setiawan@cs.ui.ac.id)
Fakultas Ilmu Komputer Universitas Indonesia Semester Genap - 2004/2005
Version 2.0 - Internal Use Only Sorting
SDA/SORT/V2.0/2
Objectives
Memahami beberapa algoritme sorting dan dapat menganalisa kompleksitas-nya
SDA/SORT/V2.0/3
Outline
Beberapa algoritme untuk melakukan sorting
Idea Example
Running time for each algorithm
SDA/SORT/V2.0/4
Sort
Sorting = pengurutan
Sorted = terurut menurut kaidah tertentu
Data pada umumnya disajikan dalam bentuk sorted. Why?
Bayangkan bagaimana mencari telepon seorang teman dalam buku yang disimpan tidak terurut.
Bubble Sort: idea
bubble = busa/udara dalam air, so?
Busa dalam air akan naik ke atas. Why? How?
• Ketika busa naik ke atas, maka air yang di atasnya akan turun memenuhi tempat bekas busa tersebut.
Bubble Sort
40 2 1 43 3 65 0 -1 58 3 42 4 65 2 1 40 3 43 0 -1 58 3 42 4 65 58 1 2 3 40 0 -1 43 3 42 4 1 2 3 0 -1 40 3 42 4 43 58 65 1 2 3 4 Perhatikan bahwa pada setiap iterasi, dapat
SDA/SORT/V2.0/7 1 0 -1 2 3 3 4 40 42 43 58 65
Bubble Sort
0 -1 1 2 3 3 4 40 42 43 58 65 -1 0 1 2 3 3 4 40 42 43 58 65 6 7 8Stop here… why?
1 2 0 -1 3 3 40 4 42 43 58 65
5
SDA/SORT/V2.0/8
Bubble Sort: algorithms
Algorithm (see jdk1.5.0_01\demo\applets\SortDemo)
void sort(int a[]) throws Exception { for (int i = a.length; --i>=0; ) {
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; } } SDA/SORT/V2.0/9
Bubble Sort
Running time:Worst case: O(n2)
Best case: O(n) -- when? why? Variant:
bi-directional bubble sort
• original bubble sort: hanya bergerak ke satu arah • bi-directional bubble sort bergerak dua arah (bolak
balik).
see jdk1.5.0_01\demo\applets\SortDemo
SDA/SORT/V2.0/10
Selection Sort: idea
Ambil yang terbaik (select) dari suatu kelompok, kemudian diletakkan di belakang barisan
Lakukan terus sampai kelompok tersebut habis
SDA/SORT/V2.0/11 42 40 2 1 3 3 4 0 -1 43 58 65 40 2 1 43 3 4 0 -1 42 3 58 65 40 2 1 43 3 4 0 -1 58 3 42 65 40 2 1 43 3 65 0 -1 58 3 42 4
Selection Sort
SDA/SORT/V2.0/12 42 40 2 1 3 3 4 0 -1 43 58 65 42 -1 2 1 3 3 4 0 40 43 58 65 42 -1 2 1 3 3 0 4 40 43 58 65 42 -1 2 1 0 3 3 4 40 43 58 65Selection Sort
SDA/SORT/V2.0/13 1 42 -1 2 1 0 3 3 4 40 43 58 65 42 -1 0 2 3 3 4 40 43 58 65 1 42 -1 0 2 3 3 4 40 43 58 65 1 42 0 2 3 3 4 40 43 58 65 -1 1 42 0 2 3 3 4 40 43 58 65 -1
Selection Sort
SDA/SORT/V2.0/14Selection: algoritme
void sort(int a[]) throws Exception {
for (int i = 0; i < a.length; i++) { int min = i;
int j; /*
Find the smallest element in the unsorted list
*/ for (j = i + 1; j < a.length; j++) ... } if (a[j] < a[min]) { min = j; } ... } SDA/SORT/V2.0/15
Selection: algoritme (2)
/*Swap the smallest unsorted element into the end of the
sorted list. */ int T = a[min]; a[min] = a[i]; a[i] = T; ... } } SDA/SORT/V2.0/16
Selection Sort: analysis
Running time:
Worst case: O(n2) Best case: O(n2)
Based on big-oh analysis, is selection sort better than bubble sort?
Does the actual running time reflect the analysis?
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 40
Insertion Sort
Idea: mengurutkan kartu-kartu
1 2 3 40 43 65 0 -1 58 3 42 4 1 2 40 43 3 65 0 -1 58 3 42 4
1 2 3 40 43 65 0 -1 58 3 42 4
Insertion Sort
SDA/SORT/V2.0/19 1 2 3 40 43 65 0 -1 58 3 42 4 1 2 3 40 43 65 0 -1 58 3 42 4 1 2 3 40 43 65 0 0 1 2 3 40 43 65 58 3 42 4 -1
Insertion Sort
SDA/SORT/V2.0/20 1 2 3 40 43 65 0 0 1 2 3 40 43 58 65 3 42 4 -1 1 2 3 40 43 65 0 0 1 2 3 3 43 58 65 42 4 -1 40 43 58 65 1 2 3 40 43 65 0 0 1 2 3 3 43 42 65 4 -1 40 43 58 65Insertion Sort
1 2 3 40 43 65 0 0 1 2 3 3 43 42 65 -1 4 40 4342 5843 6558 65 SDA/SORT/V2.0/21Insertion Sort: ineffecient version
Insertion sort untuk mengurutkan array integer
public static void insertionSort (int[] a) {
for (int ii = 1; ii < a.length; ii++) { int jj = ii;
while (( jj > 0) && (a[jj] < a[jj - 1])) { int temp = a[jj];
a[jj] = a[jj - 1]; a[jj - 1] = temp; jj--; } } }
Perhatikan: ternyata nilai di a[jj] selalu sama⇒ dapat dilakukan efisiensi di sini.
SDA/SORT/V2.0/22
Insertion Sort
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; } } SDA/SORT/V2.0/23
Insertion Sort
Running time analysis:
Worst case: O(n2) Best case: O(n)
Is insertion sort faster than selection sort?
Notice the similarity and the difference between insertion sort and selection sort.
SDA/SORT/V2.0/24
Mergesort
Divide and Conquer approach
Idea:
Merging two sorted array takes O(n) time Split an array into two takes O(1) time
SDA/SORT/V2.0/25
Mergesort: Algorithm
If the number of items to sort is 0 or 1, return.
Recursively sort the first and second half separately.
Merge the two sorted halves into a sorted group.
SDA/SORT/V2.0/26 40 2 1 43 3 65 0 -1 58 3 42 4 40 2 1 43 3 65 0 -1 58 3 42 4 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
Mergesort
SDA/SORT/V2.0/27 40 2 1 43 3 65 0 -1 58 3 42 4 2 1 3 65 -1 58 42 4 40 1 2 43 3 65 0 -1 58 3 4 42 split merge 1 2 40 3 43 65 -1 0 58 3 4 42Mergesort
SDA/SORT/V2.0/28 merge 1 2 40 3 43 65 -1 0 58 3 4 42 1 2 3 40 43 65 -1 0 3 4 42 58 -1 0 1 2 3 3 4 40 42 43 58 62Mergesort
Merge Sort: implementation
Implement operation to merge two sorted arrays into one sorted array!
public static void merge (int [] array, int lo, int high)
// assume:
// mid = (lo + high) / 2;
// array [lo..mid] and [mid+1..high] are sorted {
Merge Sort: implementation
There are two ways to merge two sorted array:
in place merging using extra place merging
SDA/SORT/V2.0/31
Merge Sort: analysis
Running Time: O(n log n)
Why?
SDA/SORT/V2.0/32
Quicksort
Divide and Conquer approach
Quicksort(S) algorithm:
If the number of items in S is 0 or 1, return. Pick any element v in S. This element is called the
pivot.
Partition S – {v} into two disjoint groups:
• L = {x ∈ S – {v} | x ≤ v} and • R = {x ∈ S – {v} | x ≥ v}
Return the result of Quicksort(L), followed by v, followed by Quicksort(R). SDA/SORT/V2.0/33 40 2 1 3 43 65 0 -1 58 3 42 4
Quicksort: select pivot
SDA/SORT/V2.0/34 2 1 3 43 65 0 -1 3 58 42 4 40
Quicksort: partition
SDA/SORT/V2.0/35 2 1 3 43 65 0 -1 3 4 40 42 58 2 1 3 43 65 0 -1 3 4 40 42 58Quicksort: recursive sort & merge
the results
SDA/SORT/V2.0/36 40 2 1 43 3 65 0 -1 58 3 42 4 left 40 2 1 43 3 65 0 -1 58 3 42 4 40 2 1 3 65 0 -1 58 3 42 43 4 40 rightQuicksort: partition algorithm 1
SDA/SORT/V2.0/37 40 2 1 3 3 65 0 -1 58 42 43 4 40 2 1 3 3 0 -1 58 65 42 43 4 40 2 1 3 3 -1 0 58 65 42 43 4 40 2 1 3 3 -1 0 58 65 42 43 4 left right
Quicksort: partition algorithm 1
SDA/SORT/V2.0/38 -1 0 1 2 3 3 4 40 42 43 58 65 -1 1 3 3 2 42 2 1 3 0 -1 3 42 43 65 2 1 3 0 3 -1 4 43 42 58 65 40 2 1 43 3 65 0 -1 58 3 42 4 40 2 1 3 3 -1 0 58 65 42 43 4 3 3 2 3 2 3
Quicksort: partition algorithm 1
SDA/SORT/V2.0/39
Quicksort: partition algorithm 2
40 2 1 43 3 65 0 -1 58 3 42 4
40 original pivot =
40 left++while < pivot right--while >= pivot
4 2 1 43 3 65 0 -1 58 3 42 40 40 2 1 43 3 65 0 -1 58 3 42 4 right left 40 2 1 3 3 65 0 -1 58 43 42 4 right left left right SDA/SORT/V2.0/40
Quicksort: partition algorithm 2
40 2 1 3 3 65 0 -1 58 43 42 4 right left 40 2 1 3 3 -1 0 65 58 43 42 4 right left 40 2 1 3 3 -1 0 65 58 43 42 4 right left CROSSING! sort sortQuicksort: algoritme
static void QuickSort(int a[], int lo0, int hi0) { int lo = lo0; int hi = hi0; int pivot; // base case if ( hi0 <= lo0) { return; } pivot = a[lo0];
// loop through the array until indices cross while( lo <= hi ) {
/* find the first element that is greater than or equal to the partition element starting from the left Index.
*/
while( ( lo < hi0 ) && ( a[lo] < pivot )) { ++lo;
}
/* find an element that is smaller than the partition element starting from the right Index. */
while( ( hi > lo0 ) && ( a[hi] >= pivot )) { --hi;
}
// if the indexes have not crossed, swap if( lo <= hi ) {
swap(a, lo, hi); ++lo;
SDA/SORT/V2.0/43
/* If the right index has not reached the left side of array must now sort the left partition. */
if (lo0 < hi) {
QuickSort( a, lo0, hi ); }
/* If the left index has not reached the right side of array must now sort the right partition. */
if( lo < hi0 ) {
QuickSort( a, lo, hi0 ); } } SDA/SORT/V2.0/44
Quicksort: analysis
Partitioning takes O(n) Merging takes O(1) So, for each recursive call, the algorithm takes O(n)
How many recursive calls does a quick sort need?
SDA/SORT/V2.0/45
Quicksort: selecting pivot
Ideal pivot:
median element Common pivot
First element Element at the middle Median of three
SDA/SORT/V2.0/46
40 2 1 43 3 65 0 -1 58 3 42 4
Original:
5-sort: Sort setiap item yang berjarak 5:
40 2 1 43 3 65 0 -1 58 3 42 4
Shellsort
SDA/SORT/V2.0/47 40 2 1 43 3 65 0 -1 58 3 42 4 Original: 40 0 -1 43 3 42 2 1 58 3 65 4 After 5-sort: 2 0 -1 3 1 4 40 3 42 43 65 58 After 3-sort:Shellsort
After 1-sort: 1 2 3 40 43 65 0 0 1 2 3 3 43 42 65 -1 4 40 4342 5843 6558 65 SDA/SORT/V2.0/48 Shell's Odd Gaps Only Dividing by 2.21000 122 11 11 9 2000 483 26 21 23 4000 1936 61 59 54 8000 7950 153 141 114 16000 32560 358 322 269 32000 131911 869 752 575 64000 520000 2091 1705 1249 Shellsort N Insertion Sort
O(N3/2) O(N5/4) O(N7/6)
SDA/SORT/V2.0/49
Generic Sort
Bagaimana jika diperlukan method untuk mengurutkan array dari String, array dari Lingkaran(berdasarkan radiusnya) ?
Apakah mungkin dibuat suatu method yang bisa dipakai untuk semua jenis object?
Ternyata supaya object bisa diurutkan, harus bisa dibandingkan dengan object lainnya (mempunyai behavior bisa dibandingkan - comparable⇒ method).
Solusinya:
Gunakan interface yang mengandung method yang dapat membandingkan dua buah object.
SDA/SORT/V2.0/50
Generic Sort
REVIEW: Suatu kelas yang meng-implements sebuah interface, berarti kelas tersebut mewarisi interface (definisi method-method) = interface inheritance, bukan implementation inheritance
Dalam Java, terdapat interface java.lang.Comparable
method: int compareTo (Object o)
SDA/SORT/V2.0/51
Other kinds of sort
Heap sort. We will discuss this after tree.
Postman sort / Radix Sort.
etc.
SDA/SORT/V2.0/52
Further Reading
http://telaga.cs.ui.ac.id/WebKuliah/IKI101 00/resources/animation/
Chapter 8: Sorting Algorithm