460 likes | 867 Views
Sorting. Outline. Pembagian algoritma sorting Algoritma sorting Paradigma Contoh Running Time. Sorting. Sorting = pengurutan Sorted = terurut menurut kaidah tertentu Data pada umumnya disajikan dalam bentuk sorted Why?. A. A. B. B. C. C. D. D. E. E. F. F. G. G.
E N D
Outline • Pembagian algoritma sorting • Algoritma sorting • Paradigma • Contoh • Running Time
Sorting • Sorting = pengurutan • Sorted = terurut menurut kaidah tertentu • Data pada umumnya disajikan dalam bentuk sorted • Why?
A A B B C C D D E E F F G G • Sort by special key (s) ascending descending
A-C D-F G-I J-L M-O P-R S-U V-X Y-Z Efficient job ! A-C D-F G-I J-L M-O P-R S-U V-X Y-Z • Faster and easier in accessing data find “L”!
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.
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 Quick sort Merge Sort Shell sort Insertion sort Tree sort Bubble Sort Selection sort Heap sort
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
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
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
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.
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
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; } }
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; } }
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.
p q 2 4 5 7 L q + 1 r r p q 1 2 3 6 R 1 2 3 4 5 6 7 8 2 4 5 7 1 2 3 6 n2 n1 Merge Program 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 r Example: MERGE(A, 9, 12, 16)
Example (cont.) Done!
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).
Quick Sort Algorithm Select a pivot Partition Recursive sort and merge the result
Shell Sort • Ide: • Penukaran tempat sepasang elemen dengan jarak tertentu. • Jarak antar elemen akan terus berkurang sampai dihasilkan keadaan terurut.
0 1 2 3 4 5 6 7 8 9 10 11 40 2 1 43 3 65 0 -1 58 3 42 4 Shell Sort Example
0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 3 40 3 3 3 2 2 2 2 2 1 1 0 0 1 -1 43 43 43 43 40 3 40 40 40 65 65 65 65 65 0 1 1 0 0 -1 -1 4 -1 -1 58 58 58 58 58 3 3 3 3 3 42 42 42 42 42 4 43 4 4 4 Unsorted Delta-4 Subsequences
0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 3 3 3 3 3 2 2 2 2 2 0 0 0 0 0 -1 -1 -1 -1 -1 40 40 40 40 40 3 65 3 65 3 1 1 1 1 1 4 4 4 4 4 58 58 58 58 58 3 65 65 65 3 42 42 42 42 42 43 43 43 43 43 Sorted Delta-4 Subsequences
0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 11 0 3 0 2 -1 2 0 3 3 -1 -1 2 1 1 40 3 3 3 40 1 40 4 4 4 58 42 42 65 65 65 58 42 58 43 43 43 Unsorted Delta-2 Subsequences
0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 11 0 0 0 -1 -1 -1 3 1 1 2 2 2 3 3 1 3 3 3 40 40 40 4 4 4 42 42 42 65 65 43 58 58 58 43 43 65 Sorted Delta-2 Subsequences
0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 0 -1 0 -1 1 1 2 2 3 3 3 3 4 40 40 4 42 42 43 43 58 58 65 65 Delta-1 Subsequences
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
0 1 2 3 4 5 6 7 8 9 10 11 12 0.4 1.1 3.7 4.8 5.9 6.1 7.3 8.4 10.5 11.5 1.2 6.7 1.8 0 1 2 3 4 5 6 7 8 9 10 11 12 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 A[i] H[i] 1 3 0 1 1 1 2 1 1 0 1 1 0 Proxmap Example & Program Step 1: sorted linked list 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]++; • }
0 1 2 3 4 5 6 7 8 9 10 11 12 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 A[i] H[i] 1 3 0 1 1 1 2 1 1 0 1 1 0 P[i] 0 1 0 4 5 6 7 9 10 0 11 12 0 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]; • } • }
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])]; • } 0 1 2 3 4 5 6 7 8 9 10 11 12 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 A1[i] H[i] 1 3 0 1 1 1 2 1 1 0 1 1 0 P[i] 0 1 0 4 5 6 7 9 10 0 11 12 0 L[i] 7 6 10 1 9 4 12 1 5 0 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]
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
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