- DAA - Discussion
- DAA - Useful Resources
- DAA - Quick Guide
- DAA - Hill Climbing Algorithm
- NP Hard & NP-Complete Classes
- DAA - Cook’s Theorem
- DAA - P and NP Class
- DAA - Vertex Cover
- DAA - Max Cliques
- Deterministic vs. Nondeterministic Computations
- DAA - Sublist Search
- DAA - Fibonacci Search
- DAA - Exponential Search
- DAA - Jump Search
- DAA - Interpolation Search
- DAA - Binary Search
- DAA - Linear Search
- Searching Techniques Introduction
- DAA - Radix Sort
- DAA - Counting Sort
- DAA - Bucket Sort
- DAA - Heap Sort
- DAA - Shell Sort
- DAA - Selection Sort
- DAA - Insertion Sort
- DAA - Bubble Sort
- DAA - Extract Method
- DAA - Heapify Method
- DAA - Insert Method
- DAA - Binary Heap
- Optimal Cost Binary Search Trees
- DAA - Multistage Graph
- DAA - Shortest Paths
- DAA - Spanning Tree
- Travelling Salesperson Approximation Algorithm
- Set Cover Problem
- Vertex Cover Problem
- Approximation Algorithms
- Fisher-Yates Shuffle
- Karger’s Minimum Cut
- Randomized Quick Sort
- Randomized Algorithms
- Travelling Salesman Problem | Dynamic Programming
- Longest Common Subsequence
- DAA - 0-1 Knapsack
- Floyd Warshall Algorithm
- Matrix Chain Multiplication
- DAA - Dynamic Programming
- DAA - Optimal Merge Pattern
- DAA - Job Sequencing with Deadline
- DAA - Fractional Knapsack
- Map Colouring Algorithm
- Dijkstra’s Shortest Path Algorithm
- Kruskal’s Minimal Spanning Tree
- Travelling Salesman Problem
- DAA - Greedy Method
- Towers of Hanoi
- Karatsuba Algorithm
- Strassen’s Matrix Multiplication
- DAA - Binary Search
- DAA - Merge Sort
- DAA - Max-Min Problem
- DAA - Divide & Conquer
- DAA - Space Complexities
- Master’s Theorem
- Time Complexity
- Asymptotic Notations & Apriori Analysis
- DAA - Methodology of Analysis
- DAA - Analysis of Algorithms
- DAA - Introduction
- Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design and Analysis - Merge Sort
Merge sort is a sorting technique based on spanide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most used and approached algorithms.
Merge sort first spanides the array into equal halves and then combines them in a sorted manner.
To understand merge sort, we take an unsorted array as the following −
We know that merge sort first spanides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is spanided into two arrays of size 4.
This does not change the sequence of appearance of items in the original. Now we spanide these two arrays into halves.
We further spanide these arrays and we achieve atomic value which can no more be spanided.
Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to these psts.
We first compare the element for each pst and then combine them into another pst in a sorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target pst of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and 44 are placed sequentially.
In the next iteration of the combining phase, we compare psts of two data values, and merge them into a pst of found data values placing all in a sorted order.
After the final merging, the pst becomes sorted and is considered the final solution.
Merge Sort Algorithm
Merge sort keeps on spaniding the pst into equal halves until it can no more be spanided. By definition, if it is only one element in the pst, it is considered sorted. Then, merge sort combines the smaller sorted psts keeping the new pst sorted too.
Step 1 − if it is only one element in the pst, consider it already sorted, so return.
Step 2 − spanide the pst recursively into two halves until it can no more be spanided.
Step 3 − merge the smaller psts into new pst in sorted order.
Pseudocode
We shall now see the pseudocodes for merge sort functions. As our algorithms point out two main functions − spanide & merge.
Merge sort works with recursion and we shall see our implementation in the same way.
procedure mergesort( var a as array ) if ( n == 1 ) return a var l1 as array = a[0] ... a[n/2] var l2 as array = a[n/2+1] ... a[n] l1 = mergesort( l1 ) l2 = mergesort( l2 ) return merge( l1, l2 ) end procedure procedure merge( var a as array, var b as array ) var c as array while ( a and b have elements ) if ( a[0] > b[0] ) add b[0] to the end of c remove b[0] from b else add a[0] to the end of c remove a[0] from a end if end while while ( a has elements ) add a[0] to the end of c remove a[0] from a end while while ( b has elements ) add b[0] to the end of c remove b[0] from b end while return c end procedure
Example
In the following example, we have shown Merge-Sort algorithm step by step. First, every iteration array is spanided into two sub-arrays, until the sub-array contains only one element. When these sub-arrays cannot be spanided further, then merge operations are performed.
Analysis
Let us consider, the running time of Merge-Sort as T(n). Hence,
$$mathrm{Tleft ( n ight )=left{egin{matrix} c & if, nleq 1 \ 2, xTleft ( frac{n}{2} ight )+dxn &otherwise \ end{matrix} ight.}:where: c: and: d: are: constants$$
Therefore, using this recurrence relation,
$$Tleft ( n ight )=2^{i}, Tleft ( n/2^{i} ight )+icdot dcdot n$$
$$As,:: i=log: n,: Tleft ( n ight )=2^{log, n}Tleft ( n/2^{log, n} ight )+log, ncdot dcdot n$$
$$=ccdot n+dcdot ncdot log: n$$
$$Therefore,: : Tleft ( n ight ) = O(n: log: n ).$$
Example
Following are the implementations of this operation in various programming languages −
#include <stdio.h> #define max 10 int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 }; int b[10]; void merging(int low, int mid, int high){ int l1, l2, i; for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) { if(a[l1] <= a[l2]) b[i] = a[l1++]; else b[i] = a[l2++]; } while(l1 <= mid) b[i++] = a[l1++]; while(l2 <= high) b[i++] = a[l2++]; for(i = low; i <= high; i++) a[i] = b[i]; } void sort(int low, int high){ int mid; if(low < high) { mid = (low + high) / 2; sort(low, mid); sort(mid+1, high); merging(low, mid, high); } else { return; } } int main(){ int i; printf("List before sorting "); for(i = 0; i <= max; i++) printf("%d ", a[i]); sort(0, max); printf(" List after sorting "); for(i = 0; i <= max; i++) printf("%d ", a[i]); }
Output
List before sorting 10 14 19 26 27 31 33 35 42 44 0 List after sorting 0 10 14 19 26 27 31 33 35 42 44
#include <iostream> using namespace std; #define max 10 int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 }; int b[10]; void merging(int low, int mid, int high){ int l1, l2, i; for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) { if(a[l1] <= a[l2]) b[i] = a[l1++]; else b[i] = a[l2++]; } while(l1 <= mid) b[i++] = a[l1++]; while(l2 <= high) b[i++] = a[l2++]; for(i = low; i <= high; i++) a[i] = b[i]; } void sort(int low, int high){ int mid; if(low < high) { mid = (low + high) / 2; sort(low, mid); sort(mid+1, high); merging(low, mid, high); } else { return; } } int main(){ int i; cout << "List before sorting "; for(i = 0; i <= max; i++) cout<<a[i]<<" "; sort(0, max); cout<< " List after sorting "; for(i = 0; i <= max; i++) cout<<a[i]<<" "; }
Output
List before sorting 10 14 19 26 27 31 33 35 42 44 0 List after sorting 0 10 14 19 26 27 31 33 35 42 44
pubpc class Merge_Sort { static int a[] = { 16, 18, 12, 8, 22, 14, 9 }; static int b[] = new int[10]; static void merging(int low, int mid, int high) { int l1, l2, i; for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) { if(a[l1] <= a[l2]) b[i] = a[l1++]; else b[i] = a[l2++]; } while(l1 <= mid) b[i++] = a[l1++]; while(l2 <= high) b[i++] = a[l2++]; for(i = low; i <= high; i++) a[i] = b[i]; } static void sort(int low, int high) { int mid; if(low < high) { mid = (low + high) / 2; sort(low, mid); sort(mid+1, high); merging(low, mid, high); } else { return; } } pubpc static void main(String args[]) { int i; int n = a.length; System.out.println("List before sorting"); for(i = 0; i < n; i++) System.out.print(a[i] + " "); sort(0, n-1); System.out.println(" List after sorting"); for(i = 0; i < n; i++) System.out.print(a[i]+" "); } }
Output
List before sorting 16 18 12 8 22 14 9 List after sorting 8 9 12 14 16 18 22
def merge_sort(a, n): if n > 1: m = n // 2 #spanide the pst in two sub psts l1 = a[:m] n1 = len(l1) l2 = a[m:] n2 = len(l2) #recursively calpng the function for sub psts merge_sort(l1, n1) merge_sort(l2, n2) i = j = k = 0 while i < n1 and j < n2: if l1[i] <= l2[j]: a[k] = l1[i] i = i + 1 else: a[k] = l2[j] j = j + 1 k = k + 1 while i < n1: a[k] = l1[i] i = i + 1 k = k + 1 while j < n2: a[k]=l2[j] j = j + 1 k = k + 1 a = [16, 18, 12, 8, 22, 14, 9] n = len(a) print("List before Sorting") print(a) merge_sort(a, n) print("List after Sorting") print(a)
Output
List before Sorting [16, 18, 12, 8, 22, 14, 9] List after Sorting [8, 9, 12, 14, 16, 18, 22]Advertisements