Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
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 ascending A B C D E F G
descending
5
Faster and easier in accessing data find “L”!
D-F G-I J-L M-O P-R S-U V-X Y-Z A-C D-F G-I J-L M-O P-R S-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 : Eksternal Sorting Internal Sorting Comparison Based Address Calculation Transposition Insert & Keep Sorted Priority Queue Divide &Conquer Diminishing Increment Proxmap Radix Bubble Sort Insertion sort Tree sort Quick sort Merge Sort Shell sort Selection sort Heap sort
8
Transposition Insert & Keep Sorted
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 Divide & Conquer
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 Lakukan terus sampai kelompok tersebut habis 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: Algorithm
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 7 5 4 2 6 3 1 p r q n1 n2 Alg.: MERGE(A, p, q, r)
8 p r q n1 n2 Alg.: MERGE(A, p, q, r) Compute n1 and n2 Copy the first n1 elements into L[1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1] L[n1 + 1] ← ; R[n2 + 1] ← i ← 1; j ← 1 for k ← p to r do if L[ i ] ≤ R[ j ] then A[k] ← L[ i ] i ←i + 1 else A[k] ← R[ j ] j ← j + 1 p q 7 5 4 2 6 3 1 r q + 1 L R
22
Example: MERGE(A, 9, 12, 16) p r q
23
Example: MERGE(A, 9, 12, 16)
24
Example (cont.)
25
Example (cont.)
26
Example (cont.) 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 pivot Partition
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 1 2 3 4 5 6 7 8 9 10 11 40 43 65 -1 58 42
33
Unsorted Delta-4 Subsequences
1 2 3 4 5 6 7 8 9 10 11 40 43 65 -1 58 42 1 2 3 4 5 6 7 8 9 10 11 43 40 65 -1 58 42 1 2 3 4 5 6 7 8 9 10 11 43 40 65 -1 58 42 1 2 3 4 5 6 7 8 9 10 11 43 40 65 -1 58 42 1 2 3 4 5 6 7 8 9 10 11 -1 40 65 58 42 43
34
Sorted Delta-4 Subsequences
1 2 3 4 5 6 7 8 9 10 11 -1 40 65 58 42 43 1 2 3 4 5 6 7 8 9 10 11 -1 40 65 58 42 43 1 2 3 4 5 6 7 8 9 10 11 -1 40 58 65 42 43 1 2 3 4 5 6 7 8 9 10 11 -1 40 58 65 42 43 1 2 3 4 5 6 7 8 9 10 11 -1 40 58 65 42 43
35
Unsorted Delta-2 Subsequences
1 2 3 4 5 6 7 8 9 10 11 -1 40 58 65 42 43 1 2 3 4 5 6 7 8 9 10 11 -1 40 42 65 58 43 1 2 3 4 5 6 7 8 9 10 11 -1 40 42 65 58 43
36
Sorted Delta-2 Subsequences
1 2 3 4 5 6 7 8 9 10 11 -1 40 42 65 58 43 1 2 3 4 5 6 7 8 9 10 11 -1 40 42 65 58 43 1 2 3 4 5 6 7 8 9 10 11 -1 40 42 43 58 65
37
Delta-1 Subsequences -1 40 42 43 58 65 -1 40 42 43 58 65 1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11 -1 40 42 43 58 65 1 2 3 4 5 6 7 8 9 10 11 -1 40 42 43 58 65
38
Shell Sort Program
39
Proxmap Sort Idea: using a mapkey to locate the item in the proper place Algorithm: Use mapkey to map the item into sorted linked list Compute hit count H[i] Compute Proxmap P[i] Compute insertion location L[i] into A2 output array
40
Proxmap Example & Program
Step 1: sorted linked list 1 2 3 4 5 6 7 8 9 10 11 12 0.4 1.1 1.2 1.8 3.7 4.8 5.9 6.1 6.7 7.3 8.4 10.5 11.5 Step 2: compute hit count /*compute hit counts, H[i], for each position, i, in A*/ for(i=0;i<13;++i) { j=MapKey(A[i]); H[j]++; } 1 2 3 4 5 6 7 8 9 10 11 6.7 5.9 8.4 1.2 7.3 3.7 11.5 1.1 4.8 0.4 10.5 6.1 i A[i] 12 1.8 H[i]
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]; } 1 2 3 4 5 6 7 8 9 10 11 6.7 5.9 8.4 1.2 7.3 3.7 11.5 1.1 4.8 0.4 10.5 6.1 i A[i] 12 1.8 H[i] P[i]
42
Proxmap Example & Program
Step 4: Compute insertion location L[i] into A2 output array /*Compute insertion locations, L[i], for each key*/ for(i=0;i<13;++i) { L[i]=P[MapKey(A[i])]; } i 1 2 3 4 5 6 7 8 9 10 11 12 A1[i] 6.7 5.9 8.4 1.2 7.3 3.7 11.5 1.1 4.8 0.4 10.5 6.1 1.8 H[i] 1 3 1 1 1 2 1 1 1 1 P[i] 1 4 5 6 7 9 10 11 12 L[i] 7 6 10 1 9 4 12 1 5 11 7 1 0.4 1.1 1.2 1.8 3.7 4.8 5.9 6.1 6.7 7.3 8.4 10.5 11.5 A2[i]
43
Radix Sort Idea:radix sort is a sorting algorithm that sorts integers by processing individual digits Two classifications of radix sorts: least significant digit (LSD) radix sorts most significant digit (MSD) radix sorts 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
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.