Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Sorting. Outline Pembagian algoritma sorting Algoritma sorting Paradigma Contoh Running Time.

Presentasi serupa


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

1 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 Pembagian Algoritma Sorting Metode Sorting dibedakan menjadi : Internal Sorting Comparison Based Address Calculation Transposition Insert & Keep Sorted Priority Queue Diminishing Increment Divide &Conquer ProxmapRadix Eksternal Sorting Bubble Sort Insertion sort Tree sort Selection sort Heap sort Quick sort Merge Sort Shell sort

8 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

9 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

10 Diminishing Increment Penukaran tempat sepasang elemen dengan jarak tertentu. Jarak antar elemen akan terus berkurang sampai dihasilkan keadaan terurut. Shell Sort Address 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

11 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.

12 Bubble Sort Example

13 Bubble Sort Algorithm

14 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

15 Selection Sort Example

16 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; }

17 Insertion Sort Example

18 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; }

19 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.

20 Merge Sort Example

21 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

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

23

24 Example (cont.)

25

26 Done!

27 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).

28 Quick Sort Algorithm Select a pivotPartition Recursive sort and merge the result

29 Quick Sort Example

30 Quick Sort Program

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

32 Shell Sort Example

33 Unsorted Delta-4 Subsequences

34 Sorted Delta-4 Subsequences

35 Unsorted Delta-2 Subsequences

36 Sorted Delta-2 Subsequences

37 Delta-1 Subsequences

38 Shell Sort Program

39 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

40 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

41 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]

42 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]

43 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

44 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


Download ppt "Sorting. Outline Pembagian algoritma sorting Algoritma sorting Paradigma Contoh Running Time."

Presentasi serupa


Iklan oleh Google