SlideShare a Scribd company logo
1 of 19
Merge Sort
Data Structures and Algorithms
Content
● What is Merge Sort?
● How it happens?
● Algorithm
● Implementation
● Complexity
● Comparison with other sort algorithms
● Advantages and Disadvantages
What is Merge Sort?
3
What is merging?
● Mixing two array elements in ascending order to
produce a third array in ascending.
Ex:
12
15
45
10
22
30
10
12
15
22
30
45
Array A Array B
Array C
» Merge sort is a sorting technique based on divide and
conquer technique with worst-case time complexity
being O(n log n)
» In computer science, merge sort is an efficient, most
respected ,general-purpose and comparison-based
sorting algorithm
» In simply, merge sort first divides the array into equal
halves and then combines them in a sorted manner
4
5
» To merge two sorted arrays,firstly we index both arrays
starting at zero,where the smallest element is located.
» Comparing the elements at each index,we choose the smaller
element,put it into the array that we are merging into.
» Increment the index of the smaller
element.
» By this method,we continually select
the next smallest element from the
two arrays and merge them into
sorted array.
How it happens?
Divide
Problem is decomposed into one or more sub problems
Conquer
Every sub problem is solved recursively
Combine
Merge the solutions of subproblems to
create the solution of the original problem
6
How it happens cont;
Ex:
Array Name : A
Starting index : p
Ending index : r
Array : A[p..r]
7
How it happens cont;
Divide
Mid point of p and r : q
the subarrays : A[p..q] and A[q+1, r]
Conquer
Sorting subarrays A[p..q] and A[q+1, r] till the base case is
reached
Combine
Combining two sorted subarrays A[p..q] and A[q+1, r]
8
9
Algorithm MergeSort(l,h){
if(l<h){
mid=(l+h)/2;
MergeSort(l,mid);
MergeSort((mid+1),h);
Merge(l,mid,h);
}
}
4 1 3 47
0 1 2 43
Low
Mid
High
l = low , h = high
2
5
6
0 1 2 3
8 5
76
9
Algorithm
10
4 1 3
4
2
5
6
0 1 2 3
8 5
76
7
0,7
0,3 4,7
0,1 2,3 4,5 6,7
0,0 1,1 2,2 3,3 4,4 5,5 6,6 7,7
Split
Merge
1 2
3
4 5
6
7
merging steps
public class MergeSortEasy {
private static void mergesort(int low, int high){
if(low < high){
int middle = (high + low) / 2;
mergesort(low, middle);
mergesort(middle + 1, high);
merge(low, middle, high);
}
}
11
Implementation
12
private static void merge(int low, int middle, int high){
for(int i = low; i <= high; i++){
helper[i] = numbers[i];
}
int i = low;
int j = middle + 1;
int k = low;
13
while(i <= middle && j <= high){
if(helper[i] <= helper[j]){
numbers[k] = helper[i];
i++;
} else {
numbers[k] = helper[j];
j++;
}
k++;
}
14
while( i <= middle){
numbers[k] = helper[i];
k++;
i++;
}
}
}
15
·Best, worst, and average time complexity of algorithm express what the
resource usage is at least, at most and on average, respectively.
·In merge Sort algorithm it can be expressed as,
T(n)=T(n/2)+T(n/2)+ ɵ(n)
T(n) = 2T(n/2) + ɵ(n)
T(n) ={2T(n/2) + cn} (if n>1)
f(n)=cn
T(n)= ɵ(nlgn)
·In all 3 cases (worst, average and best) time complexity of Merge Sort
is ɵ(nLogn).
Time Complexity
16
Space Complexity
● Space complexity of merge sort is O(n).
● In every recursive call that we create an array for
merging and they take no more than O(n)space.
● When the merging is done ,these arrays are
deleted and some new ones will be created in
some other recursive call.
17
Comparison with other sort algorithms
•Merge sort is a stable sort and is more efficient at handling slow to
access sequential media
•Merge sort is often best choice for sorting a linked list
•Heapsort has same time bounds as merge sort (heapsort requires
only O(1) and merge sort requires O(n) )
•Relatively easy to implement merge sort in such a way it requires
only O(1) extra space
•Slow random access performance of a linked list make some other
algorithms perform poor (quick sort) and some others completely
impossible (heapsort)
18
Thank You!
19

More Related Content

What's hot

Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithnKumar
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | EdurekaEdureka!
 
Recursion fib
Recursion fibRecursion fib
Recursion fibmallikamt
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)Imdadul Himu
 
Insertion sort and shell sort
Insertion sort and shell sortInsertion sort and shell sort
Insertion sort and shell sortPraveen Kumar
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationZakaria Hossain
 

What's hot (20)

Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Stack
StackStack
Stack
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Mergesort
MergesortMergesort
Mergesort
 
Functions & Recursion
Functions & RecursionFunctions & Recursion
Functions & Recursion
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
Merge sort
Merge sortMerge sort
Merge sort
 
Recursion fib
Recursion fibRecursion fib
Recursion fib
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Heaps
HeapsHeaps
Heaps
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
Insertion sort and shell sort
Insertion sort and shell sortInsertion sort and shell sort
Insertion sort and shell sort
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
stack presentation
stack presentationstack presentation
stack presentation
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with Animation
 
PRIM'S ALGORITHM
PRIM'S ALGORITHMPRIM'S ALGORITHM
PRIM'S ALGORITHM
 

Similar to Merge sort

Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfStep 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfaloeplusint
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 DsQundeel
 
Data Structure
Data StructureData Structure
Data Structuresheraz1
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 DsQundeel
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Anwar Patel
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureRai University
 
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 applicationsyazad dumasia
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfNiravPanchal50
 
Size measurement and estimation
Size measurement and estimationSize measurement and estimation
Size measurement and estimationLouis A. Poulin
 

Similar to Merge sort (20)

Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfStep 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Data Structure
Data StructureData Structure
Data Structure
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
Merge sort
Merge sortMerge sort
Merge sort
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
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
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
 
Size measurement and estimation
Size measurement and estimationSize measurement and estimation
Size measurement and estimation
 

Recently uploaded

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 

Recently uploaded (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 

Merge sort

  • 1. Merge Sort Data Structures and Algorithms
  • 2. Content ● What is Merge Sort? ● How it happens? ● Algorithm ● Implementation ● Complexity ● Comparison with other sort algorithms ● Advantages and Disadvantages
  • 3. What is Merge Sort? 3 What is merging? ● Mixing two array elements in ascending order to produce a third array in ascending. Ex: 12 15 45 10 22 30 10 12 15 22 30 45 Array A Array B Array C
  • 4. » Merge sort is a sorting technique based on divide and conquer technique with worst-case time complexity being O(n log n) » In computer science, merge sort is an efficient, most respected ,general-purpose and comparison-based sorting algorithm » In simply, merge sort first divides the array into equal halves and then combines them in a sorted manner 4
  • 5. 5 » To merge two sorted arrays,firstly we index both arrays starting at zero,where the smallest element is located. » Comparing the elements at each index,we choose the smaller element,put it into the array that we are merging into. » Increment the index of the smaller element. » By this method,we continually select the next smallest element from the two arrays and merge them into sorted array.
  • 6. How it happens? Divide Problem is decomposed into one or more sub problems Conquer Every sub problem is solved recursively Combine Merge the solutions of subproblems to create the solution of the original problem 6
  • 7. How it happens cont; Ex: Array Name : A Starting index : p Ending index : r Array : A[p..r] 7
  • 8. How it happens cont; Divide Mid point of p and r : q the subarrays : A[p..q] and A[q+1, r] Conquer Sorting subarrays A[p..q] and A[q+1, r] till the base case is reached Combine Combining two sorted subarrays A[p..q] and A[q+1, r] 8
  • 9. 9 Algorithm MergeSort(l,h){ if(l<h){ mid=(l+h)/2; MergeSort(l,mid); MergeSort((mid+1),h); Merge(l,mid,h); } } 4 1 3 47 0 1 2 43 Low Mid High l = low , h = high 2 5 6 0 1 2 3 8 5 76 9 Algorithm
  • 10. 10 4 1 3 4 2 5 6 0 1 2 3 8 5 76 7 0,7 0,3 4,7 0,1 2,3 4,5 6,7 0,0 1,1 2,2 3,3 4,4 5,5 6,6 7,7 Split Merge 1 2 3 4 5 6 7 merging steps
  • 11. public class MergeSortEasy { private static void mergesort(int low, int high){ if(low < high){ int middle = (high + low) / 2; mergesort(low, middle); mergesort(middle + 1, high); merge(low, middle, high); } } 11 Implementation
  • 12. 12 private static void merge(int low, int middle, int high){ for(int i = low; i <= high; i++){ helper[i] = numbers[i]; } int i = low; int j = middle + 1; int k = low;
  • 13. 13 while(i <= middle && j <= high){ if(helper[i] <= helper[j]){ numbers[k] = helper[i]; i++; } else { numbers[k] = helper[j]; j++; } k++; }
  • 14. 14 while( i <= middle){ numbers[k] = helper[i]; k++; i++; } } }
  • 15. 15
  • 16. ·Best, worst, and average time complexity of algorithm express what the resource usage is at least, at most and on average, respectively. ·In merge Sort algorithm it can be expressed as, T(n)=T(n/2)+T(n/2)+ ɵ(n) T(n) = 2T(n/2) + ɵ(n) T(n) ={2T(n/2) + cn} (if n>1) f(n)=cn T(n)= ɵ(nlgn) ·In all 3 cases (worst, average and best) time complexity of Merge Sort is ɵ(nLogn). Time Complexity 16
  • 17. Space Complexity ● Space complexity of merge sort is O(n). ● In every recursive call that we create an array for merging and they take no more than O(n)space. ● When the merging is done ,these arrays are deleted and some new ones will be created in some other recursive call. 17
  • 18. Comparison with other sort algorithms •Merge sort is a stable sort and is more efficient at handling slow to access sequential media •Merge sort is often best choice for sorting a linked list •Heapsort has same time bounds as merge sort (heapsort requires only O(1) and merge sort requires O(n) ) •Relatively easy to implement merge sort in such a way it requires only O(1) extra space •Slow random access performance of a linked list make some other algorithms perform poor (quick sort) and some others completely impossible (heapsort) 18

Editor's Notes

  1. *Divide and conquer *Recurrion is taken significant place *what is recursion
  2. * Adding values on temporary on a data structure
  3. *Choose the smallest and highest value *0th and 1th
  4. *shift the values