0% found this document useful (0 votes)
207 views10 pages

Presentation On Merge Sort

Merge sort is a divide and conquer algorithm that divides an array into halves, recursively sorts the halves, and then merges the sorted halves back together. It works by dividing the array into single elements, sorting those, and then merging the sorted elements back together until the entire array is sorted. Merge sort runs in O(n log n) time and is useful for sorting large data sets because it requires very little additional memory and has good performance on most data sets.

Uploaded by

Saraswati Sharma
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
207 views10 pages

Presentation On Merge Sort

Merge sort is a divide and conquer algorithm that divides an array into halves, recursively sorts the halves, and then merges the sorted halves back together. It works by dividing the array into single elements, sorting those, and then merging the sorted elements back together until the entire array is sorted. Merge sort runs in O(n log n) time and is useful for sorting large data sets because it requires very little additional memory and has good performance on most data sets.

Uploaded by

Saraswati Sharma
Copyright
© © All Rights Reserved
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/ 10

By: Tset08en Namgyal Bhutia (18CSEC-10)

& Deepika Sharma(18CSEC-08)


 Introduction
 Definition
 Algorithm
 StepsInvolved
 Program
 Applications
 Merge sort is a complex and fast sorting
algorithm that repeatedly divides an unsorted
section into two equal sub-sections,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 merges
the two sorted halves.The merge() function is
used for merging two halves.
 Find the middle point to divide the array into
two halves.
 Call merge sort for first half.
 Call merge sort for second half.
 Merge the two halves sorted in step 2 and 3.
 Combine the both sorted to make solution.
 Divide the problem into sub-problems that are
similar to the original but smaller in size.
 Conquer the sub-problems by solving them
recursively.
 Combine the solutions to create a solution to
original problem.
 #include<stdio.h>

 void mergesort(int a[],int i,int j);
 void merge(int a[],int i1,int j1,int i2,int j2);

 int main()
 {
 int a[30],n,i;
 printf("Enter no of elements:");
 scanf("%d",&n);
 printf("Enter array elements:");
 for(i=0;i<n;i++)
 scanf("%d",&a[i]);
 mergesort(a,0,n-1);
 printf("\nSorted array is :");
 for(i=0;i<n;i++)
 printf("%d ",a[i]);
 return 0;
 }

 void mergesort(int a[],int i,int j)
 {
 int mid;
 if(i<j)
 {
 mid=(i+j)/2;
 mergesort(a,i,mid); //left recursion
 mergesort(a,mid+1,j); //right recursion
 merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
 }
 }

 void merge(int a[],int i1,int j1,int i2,int j2)
 {
 int temp[50]; //array used for merging
 int i,j,k;
 i=i1; //beginning of the first list
 j=i2; //beginning of the second list
 k=0;
 while(i<=j1 && j<=j2) //while elements in both lists
 {
 if(a[i]<a[j])
 temp[k++]=a[i++];
 else
 temp[k++]=a[j++];
 }
 while(i<=j1) //copy remaining elements of the first list
 temp[k++]=a[i++];
 while(j<=j2) //copy remaining elements of the second list
 temp[k++]=a[j++];
 //Transfer elements from temp[] back to a[]
 for(i=i1,j=0;i<=j2;i++,j++)
 a[i]=temp[j];
 }
 Merge sort type algorithm allows large data
sets to be sorted easily.
 Merge sort accesses the data sequentially and
the need of random access is low.
 Used in External Sorting.

You might also like