1 / 44

DANDC

DANDC. wijanarto. Why SORT. Banyak di pakai dalam aplikasi Database  binary sort Computer graphic dan komputasi geometri Closest pair dan keunikan suatu elemen. Algoritma. Insertion dan selection sort : worst case O(n 2 ) Heap sort (quick sort) : worst case O(n log n).

borka
Download Presentation

DANDC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DANDC wijanarto

  2. Why SORT • Banyakdipakaidalamaplikasi • Database  binary sort • Computer graphic dankomputasigeometri • Closest pair dankeunikansuatuelemen

  3. Algoritma • Insertion dan selection sort : worst case O(n2) • Heap sort (quick sort) : worst case O(n log n)

  4. DIVEDE AND CONQUER • Divide • Bagimasalahaslimenjadiduaataulebih sub problem yang unik • Conquer • Gunakan DANDC secararekursifuntukmenyelesaikansubproblem • Combine • Ambilsolusidarisubproblemdan merge solusitersebutkedalamsolusiuntukmasalahaslinya

  5. Merge sort • Divide • Jika S memilikisetidaknya 2 elemen , pindahkanseluruhelemen S danletakandalam 2 urutan S1 dan S2, tiapurutanmengandungsetengahdari S • S1=n/2 elemendan s2= sisanya  n/2  elemen • Conquer • SORT s1 dan s1 dengan merge sort • Combine • Kembalikanelementadike S denganmenggabungkanurutan yang telahdi sort S1 dan S2 menjadi 1 urutan sort

  6. Algoritma Merge Sort P dan r adalah Bagiandari array A, yaitu Lower bound dan upper bound Merge-Sort(A,p,r) If p<r then q(p+r)/2 Merge-Sort(A,p,q) Merge-Sort(A,q+1,r) Merge (A,p,q,r) divide conquer combine Merge(A,p,q,r) Ambil 2 elementeratas yang terkecildari A[p..q] Dan A[q+1..r], laluletakankehasilpengurutan. Ulangiurutan (s1 dan s2) tersebuthinggakosong. Kopi hasilpengurutankedalam A[p..r]

  7. contoh 85 24 63 45 17 31 50 96 85 24 63 45 85 24 63 45 85 24 63 45 85 24 63 45

  8. Running algoritma 17 31 50 96 85 24 63 45 85 24 63 45 85 24 63 45 85 24 63 45 85 24 63 45

  9. Running algoritma 17 31 50 96 85 24 63 45 85 24 63 45 85 24 85 24 63 45 85 24 63 45 85 24 63 45

  10. Running algoritma 17 31 50 96 85 24 63 45 85 24 63 45 85 24 85 24 63 45 85 24 63 45 85 24 63 45 85

  11. Running algoritma 17 31 50 96 85 24 63 45 85 24 63 45 24 85 24 63 45 85 24 63 45 85 24 63 45 85

  12. Running algoritma 17 31 50 96 85 24 63 45 85 24 63 45 85 85 24 63 45 85 24 63 45 85 24 63 45 24

  13. Running algoritma 17 31 50 96 63 45 85 24 63 45 24 85 85 24 63 45 85 24 63 45 85 24 63 45

  14. Running algoritma 17 31 50 96 24 85 63 45 85 24 63 45 85 24 63 45 85 24 63 45 85 24 63 45

  15. Running algoritma 17 31 50 96 24 85 85 24 63 45 85 24 63 45 63 45 85 24 63 45 85 24 63 45

  16. Running algoritma 17 31 50 96 24 85 85 24 63 45 85 24 63 45 45 63 85 24 63 45 85 24 63 45

  17. Running algoritma 17 31 50 96 24 85 45 63 85 24 63 45 85 24 63 45 85 24 63 45 85 24 63 45

  18. Running algoritma 17 31 50 96 24 45 63 85 85 24 63 45 85 24 63 45 85 24 63 45 85 24 63 45

  19. Running algoritma 24 45 63 85 17 31 50 96 24 45 63 85 85 24 63 45 85 24 63 45 85 24 63 45 85 24 63 45

  20. Running algoritma 24 45 63 85 17 31 50 96 24 45 63 85 85 24 63 45 85 24 63 45 85 24 63 45 85 24 63 45 Langkahselanjutnyasamadenganbagianpertama

  21. Merging 2 seq array 24 45 63 85 a S1 17 31 50 96 S2 S 24 45 63 85 b S1 31 50 96 S2 17 S

  22. Merging 2 seq array 45 63 85 c S1 31 50 96 S2 17 24 S 45 63 85 d S1 50 96 S2 17 24 31 S

  23. Merging 2 seq array 63 85 e S1 50 96 S2 17 24 31 45 S 63 85 f S1 96 S2 17 24 31 45 50 S

  24. Merging 2 seq array 85 g S1 96 S2 17 24 31 45 50 63 S h S1 96 S2 17 24 31 45 50 63 85 S

  25. Merging 2 seq array i S1 S2 17 24 31 45 50 63 85 96 S

  26. Merge revisi Input 1 5 2 4 4 3 2 6 • Untuk men-SORT sejumlah n • Jika n=1 selesai • Reccuren sort 2 list sejumlah •  n/2  dan  n/2 elemen • Merge 2 list tsbdlm O(n) • Strategi • Pecahmasalahmjdsubproblem • ygmirip • Reccuren sub masalah • Combine solusi split 1 5 2 4 4 3 2 6 split split 1 5 2 4 4 3 2 6 split split split split 1 5 2 4 4 3 2 6 merge merge merge merge 1 5 2 4 4 3 2 6 merge merge 1 2 4 5 2 3 4 6 merge 1 2 2 3 4 4 6 6 output

  27. C implementation void mergesort(int numbers[], int temp[], intarray_size){ m_sort(numbers, temp, 0, array_size - 1); }  void m_sort(int numbers[], int temp[], int left, int right){ int mid; if (right > left) { mid = (right + left) / 2; m_sort(numbers, temp, left, mid); m_sort(numbers, temp, mid+1, right); merge(numbers, temp, left, mid+1, right); } }

  28. C implementation void merge(int numbers[], int temp[], int left, int mid, int right){ inti, left_end, num_elements, tmp_pos; left_end = mid - 1; tmp_pos = left; num_elements = right - left + 1; while ((left <= left_end) && (mid <= right)) { if (numbers[left] <= numbers[mid]) { temp[tmp_pos] = numbers[left]; tmp_pos = tmp_pos + 1; left = left +1; } else

  29. C implementation {temp[tmp_pos] = numbers[mid]; tmp_pos = tmp_pos + 1; mid = mid + 1; } } while (left <= left_end) { temp[tmp_pos] = numbers[left]; left = left + 1; tmp_pos = tmp_pos + 1; } while (mid <= right) { temp[tmp_pos] = numbers[mid]; mid = mid + 1; tmp_pos = tmp_pos + 1; } for (i=0; i <= num_elements; i++) { numbers[right] = temp[right]; right = right – 1; } }

  30. Quick Sort • Karakteristik • Sort dalamsatu “tempat”, tidakperlu array tambahan • Sangatpraktis, avarage case O(n log n), worst case O (n2)

  31. Prinsip Quick Sort • Melihatdeskripsi high level algorithm • DANDC • Divide • Partisi array mjd 2 sub array s.r.sdalambagian lower <= bagian higher • Conquer • Sort Recursive 2 sub array tadi • Combine • Sort selesaidi “tempat” tadi

  32. Partitioning (pembagian)wt=linear j i Partition(A,p,r) /*p & r batas array A*/ xA[p] ip-1 jr+1 While TRUE REPEAT jj-1 UNTIL A[j]>= x REPEAT ii+1 UNTIL A[i]<= x and i<=r if i>=j break; Tukar(A[i],A[j]) Tukar(A[p],A[j]) Return j p r i j i j j i Mengembalikanposisipartisidari array A

  33. Algoritma Quick Sort • PemanggilanQuicksort(A,1,pjg[A]) Quicksort(A,p,r) if p<r then qPartition(A,p,r) Quicksort(A,p,q-1) Quicksort(A,q+1,r) Partisi array A antara p dan r

  34. AnalisaQuicksort • Asumsikansemuaelemenberbeda • Running time tergantungdaridistribusispliting array • Splitingpartisiperluwaktu

  35. Best case n n n n/2 n/2 n/4 n/4 n n/4 n/4 n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 n lg n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 n 1 (n lg n) Running time tree adalahlg n, jadi total waktutempuhnyaadalah n lg n, Karenasetiap level pada tree adalah n, Worst case = (n2)

  36. Randomize Quicksort • Asumsisemuaelemenberbeda • Partisidipilihsecara random dalamelemen • Konsekuensinyasemua split (1:n-1,1:n-2,…, n-1:1) miripdenganprobabilitas 1/n • Randomisasimerupakanalatgeneralisasiuntukmeningkatkanalgoritmadengan worst case yang jelektetapibagusuntukavarage case

  37. Randomize Quicksort RandomizePartition(A,p,r) iRandom(p,r)//generate angka //random antara p dan r Tukar(A[r],A[i]) return Partition(A,p,r) RandomizeQS(A,p,r) if p<r then qRandomizePartition(A,p,r) RandomizeQS(A,p,r) RandomizeQS(A,q+1,r)

  38. Analisis Randomize Quicksort • Misal T(n), adalahwaktu yang dibutuhkandalamperbandingandalamquicksortsejumlah n • Karena split butuhprobabilitas 1/n, maka T(n) bernilai = T(i-1)+T(n-i)+n-1, jadi • Sehingga dg solving reccmjd O(n log n)

  39. Analisis Randomize Quicksort • Worstcase =O(n2) • Bestcase=O(n log n) • Expected running time quicksort O(n log n)

  40. Searching • Binary search • Interpolasi search

  41. Binary Search • t[1..n] data tersortirmenaik, t[i]t[j], dimana 1  i  j  n. • X adalahelemen yang dicaridalam t, jika x tidakadadalam t, makakitadapatmenyisipkannyake t. • Problem : mencari index I sedemikianrupasehingga 1  i  j  n dan t[i-1]< x  t[i]

  42. Binary Search dengan DANDC Binsearch (t[1..n],x) { if n==0 and x> t[n] then return n+1 else return Binrecc(t[1..n],x) } Binrecc (t[1..n],x) { /*mencari x dalam array t*/ /*untuk t[i-1]<x  t[j]*/ if i=j then return i k(i+j)\2 if x  t[k] then return Binrecc(t[1..k],x) else return Binrecc(t[k+1..j],x) }

  43. Binary Search dengan DANDC • Mencari x=12 dalam array t Binrecc (t[1..n],x) { if i=j then return i k(i+j)\2 if x  t[k] then return Binrecc(t[1..k],x) else return Binrecc(t[k+1..j],x) } 1 2 3 4 5 6 7 8 9 10 11 -5 -2 0 3 8 8 9 12 12 26 31 x t[k] ? i k j T i k j Y i k j Y i k j T i=j berhenti i j

  44. Interpolasi Search

More Related