1 / 7

Sorting – Lecture 3

Sorting – Lecture 3. More about Merge Sort , Quick Sort. Merge Sort: Time Complexity and Space Complexity. We have learned that the merge-sort has the time complexity O(n log(n))

billy
Download Presentation

Sorting – Lecture 3

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. Sorting – Lecture 3 More about Merge Sort , Quick Sort

  2. Merge Sort: Time Complexity and Space Complexity • We have learned that the merge-sort has the time complexity O(n log(n)) • Let’s focus on the space (memory usage) of the merge-sort. In order to understand the memory usage, lets workout the following array sort using the merge-sort • A={100, -1, 24, 3, 102, 89}; • From our space analysis, it is clear that the merge-sort need additional space in the order of O(n). So, the merge-sort need an auxiliary array.

  3. Quick Sort - Algorithm The quick sort uses the strategy of divide and conquer like merge sort. Its idea is to rearrange the element in the range so that no element in the first sub-array is larger than any element in the last sub-array. i.e., all elements in the first sub-array are smaller or equals to a pivot value, and all elements in the last sub-array are greater or equals to a pivot value. There are several ways to pick a pivot value, but one way is to pick the first element of a given array. If we have an array containing: {5 3 2 6 7 1 3 4} And if we use “4” (the last element of this array) as a pivot to partition, we can arrange it into two sub arrays as: {3 3 2 1 } {6 5 7} (this can be viewed as one array {3 3 2 1 4 6 5 7}) The first sub-array contains elements smaller than 4, and the second sub-array contains elements greater 5. We can repeat the similar partitioning process on two sub arrays (to get four sub-arrays, and so on). At the end, we will have a sorted array.

  4. Quick Sort - Workout Let’s workout quick sort for the following array A={100, -1, 24, 3, 102, 89};

  5. Quick Sort: Program public static int partition(int[] b, int start, int end) { int pivot=b[end]; intpindex=start; for(inti=start; i<end ; i++) { if(b[i] <= pivot) { int temp=b[i]; b[i]=b[pindex]; b[pindex]=temp; pindex++; } } int temp=b[pindex]; b[pindex]=b[end]; b[end]=temp; return pindex; } public static void qsort(int[] a, int start, int end) { if(start < end) { intpIndex=partition(a, start, end); qsort(a,start, pIndex-1); qsort(a,pIndex+1, end); } }

  6. Quick Sort – Time Complexity and Space Complexity Running Time Analysis of QuickSort Worst Case running time: O( n2 ) Worst Cases occur when the array is re-arranged into one sub-array containing only one element and the other sub-array containing the rest of elements in every partition process. This generates an extreme split and results in a slow performance. Average and Best Case running time: O( n log (n) ) The best case occurs when the array is re-arranged into two same (or almost same in case the length is odd) length sub arrays in every partition process. In this case, its running time is computed in almost same way as that of the Merge Sort. However, the constant/ coefficient of Quick Sort running time is much smaller than that of Merge Sort, thus Quick Sort runs much faster than Merge Sort in most cases.

  7. Quick Sort – Time Complexity and Space Complexity Space Analysis of QuickSort Quick sort is an in-place sorting algorithm (if we do not consider the stack memory for the recursive call). That is additional array space is not needed like the merge-sort. So, the quick sort is efficient in its space usage compared to the merge-sort. So, considering space and time complexity, QuickSort is the most efficient practical general purpose sorting algorithm. Most of the programming language sorting library functions use the quick sort.

More Related