Merge sort algorithm

33
BY Halima Khan NIazi

Transcript of Merge sort algorithm

Page 1: Merge sort algorithm

BYHalima Khan NIazi

Page 2: Merge sort algorithm

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

Page 3: Merge sort algorithm

Dictionary Phone contacts

Page 4: Merge sort algorithm

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.

Page 5: Merge sort algorithm

Divide.

Page 6: Merge sort algorithm

combine

Page 7: Merge sort algorithm
Page 8: Merge sort algorithm

Consider an array having 6 elements 5 4 3 1 2 6Arrange the elements in ascending order

using quick sort algorithm

Page 9: Merge 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

Page 10: Merge sort algorithm

beg end

Is beg<end?

0 1 2 3 4 55 4 3 1 2 6

Page 11: Merge 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

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]

Page 12: Merge sort algorithm

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

Page 13: Merge sort algorithm

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]

Page 14: Merge sort algorithm

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]

Page 15: Merge sort algorithm

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

Page 16: Merge sort algorithm

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

Page 17: Merge sort algorithm

0 15 4

03

Left sub array Right sub-array

As 4&5 > 3 So merge sorted the left side

Page 18: Merge sort algorithm

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

Page 19: Merge sort algorithm

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

Page 20: Merge sort algorithm

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

Page 21: Merge sort algorithm

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

Page 22: Merge sort algorithm

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

Page 23: Merge sort algorithm

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

Page 24: Merge sort algorithm

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

Page 25: Merge sort algorithm

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

Page 26: Merge sort algorithm

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

Page 27: Merge sort algorithm

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

Page 28: Merge sort algorithm

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

Page 29: Merge sort algorithm

0 1 2 3 4 51 4 3 1 2 6

Page 30: Merge sort algorithm

#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]; }

Page 31: Merge sort algorithm

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 } }

Page 32: Merge sort algorithm

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]; }

Page 33: Merge sort algorithm