Merge sort algorithm
-
Upload
halima-khan -
Category
Engineering
-
view
44 -
download
0
Transcript of Merge sort algorithm
BYHalima Khan NIazi
Sorting is a process of arranging data in some logical order
In case of numeric values order may be ascending or descending or dictionary order(lexicographical order) in case of alphanumeric values.
There are two types of sorting1. Internal sorting2.External sorting
Dictionary Phone contacts
Merge sort is a sorting algorithm that sortsdata items into ascending order whichcomes under the category of comparisonbased sorting . It is a technique based on divide and
conquer. Merge sort first divides the array into equal
halves and the combine them in a sorted manner.
Divide.
combine
Consider an array having 6 elements 5 4 3 1 2 6Arrange the elements in ascending order
using quick sort algorithm
beg
end
starting index of the array
ending index of the array
0 1 2 3 4 55 4 3 1 2 6
End
beg end
Is beg<end?
0 1 2 3 4 55 4 3 1 2 6
beg end
starting index of the array ending index of the array
0 1 2 3 4 55 4 3 1 2 6
if beg<end? yes
then divide the array into two halves mid=(beg+end)/2
mid=(0+5)/2 mid=2[taking integer part only]
beg end
we will first consider the left sub array
Left sub-array Right sub-array
0 1 2 3 4 55 4 3 1 2 6
0 1 25 4 3
endbeg
Left sub-array
If beg<end?Yes
Then divide the array into two halves mid=(beg+end)/2
mid=(0+2)/2 mid=2[taking integer part
only]
0 1 25 4 3
0 15 4
end beg
03
0 15 4
03
04
05
04
If beg<end?
yes
Then divide the array into two halves mid=(beg+end)/2 mid=(0+2)/2 mid=2[taking integer part only]
0 1 2 3 4 55 4 3 1 2 6
0 1 25 4 3
0 15 4
03
04
05
04
0 1 21 2 6
0 11 2
26
06
02
01
Conquer and combine
04
05
04
06
02
01
0 15 4
03 Is 5<4?
So swap their position
As the right sub array has 1 element so we will sort it with the left sorted sub array
54
right sub-array
0 15 4
03
Left sub array Right sub-array
As 4&5 > 3 So merge sorted the left side
0 1 2 3 4 55 4 3 1 2 6
0 1 23 4 5
Left sub array
Now we can move to right sub array
Left sub array Right sub-array
The left sub array is sorted
0 1 2 3 4 55 4 3 1 2 6
Left sub array Right sub-array
0 1 23 4 5
Left sub array
0 0 01 2 6
Right sub-array
left sub array is sorted we can move to right the right sub array
0 0 01 2 6
Right sub-array
01
02
06
Left Right
Is 1<2 ?yes
*as1<2so we dont have to swap position
0 1 21 2 6
0 11 2
06
Left sub array Right sub array
The left sub array is sorted So we can move to right sub array
0 1 2 3 4 55 4 3 1 2 6
Left sub array Right sub-array
0 1 2 3 4 55 4 3 1 2 6
Left sub array
Right sub-array
0 1 21 2 6
0 11 2
06
As there is only one element in the right sub array So we can now sort the right sub array with left sorted sub array
as 1 and 2 are both less than 6 so we dont have to swap position
0 1 2 3 4 55 4 3 1 2 6
Left sub array Right sub-array
0 1 23 4 5
Left sub array
0 1 21 2 6
Right sub-array
As both the left and right sub array are sortedwe just have to merge them in ascending order
using merging algorithm
0 1 2 3 4 55 4 3 1 2 6
0 1 23 4 5
0 1 21 2 6
Is 3<1 ?No
So swap theirposition
0 1 2 3 4 51 4 3 1 2 6
0 1 23 4 5
3 4 51 2 6
Is 3<2 ?No
So swap theirposition
0 1 2 3 4 51 4 3 1 2 6
0 1 23 4 5
3 4 51 2 6
Is 3<6 ?Yes
0 1 2 3 4 51 4 3 1 2 6
0 1 23 4 5
3 4 51 2 6
Is 4<6 ?yes
0 1 2 3 4 51 4 3 1 2 6
0 1 23 4 5
3 4 51 2 6
Is 5<6 ?Yes
0 1 2 3 4 51 4 3 1 2 6
0 1 2 3 4 51 4 3 1 2 6
#include<conio.h> #include<iostream.h> void merge_sort(int *,int,int); void merge(int *,int,int,int); int main() { int array[30],n,i; cout<<"\n\t\t****MERGING SORTED ALGORITHEM***\n"; cout<<"\nPlease!Enter the number of elements you want to insert\n"; cin>>n; cout<<"\nNOW!Enter elements for array\n"; for(i=0;i<n;i++) { cin>>array[i]; }
merge_sort(array,0,n-1); cout<<"AFTER MERGE sorted array is\n"; for(int i=0;i<n;i++) { cout<<"array["<<i<<"]="<<array[i]<<"\n"; } getch(); return 0; } void merge_sort(int *array,int l,int r) { if(l<r) { int mid; mid=(l+r)/2; //divide list into two equal parts merge_sort(array,l,mid); //left recursion merge_sort(array,mid+1,r); //right recursion merge(array,l,mid,r); //merging of two sorted sub arrays } }
void merge(int *array,int l,int mid,int r) { //cout<<endl; int i=l; //start from left or 1st list int j=mid+1; //start from 2nd or right list int k=0;; int *b=new int[r-l+1]; //point array used temp for
merging while(i<=mid && j<=r) //condition check in both list { if(array[i]<array[j]){ b[k++]=array[i++];} else{b[k++]=array[j++];}} while(i<=mid) //copy remaining element of first list b[k++]=array[i++]; while(j<=r) //copy remaining element of the second
list b[k++]=array[j++]; //transfer elements from b[] back to a[] for(i=r;i>=l;i--) array[i]=b[--k]; }