SlideShare a Scribd company logo
1 of 26
Welcome
University of Science & Technology,
Chittagong
Faculty of Science, Engineering &
Technology
Department: Computer Science &
Engineering
Course Title: Algorithm lab
Course code: CSE 222
Semester: 4th
Batch: 25th
Submitted by:
Name: Md Abdul Kuddus
Roll: 15010102
Submitted to:
Sohrab Hossain , Assistant Professor,
Department of CSE,FSET,USTC
Presentation Topic:
MERGE SORT
MERGE SORT :
Merge sort was invented by John Von Neumann
(1903 - 1957)
Merge sort is divide & conquer technique of sorting
element
Merge sort is one of the most efficient sorting
algorithm
Time complexity of merge sort is O(n log n)
DIVIDE & CONQUER
• DIVIDE : Divide the unsorted list into two sub lists of
about half the size
• CONQUER : Sort each of the two sub lists recursively.
If they are small enough just solve them in a straight
forward manner
• COMBINE : Merge the two-sorted sub lists back into
one sorted list
DIVIDE & CONQUER TECHNIQUE
a problem of size n
sub problem 1 of size n/2
sub problem 2 of
size n/2
a solution to sub
problem 1
a solution to sub
problem 2
a solution to the
original problem
MERGE SORT PROCESS :
• The list is divided into two equal (as equal as
possible) part
• There are different ways to divide the list into two
equal part
• The following algorithm divides the list until the list
has just one item which are sorted
• Then with recursive merge function calls these
smaller pieces merge
MERGE SORT ALGORITHM :
• Merge(A[],p , q , r)
{ n1 = q – p + 1
n2 = r – q
Let L[1 to n1+1] and R[1 to n2+1] be new array
for(i = 1 to n1)
L[i] = A[p + i - 1]
for(j = 1 to n2)
R[j] = A[q + j]
L[n1 + 1] = infinity
R[n2 + 1] = infinity
for(k = p to r)
{ if ( L[i] <= R[j])
A[k] = L[j]
i = i + 1
else
A[k] = R[j]
j = j + 1
}
Merge sort (A, p, r)
{ if (p < r) // check for base case
q = [ (p + r)/2 ] // divide step
Merge sort (A, p, q) // conquer step
Merge sort (A, q+1, r) // conquer step
Merge sort (A, p, q, r) // conquer step
}
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1K
1 2 3 4 5 6 7 8
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4 5K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4 5 6K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4 5 6 7K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4 5 6 7 8K
MERGE SORT EXAMPLE :
1 5 7 8 2 4 6 9
p q q + 1 r
1 5 7 8 infinity 2 4 6 9 infinityL R
i=1 2 3 4 5 j=1 2 3 4 5
1 2 4 5 6 7 8 9K
/* Merging (Merge sort) */
#include <stdio.h>
void main ()
{
int a[10] = {1, 5, 11, 30, 2, 4, 6, 9};
int i,j,k,n1,n2,p,q,r;
p = 1; q = 4; r = 8; n1 = q-p+1; n2 = r-q;
int L[n1+1];
int R[n2+1];
for( i=0; i<n1; i++)
L[i] = a[p+i-1];
for( i=0; i<n1; i++)
printf("%d n", L[i]);
for( j=0; j<n2; j++)
R[j] = a[q+j];
L[n1] = 300; R[n2] = 300; i=0; j=0;
for( k=0; k<r; k++)
{
if (L[i] <= R[j])
{
a[k] = L[i];
i++;
}
else
{
a[k] = R[j];
j++;
IMPLEMENTING MERGE SORT :
There are two basic ways to implement
merge sort
In place : Merge sort is done with only the
input array
Double storage : Merging is done with a
temporary array of the same size as the input
array
MERGE-SORT ANALYSIS :
Time, merging
log n levels
Total time for merging : O (n log n)
Total running time : Order of n log n
Total space : Order of n
n/2 n/2
n/4 n/4 n/4 n/4
n
Performance of MERGESORT :
• Unlike quick sort, merge sort guarantees O (n log n)in
the worst case
• The reason for this is that quick sort depends on the
value of the pivot whereas merge sort divides the list
based on the index
• Why is it O (n log n ) ?
• Each merge will require N comparisons
• Each time the list is halved
• So the standard divide & conquer recurrence applies
to merge sort
T(n) = 2T * n/2 + (n)
FEATURES OF MERGE SORT :
• It perform in O(n log n ) in the worst case
• It is stable
• It is quite independent of the way the initial list is organized
• Good for linked lists. Can be implemented in such a way that
data is accessed sequentially.
Drawbacks :
It may require an array of up to the size of the original list.
This can be avoided but the algorithm becomes significantly
more complicated making it worth while.
Instead of making it complicated we can use HEAP SORT which
is also O(n log n ). But you have to remember that HEAP SORT is not
stable in comparison to MERGE SORT
QUERIES
?
THANK YOU

More Related Content

What's hot

Insertion sort
Insertion sortInsertion sort
Insertion sortalmaqboli
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithnKumar
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation AhmedAlbutty
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsMohamed Loey
 
Selection sort
Selection sortSelection sort
Selection sortstella D
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 

What's hot (20)

Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Merge sort
Merge sortMerge sort
Merge sort
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Merge sort
Merge sortMerge sort
Merge sort
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Quick sort
Quick sortQuick sort
Quick sort
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Quick sort
Quick sortQuick sort
Quick sort
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Shell sort
Shell sortShell sort
Shell sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 

Similar to Merge sort algorithm power point presentation

Similar to Merge sort algorithm power point presentation (20)

Merge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering studentsMerge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering students
 
Mergesort
MergesortMergesort
Mergesort
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptx
 
Merge sort
Merge sortMerge sort
Merge sort
 
quick and merge.pptx
quick and merge.pptxquick and merge.pptx
quick and merge.pptx
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Parallel sorting algorithm
Parallel sorting algorithmParallel sorting algorithm
Parallel sorting algorithm
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
shell and merge sort
shell and merge sortshell and merge sort
shell and merge sort
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
Introduction of Algorithm.pdf
Introduction of Algorithm.pdfIntroduction of Algorithm.pdf
Introduction of Algorithm.pdf
 
sorting
sortingsorting
sorting
 
l1.ppt
l1.pptl1.ppt
l1.ppt
 
l1.ppt
l1.pptl1.ppt
l1.ppt
 
Welcome to Introduction to Algorithms, Spring 2004
Welcome to Introduction to Algorithms, Spring 2004Welcome to Introduction to Algorithms, Spring 2004
Welcome to Introduction to Algorithms, Spring 2004
 

Recently uploaded

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Christo Ananth
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 

Recently uploaded (20)

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 

Merge sort algorithm power point presentation

  • 2. University of Science & Technology, Chittagong Faculty of Science, Engineering & Technology Department: Computer Science & Engineering Course Title: Algorithm lab Course code: CSE 222 Semester: 4th Batch: 25th
  • 3. Submitted by: Name: Md Abdul Kuddus Roll: 15010102 Submitted to: Sohrab Hossain , Assistant Professor, Department of CSE,FSET,USTC
  • 5. MERGE SORT : Merge sort was invented by John Von Neumann (1903 - 1957) Merge sort is divide & conquer technique of sorting element Merge sort is one of the most efficient sorting algorithm Time complexity of merge sort is O(n log n)
  • 6. DIVIDE & CONQUER • DIVIDE : Divide the unsorted list into two sub lists of about half the size • CONQUER : Sort each of the two sub lists recursively. If they are small enough just solve them in a straight forward manner • COMBINE : Merge the two-sorted sub lists back into one sorted list
  • 7. DIVIDE & CONQUER TECHNIQUE a problem of size n sub problem 1 of size n/2 sub problem 2 of size n/2 a solution to sub problem 1 a solution to sub problem 2 a solution to the original problem
  • 8. MERGE SORT PROCESS : • The list is divided into two equal (as equal as possible) part • There are different ways to divide the list into two equal part • The following algorithm divides the list until the list has just one item which are sorted • Then with recursive merge function calls these smaller pieces merge
  • 9. MERGE SORT ALGORITHM : • Merge(A[],p , q , r) { n1 = q – p + 1 n2 = r – q Let L[1 to n1+1] and R[1 to n2+1] be new array for(i = 1 to n1) L[i] = A[p + i - 1] for(j = 1 to n2) R[j] = A[q + j] L[n1 + 1] = infinity R[n2 + 1] = infinity
  • 10. for(k = p to r) { if ( L[i] <= R[j]) A[k] = L[j] i = i + 1 else A[k] = R[j] j = j + 1 }
  • 11. Merge sort (A, p, r) { if (p < r) // check for base case q = [ (p + r)/2 ] // divide step Merge sort (A, p, q) // conquer step Merge sort (A, q+1, r) // conquer step Merge sort (A, p, q, r) // conquer step }
  • 12. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1K 1 2 3 4 5 6 7 8
  • 13. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2K
  • 14. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4K
  • 15. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4 5K
  • 16. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4 5 6K
  • 17. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4 5 6 7K
  • 18. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4 5 6 7 8K
  • 19. MERGE SORT EXAMPLE : 1 5 7 8 2 4 6 9 p q q + 1 r 1 5 7 8 infinity 2 4 6 9 infinityL R i=1 2 3 4 5 j=1 2 3 4 5 1 2 4 5 6 7 8 9K
  • 20. /* Merging (Merge sort) */ #include <stdio.h> void main () { int a[10] = {1, 5, 11, 30, 2, 4, 6, 9}; int i,j,k,n1,n2,p,q,r; p = 1; q = 4; r = 8; n1 = q-p+1; n2 = r-q; int L[n1+1]; int R[n2+1]; for( i=0; i<n1; i++) L[i] = a[p+i-1]; for( i=0; i<n1; i++) printf("%d n", L[i]); for( j=0; j<n2; j++) R[j] = a[q+j];
  • 21. L[n1] = 300; R[n2] = 300; i=0; j=0; for( k=0; k<r; k++) { if (L[i] <= R[j]) { a[k] = L[i]; i++; } else { a[k] = R[j]; j++;
  • 22. IMPLEMENTING MERGE SORT : There are two basic ways to implement merge sort In place : Merge sort is done with only the input array Double storage : Merging is done with a temporary array of the same size as the input array
  • 23. MERGE-SORT ANALYSIS : Time, merging log n levels Total time for merging : O (n log n) Total running time : Order of n log n Total space : Order of n n/2 n/2 n/4 n/4 n/4 n/4 n
  • 24. Performance of MERGESORT : • Unlike quick sort, merge sort guarantees O (n log n)in the worst case • The reason for this is that quick sort depends on the value of the pivot whereas merge sort divides the list based on the index • Why is it O (n log n ) ? • Each merge will require N comparisons • Each time the list is halved • So the standard divide & conquer recurrence applies to merge sort T(n) = 2T * n/2 + (n)
  • 25. FEATURES OF MERGE SORT : • It perform in O(n log n ) in the worst case • It is stable • It is quite independent of the way the initial list is organized • Good for linked lists. Can be implemented in such a way that data is accessed sequentially. Drawbacks : It may require an array of up to the size of the original list. This can be avoided but the algorithm becomes significantly more complicated making it worth while. Instead of making it complicated we can use HEAP SORT which is also O(n log n ). But you have to remember that HEAP SORT is not stable in comparison to MERGE SORT