SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Data Structures
Data StructuresData Structures
Data Structures
 
Quick sort
Quick sortQuick sort
Quick sort
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Data Structures (BE)
Data Structures (BE)Data Structures (BE)
Data Structures (BE)
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Counting Sort
Counting SortCounting Sort
Counting Sort
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Merge sort
Merge sortMerge sort
Merge sort
 

Similar a 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
 
An Introduction to Test Driven Development with React
An Introduction to Test Driven Development with ReactAn Introduction to Test Driven Development with React
An Introduction to Test Driven Development with ReactFITC
 

Similar a 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
 
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
 
An Introduction to Test Driven Development with React
An Introduction to Test Driven Development with ReactAn Introduction to Test Driven Development with React
An Introduction to Test Driven Development with React
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Último (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.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

Notas del editor

  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