Internal dan Eksternal Sorting
Outline Pembagian algoritma sorting Algoritma sorting Paradigma Contoh Running Time Outline
Sorting Sorting = pengurutan Sorted = terurut menurut kaidah tertentu Data pada umumnya disajikan dalam bentuk sorted Why? Sorting
Sort by special key (s) A B C D E F G ascending A B C D E F G descending
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 !
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. Why Sorting?
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. Internal Vs External
Metode Sorting berdasarkan kriteria sorting yang digunakan dibedakan menjadi :
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
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
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
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. Bubble Sort
Bubble Sort Example
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]; Bubble Sort Algorithm
Bubble Sort Running Time Kompleksitas: O(n2) Bubble Sort Running Time
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 Selection Sort
Selection Sort Example
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; Selection Sort Program
Selection Sort Running Time Worst case: O(n2) Best case: O(n2) Based on big-oh analysis, is selection sort better than bubble sort? Selection Sort Running Time
Ide: Mengurutkan kartu-kartu??? Insertion Sort
Insertion Sort Example
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; Insertion Sort Program
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. Insertion Sort Running Time
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. Merge Sort
Merge Sort Example
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) MergeSort program
Merge Program 7 5 4 2 6 3 1 p q r 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 1 2 3 4 5 6 7 8 p r q n1 n2 p q 7 5 4 2 6 3 1 r q + 1 L R Merge Program
Example: MERGE(A, 9, 12, 16) p r q
Example: MERGE(A, 9, 12, 16)
Example (cont.)
Example (cont.)
Example (cont.) Done!
Merge Sort Running Time (28 & 32)
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
Quick Sort Algorithm Select a pivot Partition Recursive sort and merge the result Quick Sort Algorithm
Quick Sort Example
Quick Sort Program
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(n2) log n for average case O(n log n) Quick Sort Running Time
Ide: Penukaran tempat sepasang elemen dengan jarak tertentu. Jarak antar elemen akan terus berkurang sampai dihasilkan keadaan terurut. Shell 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 Proxmap Sort
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]
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]
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]
Worst case O(n2) all keys in A have an equal value. Best case O(n) Proxmap Running Time
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
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: 223 3: 323 4: 413,416 5: 516 6: 616,626 7: 723 8: 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: Radix Sort Example
Radix Sort Running Time Worst case and best case = O(n) Radix Sort Running Time