Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Internal dan Eksternal Sorting. Outline Pembagian algoritma sorting Algoritma sorting ◦ Paradigma ◦ Contoh ◦ Running Time.

Presentasi serupa


Presentasi berjudul: "Internal dan Eksternal Sorting. Outline Pembagian algoritma sorting Algoritma sorting ◦ Paradigma ◦ Contoh ◦ Running Time."— Transcript presentasi:

1 Internal dan Eksternal Sorting

2 Outline Pembagian algoritma sorting Algoritma sorting ◦ Paradigma ◦ Contoh ◦ Running Time

3 Sorting Sorting = pengurutan Sorted = terurut menurut kaidah tertentu Data pada umumnya disajikan dalam bentuk sorted Why?

4 Sort by special key (s) A B C D E F G A B C D E F G ascending descending

5 Faster and easier in accessing data  find “L”! A-C D-F G-IJ-L M-O P-RS-U V-X Y-Z A-C D-F G-IJ-L M-O P-RS-U V-X Y-Z Efficient job !

6 Why Sorting? Menyusun sekelompok elemen data yang tidak terurut menjadi terurut berdasarkan suatu kriteria tertentu. Mempermudah dan mempercepat proses pencarian data Jika pencarian data mudah, maka proses manipulasi data juga akan lebih cepat.

7 Internal Vs External Internal sorting:refers to the sorting of an array of data that is in RAM External sorting:refer to sorting methods that are employed when the data to be sorted is too large to fit in primary memory.

8 Metode Sorting Metode Sorting berdasarkan kriteria sorting yang digunakan dibedakan menjadi :

9 Transposition Didasarkan pada perbandingan elemen dan pertukaran posisi elemen Bubble Sort Insert & Keep Sorted Pemasukan sekumpulan data yang belum terurut ke dalam sekumpulan data yang sudah terurut. Mempertahankan keterurutan data yang sudah ada sebelumnya Insertion Sort, Tree Sort

10 Priority Queue Cari elemen yang sesuai dengan kriteria pencarian dari seluruh elemen yang ada (elemen prioritas). Tempatkan pada posisi yang sesuai Ulangi sampai semua elemen telah terurut Selection Sort, Heap Sort Divide & Conquer Pecah masalah ke dalam sub-sub masalah Sort masing-masing sub masalah Gabungkan masing-masing bagian Merge Sort, Quick Sort

11 Diminishing Increment Penukaran tempat sepasang elemen dengan jarak tertentu. Jarak antar elemen akan terus berkurang sampai dihasilkan keadaan terurut. Shell Sort Addres Calculation Membuat pemetaan atas key yang ingin di sortir,dimana pemetaan itu akan mengirimkan key tersebut ke lokasi yang paling mendekati final di output array Proxmap Sort dan Radix Sort

12 Bubble Sort Ide: bubble = busa/udara dalam air How? Busa dalam air akan naik ke atas. Ketika busa naik ke atas, maka air yang di atasnya akan turun memenuhi tempat bekas busa tersebut.

13 Bubble Sort Example

14 Bubble Sort Algorithm void bubbleSort(int numbers[], int n) { int i, j, bubble, temp; for (i = 0; i < n-1; i++) { bubble=numbers[0]; for (j = 1; j < n-i; j++) { if (bubble < numbers[j]){ numbers[j-1]=bubble; bubble=numbers[j]; } else numbers[j-1]=numbers[j]; } numbers[j-1]=bubble; }

15 Bubble Sort Running Time Running time:????? ◦Kompleksitas: O(n2)

16 Selection Sort Ide: Memilih nilai terkecil/terbesar dalam array (sesuai kriteria) dan ditempatkan pada posisi yang sesuai (Pegang index, telusuri nilai array yang sesuai untuk menempati index tersebut ). Lakukan terus sampai kelompok tersebut habis

17 Selection Sort Example

18 Selection Sort Program void selectionSort(int numbers[], int n) { int i, j; int min, temp; for (i = 0; i < n-1; i++) { min = i; for (j = i+1; j < n; j++) { if (numbers[j] < numbers[min]) min = j; } temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; }

19 Selection Sort Running Time Running time: ◦Worst case: O(n2) ◦Best case: O(n2) Based on big-oh analysis, is selection sort better than bubble sort?

20 Insertion Sort Ide: Mengurutkan kartu-kartu???

21 Insertion Sort Example

22 Insertion Sort Program void insertionSort(int numbers[], int n) { int i, j, temp; for (i=1; i < n; i++) { temp = numbers[i]; j = i; while ((j>0) && (numbers[j-1]>temp)) { numbers[j] = numbers[j-1]; j = j - 1; } numbers[j] = temp; }

23 Insertion Sort Running Time 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.

24 Merge Sort Divide and Conquer approach Ide: ◦Merging two sorted array takes O(n) time ◦Split an array into two takes O(1) time 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.

25 Merge Sort Example

26 MergeSort program Mergesort (elements) if (length <=1) return ; split(&first, &second) ; first = mergesort(first) ; second= mergesort(second) ; return merge(first, second) ; O(1) O(N) T(N/2) O(N)

27 Merge Program Alg.: MERGE(A, p, q, r) 1. Compute n 1 and n 2 2. Copy the first n 1 elements into L[1.. n 1 + 1] and the next n 2 elements into R[1.. n 2 + 1] 3. L[n 1 + 1] ←  ; R[n 2 + 1] ←  4. i ← 1; j ← 1 5. for k ← p to r 6. do if L[ i ] ≤ R[ j ] 7. then A[k] ← L[ i ] 8. i ←i else A[k] ← R[ j ] 10. j ← j + 1 pq rq + 1 L R   p r q n1n1 n2n2

28 Example: MERGE(A, 9, 12, 16) prq

29

30 Example (cont.)

31

32 Done!

33 Merge Sort Running Time (28 & 32)

34 Quick Sort 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).

35 Quick Sort Algorithm Select a pivot Partition Recursive sort and merge the result

36 Quick Sort Example

37 Quick Sort Program

38 Quick Sort Running Time 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? ◦n for worst case  if pivot is least or greatest key  O(n 2 ) ◦log n for average case  O(n log n)

39 Shell Sort Ide: ◦Penukaran tempat sepasang elemen dengan jarak tertentu. ◦Jarak antar elemen akan terus berkurang sampai dihasilkan keadaan terurut.

40 Proxmap Sort Idea: using a mapkey to locate the item in the proper place Algorithm: 1. Use mapkey to map the item into sorted linked list 2. Compute hit count H[i] 3. Compute Proxmap P[i] 4. Compute insertion location L[i] into A 2  output array

41 Proxmap Example & Program Step 1: sorted linked list /*compute hit counts, H[i], for each position, i, in A*/ for(i=0;i<13;++i) { j=MapKey(A[i]); H[j]++; } i A[i] H[i] Step 2: compute hit count

42 Proxmap Example & Program Step 3: compute proxmap /*convert hit counts to a proxmap*/ Position=0; for(i=0;i<13;++i) { if(H[i]>0) { P[i]=Position; Position+=H[i]; } i A[i] H[i] P[i]

43 Proxmap Example & Program Step 4: Compute insertion location L[i] into A 2  output array /*Compute insertion locations, L[i], for each key*/ for(i=0;i<13;++i) { L[i]=P[MapKey(A[i])]; } i A 1 [i] H[i] P[i] L[i] A 2 [i]

44 Proxmap Running Time Worst case O(n 2 )  all keys in A have an equal value. Best case O(n)

45 Radix Sort Idea:radix sort is a sorting algorithm that sorts integers by processing individual digitssorting algorithm Two classifications of radix sorts: least significant digit (LSD) radix sorts least significant digit most significant digit (MSD) radix sorts most significant digit LSD radix sorts process the integer representations starting from the least significant digit and move towards the most significant digit. MSD radix sorts work the other way around

46 Radix Sort Example Original: ◦516, 223, 323, 413, 416, 723, 813, 626, 616 Using Queues: Final Sorted:  223, 323, 413,416, 516, 616, 626, 723, 813 First Pass: 0: 1: 2: 3: 223, 323, 413, 723,813 4: 5: 6: 516, 416, 626, 616 7: 8: First Pass: 0: 1: 413, 813, 516, 416, 616 2: 223, 323, 723, 626 3: 4: 5: 6: 7: 8: First Pass: 0: 1: 2: 223 3: 323 4: 413,416 5: 516 6: 616,626 7: 723 8: 813

47 Radix Sort Running Time Worst case and best case = O(n)


Download ppt "Internal dan Eksternal Sorting. Outline Pembagian algoritma sorting Algoritma sorting ◦ Paradigma ◦ Contoh ◦ Running Time."

Presentasi serupa


Iklan oleh Google