1 / 16

Pertemuan 20 Teknik Sort II

Pertemuan 20 Teknik Sort II. Matakuliah : T0016/Algoritma dan Pemrograman Tahun : 2005 Versi : versi 2. Learning Outcomes. Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Menjelaskan teknik quick sort dan merge sort. Outline Materi. Pengenalan quick sort

chesmu
Download Presentation

Pertemuan 20 Teknik Sort II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pertemuan 20Teknik Sort II Matakuliah : T0016/Algoritma dan Pemrograman Tahun : 2005 Versi : versi 2

  2. Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : • Menjelaskan teknik quick sort dan merge sort

  3. Outline Materi • Pengenalan quick sort • Pengenalan merge sort

  4. Quick Sort • The quick sort is an in-place, divide-and-conquer, 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.

  5. 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--;

  6. 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); }

  7. Demo Quick Sort • http://www.digsys.se/js_qsort.html • http://home.att.net/~srschmitt/quick_sort.html • http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/qsort.html

  8. Efisiensi Quick Sort

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

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

  11. 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); } }

  12. 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; } }

  13. 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; } }

  14. Demo Mergesort • http://max.cs.kzoo.edu/~abrady/java/sorting/MergeSort.html • http://www.geocities.com/SiliconValley/Program/2864/File/Merge1/mergesort.html

  15. Efisiensi Merge Sort

  16. Penutup • Quick sort merupakan teknik sort yang mudah dan cepat.

More Related