The document provides an overview of Merge Sort and Quick Sort algorithms, detailing their mechanisms, advantages, disadvantages, and applications. Merge Sort is a stable sorting algorithm that uses a divide-and-conquer approach, while Quick Sort partitions the array around a pivot for sorting. Both algorithms are efficient but differ in space complexity and performance, with Merge Sort generally requiring more memory and Quick Sort being faster in practice.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
0 views
lecture 18 & 19
The document provides an overview of Merge Sort and Quick Sort algorithms, detailing their mechanisms, advantages, disadvantages, and applications. Merge Sort is a stable sorting algorithm that uses a divide-and-conquer approach, while Quick Sort partitions the array around a pivot for sorting. Both algorithms are efficient but differ in space complexity and performance, with Merge Sort generally requiring more memory and Quick Sort being faster in practice.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17
Merge sort and
quick sort BY ATTIA AGHA Merge sort
Merge sort is a sorting algorithm that follows the
divide-and-conquer approach. It works by recursively dividing the input array into smaller subarrays and sorting those subarrays then merging them back together to obtain the sorted array. In simple terms, we can say that the process of merge sort is to divide the array into two halves, sort each half, and then merge the sorted halves back together. This process is repeated until the entire array is sorted. How does Merge Sort work
Merge sort is a popular sorting algorithm known for its efficiency
and stability. It follows the divide-and-conquer approach to sort a given array of elements. Here’s a step-by-step explanation of how merge sort works: Divide: Divide the list or array recursively into two halves until it can no more be divided. Conquer: Each subarray is sorted individually using the merge sort algorithm. Merge: The sorted subarrays are merged back together in sorted order. The process continues until all elements from both subarrays have been merged. Illustration of Merge Sort: Let’s sort the array or list [38, 27, 43, 10] using Merge Sort Recurrence Relation of Merge Sort The recurrence relation of merge sort is: �(�)={Θ(1)if �=12�(�2)+Θ(�)if �>1T(n)={Θ(1)2T(2n)+Θ(n) if n=1if n>1 T(n) Represents the total time time taken by the algorithm to sort an array of size n. 2T(n/2) represents time taken by the algorithm to recursively sort the two halves of the array. Since each half has n/2 elements, we have two recursive calls with input size as (n/2). O(n) represents the time taken to merge the two sorted halves Complexity Analysis of Merge Sort Time Complexity: Best Case: O(n log n), When the array is already sorted or nearly sorted. Average Case: O(n log n), When the array is randomly ordered. Worst Case: O(n log n), When the array is sorted in reverse order. Auxiliary Space: O(n), Additional space is required for the temporary array used during merging. Advantages and Disadvantages of Merge Sort Stability : Merge sort is a stable sorting algorithm, which means it maintains the relative order of equal elements in the input array. Guaranteed worst-case performance: Merge sort has a worst-case time complexity of O(N logN) , which means it performs well even on large datasets. Simple to implement: The divide-and-conquer approach is straightforward. Naturally Parallel : We independently merge subarrays that makes it suitable for parallel processing. Disadvantages
Space complexity: Merge sort requires additional memory to
store the merged sub-arrays during the sorting process. Not in-place: Merge sort is not an in-place sorting algorithm, which means it requires additional memory to store the sorted data. This can be a disadvantage in applications where memory usage is a concern. Merge Sort is Slower than QuickSort in general as QuickSort is more cache friendly because it works in-place. Applications of Merge Sort:
Sorting large datasets
External sorting (when the dataset is too large to fit in memory) Inversion counting Merge Sort and its variations are used in library methods of programming languages. Its variation TimSort is used in Python, Java Android and Swift. The main reason why it is preferred to sort non-primitive types is stability which is not there in QuickSort. Arrays.sort in Java uses QuickSort while Collections.sort uses MergeSort. It is a preferred algorithm for sorting Linked lists. It can be easily parallelized as we can independently sort subarrays and then merge. The merge function of merge sort to efficiently solve the problems like union and intersection of two sorted arrays. Quick sort
QuickSort is a sorting algorithm based on the Divide and Conquer that
picks an element as a pivot and partitions the given array around the pickHow does QuickSort Algorithm work? QuickSort works on the principle of divide and conquer, breaking down the problem into smaller sub-problems. There are mainly three steps in the algorithm: 1. Choose a Pivot: Select an element from the array as the pivot. The choice of pivot can vary (e.g., first element, last element, random element, or median). 2. Partition the Array: Rearrange the array around the pivot. After partitioning, all elements smaller than the pivot will be on its left, and all elements greater than the pivot will be on its right. The pivot is then in its correct position, and we obtain the index of the pivot.ed pivot by placing the pivot in its correct position in the sorted array. 3. Recursively Call: Recursively apply the same process to the two partitioned sub-arrays (left and right of the pivot). 4. Base Case: The recursion stops when there is only one element left in the sub-array, as a single element is already sorted Illustration of QuickSort Algorithm In the previous step, we looked at how the partitioning process rearranges the array based on the chosen pivot. Next, we apply the same method recursively to the smaller sub-arrays on the left and right of the pivot. Each time, we select new pivots and partition the arrays again. This process continues until only one element is left, which is always sorted. Once every element is in its correct position, the entire array is sorted. Below image illustrates, how the recursive method calls for the smaller sub-arrays on the left and right of the pivot: