Matakuliah Tahun Versi : T0016/Algoritma dan Pemrograman : 2005 : versi 2 Pertemuan 20 Teknik Sort II 1 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : • Menjelaskan teknik quick sort dan merge sort 2 Outline Materi • Pengenalan quick sort • Pengenalan merge sort 3 Quick Sort • The quick sort is an in-place, divide-andconquer, massively recursive sort. As a normal person would say, it's essentially a faster in-place version of the merge sort. • The efficiency of the algorithm is majorly impacted by which element is choosen as the pivot point. The worst-case efficiency of the quick sort, O(n2), occurs when the list is sorted and the left-most element is chosen. 4 Quick Sort void quickSort(int numbers[], int array_size) { q_sort(numbers, 0, array_size - 1); } void q_sort(int numbers[], int left, int right) { int pivot, l_hold, r_hold; l_hold = left; r_hold = right; pivot = numbers[left]; while (left < right) { while ((numbers[right] >= pivot) && (left < right)) right--; 5 Quick Sort if (left != right) { numbers[left] = numbers[right]; left++; } while ((numbers[left] <= pivot) && (left < right)) left++; if (left != right) { numbers[right] = numbers[left]; right--; } } numbers[left] = pivot; pivot = left; left = l_hold; right = r_hold; if (left < pivot) q_sort(numbers, left, pivot-1); if (right > pivot) q_sort(numbers, pivot+1, right); } 6 Demo Quick Sort • http://www.digsys.se/js_qsort.html • http://home.att.net/~srschmitt/quick_sort.ht ml • http://ciips.ee.uwa.edu.au/~morris/Year2/P LDS210/qsort.html 7 Efisiensi Quick Sort 8 Merge Sort • The merge sort splits the list to be sorted into two equal halves, and places them in separate arrays. Each array is recursively sorted, and then merged back together to form the final sorted list. Like most recursive sorts, the merge sort has an algorithmic complexity of O(n log n). • Elementary implementations of the merge sort make use of three arrays - one for each half of the data set and one to store the sorted list in. The below algorithm merges the arrays in-place, so only two arrays are required. 9 Merge Sort Like quicksort, merge sort uses recursion. The basic idea is as follows: • divide the array at its midpoint, and recursively apply merge sort to both halves. • make a single pass through both halves (which are now sorted), and merge them into one sorted whole. 10 Merge Sort void mergeSort(int numbers[], int temp[], int array_size) { m_sort(numbers, temp, 0, array_size - 1); } void m_sort(int numbers[], int temp[], int left, int right) { int mid; if (right > left) { mid = (right + left) / 2; m_sort(numbers, temp, left, mid); m_sort(numbers, temp, mid+1, right); merge(numbers, temp, left, mid+1, right); }} 11 Merge Sort void merge(int numbers[], int temp[], int left, int mid, int right) { int i, left_end, num_elements, tmp_pos; left_end = mid - 1; tmp_pos = left; num_elements = right - left + 1; while ((left <= left_end) && (mid <= right)) { if (numbers[left] <= numbers[mid]) { temp[tmp_pos] = numbers[left]; tmp_pos = tmp_pos + 1; left = left +1; } else { temp[tmp_pos] = numbers[mid]; tmp_pos = tmp_pos + 1; mid = mid + 1; }} 12 Merge Sort while (left <= left_end) { temp[tmp_pos] = numbers[left]; left = left + 1; tmp_pos = tmp_pos + 1; } while (mid <= right) { temp[tmp_pos] = numbers[mid]; mid = mid + 1; tmp_pos = tmp_pos + 1; } for (i=0; i <= num_elements; i++) { numbers[right] = temp[right]; right = right - 1; } } 13 Demo Mergesort • http://max.cs.kzoo.edu/~abrady/java/sortin g/MergeSort.html • http://www.geocities.com/SiliconValley/Pro gram/2864/File/Merge1/mergesort.html 14 Efisiensi Merge Sort 15 Penutup • Quick sort merupakan teknik sort yang mudah dan cepat. 16