struktur data algoritme data structures algorithms
play

Struktur Data & Algoritme ( Data Structures & Algorithms ) - PDF document

Struktur Data & Algoritme ( Data Structures & Algorithms ) Sorting Denny ( denny@cs.ui.ac.id ) Suryana Setiawan ( setiawan@cs.ui.ac.id ) Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5 Version 2 .0


  1. Struktur Data & Algoritme ( Data Structures & Algorithms ) Sorting Denny ( denny@cs.ui.ac.id ) Suryana Setiawan ( setiawan@cs.ui.ac.id ) Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5 Version 2 .0 - I nternal Use Only Objectives � Memahami beberapa algoritme sorting dan dapat menganalisa kompleksitas-nya SDA/ SORT/ V2.0/ 2 1

  2. Outline � Beberapa algoritme untuk melakukan sorting � Idea � Example � Running time for each algorithm SDA/ SORT/ V2.0/ 3 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. SDA/ SORT/ V2.0/ 4 2

  3. 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. SDA/ SORT/ V2.0/ 5 Bubble Sort 40 2 1 43 3 65 0 -1 58 3 42 4 1 2 2 1 40 3 43 0 -1 58 3 42 4 65 1 2 3 40 0 -1 43 3 42 4 58 65 3 4 1 2 3 0 -1 40 3 42 4 43 58 65 � Perhatikan bahwa pada setiap iterasi, dapat dipastikan satu elemen akan menempati tempat yang benar SDA/ SORT/ V2.0/ 6 3

  4. Bubble Sort 5 1 2 0 -1 3 3 40 4 42 43 58 65 1 0 -1 2 3 3 4 40 42 43 58 65 6 0 -1 1 2 3 3 4 40 42 43 58 65 7 8 -1 0 1 2 3 3 4 40 42 43 58 65 Stop here… why? SDA/ SORT/ V2.0/ 7 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/ 8 4

  5. Bubble Sort � Running time: � Worst case: O(n 2 ) � 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/ 9 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/ 10 5

  6. Selection Sort 40 2 1 43 3 65 0 -1 58 3 42 4 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 SDA/ SORT/ V2.0/ 11 Selection Sort 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 SDA/ SORT/ V2.0/ 12 6

  7. Selection Sort -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 SDA/ SORT/ V2.0/ 13 Selection: 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/ 14 7

  8. 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/ 15 Selection Sort: analysis � Running time: � Worst case: O(n 2 ) � Best case: O(n 2 ) � Based on big-oh analysis, is selection sort better than bubble sort? � Does the actual running time reflect the analysis? SDA/ SORT/ V2.0/ 16 8

  9. Insertion Sort � Idea: mengurutkan kartu-kartu 40 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 SDA/ SORT/ V2.0/ 17 Insertion Sort 1 2 40 43 3 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 -1 58 3 42 4 SDA/ SORT/ V2.0/ 18 9

  10. Insertion Sort 1 2 3 40 43 65 0 -1 58 3 42 4 0 1 2 3 40 43 65 -1 58 3 42 4 -1 0 1 0 2 1 3 2 40 3 40 43 43 65 65 58 3 42 4 SDA/ SORT/ V2.0/ 19 Insertion Sort -1 0 0 1 2 1 3 2 40 3 40 43 43 65 58 65 3 42 4 -1 0 1 0 1 2 2 3 40 3 43 3 65 43 40 43 58 58 65 65 42 4 -1 0 1 0 1 2 2 3 40 3 43 3 40 43 65 42 65 43 58 65 4 -1 0 1 0 2 1 2 3 40 3 43 3 43 65 4 42 40 65 43 42 43 58 65 58 65 SDA/ SORT/ V2.0/ 20 10

  11. Insertion 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/ 21 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/ 22 11

  12. Insertion Sort � Running time analysis: � Worst case: O(n 2 ) � 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/ 23 Mergesort � Divide and Conquer approach � Idea: � Merging two sorted array takes O(n) time � Split an array into two takes O(1) time 1 2 3 40 43 65 -1 0 3 4 42 58 SDA/ SORT/ V2.0/ 24 12

  13. 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/ 25 Mergesort 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 SDA/ SORT/ V2.0/ 26 13

  14. Mergesort 40 2 1 43 3 65 0 -1 58 3 42 4 split 2 1 3 65 -1 58 42 4 40 1 2 43 3 65 0 -1 58 3 4 42 merge 1 2 40 3 43 65 -1 0 58 3 4 42 SDA/ SORT/ V2.0/ 27 Mergesort 1 2 40 3 43 65 -1 0 58 3 4 42 1 2 3 40 43 65 -1 0 3 4 42 58 merge -1 0 1 2 3 3 4 40 42 43 58 62 SDA/ SORT/ V2.0/ 28 14

  15. 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 { } SDA/ SORT/ V2.0/ 29 Merge Sort: implementation � There are two ways to merge two sorted array: � in place merging � using extra place merging SDA/ SORT/ V2.0/ 30 15

  16. Merge Sort: analysis � Running Time: O(n log n) � Why? SDA/ SORT/ V2.0/ 31 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/ 32 16

  17. Quicksort: select pivot 58 -1 4 2 42 3 43 40 0 1 3 65 SDA/ SORT/ V2.0/ 33 Quicksort: partition 65 2 42 0 3 40 4 43 -1 3 58 1 SDA/ SORT/ V2.0/ 34 17

  18. Quicksort: recursive sort & merge the results 42 43 58 65 -1 0 1 2 3 3 4 40 -1 0 1 2 3 3 4 40 42 43 58 65 SDA/ SORT/ V2.0/ 35 Quicksort: partition algorithm 1 40 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 SDA/ SORT/ V2.0/ 36 18

  19. Quicksort: 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 right SDA/ SORT/ V2.0/ 37 Quicksort: partition algorithm 1 40 2 1 43 3 65 0 -1 58 3 42 4 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 65 -1 1 3 3 2 42 2 3 3 2 3 3 -1 0 1 2 3 3 4 40 42 43 58 65 SDA/ SORT/ V2.0/ 38 19

  20. Quicksort: partition algorithm 2 original 40 40 2 1 43 3 65 0 -1 58 3 42 4 pivot = right-- while >= pivot left++ while < pivot 40 4 2 1 43 3 65 0 -1 58 3 42 40 left right 4 2 1 43 3 65 0 -1 58 3 42 40 left right 4 2 1 3 3 65 0 -1 58 43 42 40 left right SDA/ SORT/ V2.0/ 39 Quicksort: partition algorithm 2 4 2 1 3 3 65 0 -1 58 43 42 40 left right 4 2 1 3 3 -1 0 65 58 43 42 40 left right 4 2 1 3 3 -1 0 65 58 43 42 40 right left sort sort CROSSI NG! SDA/ SORT/ V2.0/ 40 20

More recommend