International Islamic University H-10, Islamabad, Pakistan
Data Structures
Lecture No. 20
Merge Sort
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chishti
 Merge Sort is a complex and fast sorting algorithm that repeatedly divides an
unsorted section into two equal subsections, sorts them separately and
merges them correctly.
 Merge sort is a DIVIDE AND CONQUER algorithm. It divides input array in two
halves, calls itself for the two halves and then merges the two sorted halves.
The merge() function is used for merging two halves.
 Steps involved:
 Divide the unsorted collection into two until the sub-arrays only contain
one element
 Conquer: then merge the sub-problem solutions together
Introduction
MergeSort(arr[], L, R)
If r > l
1. Find the middle point to divide the array into two
halves: middle M = L + (R-L)/2
2. Call mergeSort for 1st half: Call mergeSort(arr, L, M)
3. Call mergeSort for 2nd half: Call mergeSort(arr, M+1,R)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, L, M, R)
Algorithm
Merge Sort Animation
Divide
Conquer
L=0 M=1 R=3
M = L + (R - L) / 2
L=0 M=3 R=6
M=0
L=0 R=1
L=0
R=0
L=1
R=1
M=2
L=2 M=3
#include <iostream>
#include <stdlib.h>
using namespace std;
void merge(int a[], int low, int high, int mid) {
int i, j, k, c[50];
i = low; k = low; j= mid + 1;
while (i <= mid&& j <= high) {
// Comparesthe elements of two subarrays
// and merges then
if (a[i] <a[j]) {
c[k] =a[i]; k++; i++;
}
else {
c[k] =a[j]; k++; j++;
}
}
while (i <= mid) { // Copies the remaining
// elements of left array, if there is any
c[k] = a[i]; k++; i++;
}
5
Example 1: Merge Sort
1
while (j <= high) { //Copies the
// remaining elements of right array,
// if there is any
c[k] = a[j]; k++; j++;
}
for (i = low; i < k; i++) {
a[i] = c[i];
}
}
void mergesort (int a[],int low, int high)
{
int mid;
if (low < high) {
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
}
2
int main() {
int i, arr[] = {38, 27, 43, 3, 9, 82, 10};
int sz = sizeof(arr) / sizeof(arr[0]);
cout<<"before sorting : ";
for (i = 0 ; i < sz ; i++)
cout <<" "<<arr[i];
mergesort(arr,0, sz-1);
cout<<"nSorted array : ";
for (i=0; i<sz; i++)
cout <<" "<< arr[i];
cout << endl;
system("PAUSE");
return 0;
}
6
Example 1: Merge Sort
3 4

Data Structures and Agorithm: DS 20 Merge Sort.pptx

  • 1.
    International Islamic UniversityH-10, Islamabad, Pakistan Data Structures Lecture No. 20 Merge Sort Engr. Rashid Farid Chishti http://youtube.com/rfchishti http://sites.google.com/site/chishti
  • 2.
     Merge Sortis a complex and fast sorting algorithm that repeatedly divides an unsorted section into two equal subsections, sorts them separately and merges them correctly.  Merge sort is a DIVIDE AND CONQUER algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves.  Steps involved:  Divide the unsorted collection into two until the sub-arrays only contain one element  Conquer: then merge the sub-problem solutions together Introduction
  • 3.
    MergeSort(arr[], L, R) Ifr > l 1. Find the middle point to divide the array into two halves: middle M = L + (R-L)/2 2. Call mergeSort for 1st half: Call mergeSort(arr, L, M) 3. Call mergeSort for 2nd half: Call mergeSort(arr, M+1,R) 4. Merge the two halves sorted in step 2 and 3: Call merge(arr, L, M, R) Algorithm
  • 4.
    Merge Sort Animation Divide Conquer L=0M=1 R=3 M = L + (R - L) / 2 L=0 M=3 R=6 M=0 L=0 R=1 L=0 R=0 L=1 R=1 M=2 L=2 M=3
  • 5.
    #include <iostream> #include <stdlib.h> usingnamespace std; void merge(int a[], int low, int high, int mid) { int i, j, k, c[50]; i = low; k = low; j= mid + 1; while (i <= mid&& j <= high) { // Comparesthe elements of two subarrays // and merges then if (a[i] <a[j]) { c[k] =a[i]; k++; i++; } else { c[k] =a[j]; k++; j++; } } while (i <= mid) { // Copies the remaining // elements of left array, if there is any c[k] = a[i]; k++; i++; } 5 Example 1: Merge Sort 1 while (j <= high) { //Copies the // remaining elements of right array, // if there is any c[k] = a[j]; k++; j++; } for (i = low; i < k; i++) { a[i] = c[i]; } } void mergesort (int a[],int low, int high) { int mid; if (low < high) { mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); } } 2
  • 6.
    int main() { inti, arr[] = {38, 27, 43, 3, 9, 82, 10}; int sz = sizeof(arr) / sizeof(arr[0]); cout<<"before sorting : "; for (i = 0 ; i < sz ; i++) cout <<" "<<arr[i]; mergesort(arr,0, sz-1); cout<<"nSorted array : "; for (i=0; i<sz; i++) cout <<" "<< arr[i]; cout << endl; system("PAUSE"); return 0; } 6 Example 1: Merge Sort 3 4