SlideShare a Scribd company logo
Merge Sort
Divide And Conquer
• Merging two lists of one element each is the same as
sorting them.
• Merge sort divides up an unsorted list until the
above condition is met and then sorts the divided
parts back together in pairs.
• Specifically this can be done by recursively dividing
the unsorted list in half, merge sorting the left side
then the right side and then merging the left and
right back together.
Mergesort Example
8 2 9 4 5 3 1 6
8 2 1 6
9 4 5 3
8 2 9 4 5 3 1 6
2 8 4 9 3 5 1 6
2 4 8 9 1 3 5 6
1 2 3 4 5 6 8 9
Merge
Merge
Merge
Divide
Divide
Divide
1 element
8 2 9 4 5 3 1 6
Merge Sort – Example
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
26
18 6
32 15
43 1
9
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
18 26 32
6 15 43 1 9
6 18 26 32 1 9 15 43
1 6 9 15 18 26 32 43
18 26
18 26
18 26
32
32
6
6
32 6
18 26 32 6
43
43
15
15
43 15
9
9
1
1
9 1
43 15 9 1
18 26 32 6 43 15 9 1
18 26 6
32
6
26 32
18
15
43 1
9
1 9
15 43
1
6 9 15
18 26 32 43
Original Sequence Sorted Sequence
Divide-and-conquer Technique
subproblem 2
of size n/2
subproblem 1
of size n/2
a solution to
subproblem 1
a solution to
the original problem
a solution to
subproblem 2
a problem of size n
Using Divide and Conquer:
Mergesort strategy
Sorted
Merge
Sorted Sorted
Sort recursively
by Mergesort
Sort recursively
by Mergesort
first last
(first  last)2
Merge Sort Algorithm
• Divide: Partition ‘n’ elements array into two
sub lists with n/2 elements each
• Conquer: Sort sub list1 and sub list2
• Combine: Merge sub list1 and sub list2
Merge Sort Example
99 6 86 15 58 35 86 4 0
Merge Sort Example
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
Merge Sort Example
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 15
99 6 58 35 86 4 0
Merge Sort Example
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 15
99 6 58 35 86 4 0
99 6 86 15 58 35 86 4 0
Merge Sort Example
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 15
99 6 58 35 86 4 0
99 6 86 15 58 35 86 4 0
4 0
Merge Sort Example
99 6 86 15 58 35 86 0 4
4 0
Merge
Merge Sort Example
15 86
6 99 35 58 0 4 86
99 6 86 15 58 35 86 0 4
Merge
Merge Sort Example
6 15 86 99 0 4 35 58 86
15 86
6 99 35 58 0 4 86
Merge
Merge Sort Example
0 4 6 15 35 58 86 86 99
6 15 86 99 0 4 35 58 86
Merge
Merge Sort Example
0 4 6 15 35 58 86 86 99
Program
#include<iostream.h>
#include<conio.h>
class MS
{
private:
int A[10];
public:
int n;
int low, high;
void get_data();
void Mergesort(int low, int high);
void Combine(int low, int mid, int high);
void Display();
};
void MS::get_data()
{
cout<<"n Enter the length of the list";
cin>>n;
cout<<"n Enter the list elements: n";
for(int i = 0; i<n; i++)
{
cin>>A[i];
}
}
void MS::Mergesort(int low, int high)
{
int mid;
if(low<high)
{
mid = (low+high)/2; //split the list at mid
Mergesort(low, mid); //first sublist
Mergesort(mid+1, high); //second sublist
Combine(low, mid, high); //merging two sublists
}
}
Program
void MS::Combine(int low, int mid, int high)
{
int i,j,k;
int temp[10];
i = low;
j = mid+1;
k = low;
while(i <= mid && j <= high)
{
if(A[i] <= A[j])
{
temp[k] = A[i];
i++;
k++;
}
else
{
temp[k] = A[j];
j++;
k++;
}
}
while(i<=mid)
{
temp[k] = A[i];
i++;
k++;
}
while(j<=high)
{
temp[k] = A[j];
j++;
k++;
}
for(k = low; k <= high; k++)
{
A[k] = temp[k];
}
}
Program
void MS::Display()
{
cout<<"n The sorted array is:n";
for(int i = 0; i<n; i++)
{
cout<<" "<<A[i];
}
}
int main()
{
MS obj;
obj.get_data();
obj.low=0;
obj.high=(obj.n)-1;
obj.Mergesort(obj.low, obj.high);
obj.Display();
getch();
return 0;
}
Time Complexity Analysis
• Worst case: O(nlog2n)
• Average case: O(nlog2n​​)
• Best case: O(nlog2n​​)

More Related Content

PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPTX
sorting.pptx
PPT
16-sorting.ppt
PPT
Lec 6 Divide and conquer of Data Structures & Algortihms
PPTX
Daa final
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
PPT
Chapter 11 - Sorting and Searching
PPTX
Merge sort analysis and its real time applications
Data Structures - Lecture 8 [Sorting Algorithms]
sorting.pptx
16-sorting.ppt
Lec 6 Divide and conquer of Data Structures & Algortihms
Daa final
Searching Sorting-SELECTION ,BUBBBLE.ppt
Chapter 11 - Sorting and Searching
Merge sort analysis and its real time applications

Similar to merge sort help in language C with algorithms (20)

PPTX
SORT AND SEARCH ARRAY WITH WITH C++.pptx
PPTX
Insertion and merge sort
PPTX
L16-L19-Searching&Sorting&string123.pptx
PPTX
10 merge sort
PPT
Advanced s and s algorithm.ppt
PPT
24 Cs146 Jc Merge
PPTX
Chapter 8 Sorting in the context of DSA.pptx
PPTX
Analysis and Design of Algorithms -Sorting Algorithms and analysis
PPT
(Data Structure) Chapter11 searching & sorting
PPTX
Different Searching and Sorting Methods.pptx
PPT
3.2 insertion sort
PPT
Data Structure (MC501)
PPTX
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
PPT
sorting_part1.ppt
PDF
Chapter 8 advanced sorting and hashing for print
PPT
14-sorting.ppt
PPT
14-sorting (3).ppt
PPT
14-sorting.ppt
PPT
14-sorting.ppt
SORT AND SEARCH ARRAY WITH WITH C++.pptx
Insertion and merge sort
L16-L19-Searching&Sorting&string123.pptx
10 merge sort
Advanced s and s algorithm.ppt
24 Cs146 Jc Merge
Chapter 8 Sorting in the context of DSA.pptx
Analysis and Design of Algorithms -Sorting Algorithms and analysis
(Data Structure) Chapter11 searching & sorting
Different Searching and Sorting Methods.pptx
3.2 insertion sort
Data Structure (MC501)
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
sorting_part1.ppt
Chapter 8 advanced sorting and hashing for print
14-sorting.ppt
14-sorting (3).ppt
14-sorting.ppt
14-sorting.ppt
Ad

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Approach and Philosophy of On baking technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
MYSQL Presentation for SQL database connectivity
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Per capita expenditure prediction using model stacking based on satellite ima...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Advanced methodologies resolving dimensionality complications for autism neur...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Approach and Philosophy of On baking technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Programs and apps: productivity, graphics, security and other tools
“AI and Expert System Decision Support & Business Intelligence Systems”
Ad

merge sort help in language C with algorithms

  • 2. Divide And Conquer • Merging two lists of one element each is the same as sorting them. • Merge sort divides up an unsorted list until the above condition is met and then sorts the divided parts back together in pairs. • Specifically this can be done by recursively dividing the unsorted list in half, merge sorting the left side then the right side and then merging the left and right back together.
  • 3. Mergesort Example 8 2 9 4 5 3 1 6 8 2 1 6 9 4 5 3 8 2 9 4 5 3 1 6 2 8 4 9 3 5 1 6 2 4 8 9 1 3 5 6 1 2 3 4 5 6 8 9 Merge Merge Merge Divide Divide Divide 1 element 8 2 9 4 5 3 1 6
  • 4. Merge Sort – Example 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 26 18 6 32 15 43 1 9 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 26 32 6 15 43 1 9 6 18 26 32 1 9 15 43 1 6 9 15 18 26 32 43 18 26 18 26 18 26 32 32 6 6 32 6 18 26 32 6 43 43 15 15 43 15 9 9 1 1 9 1 43 15 9 1 18 26 32 6 43 15 9 1 18 26 6 32 6 26 32 18 15 43 1 9 1 9 15 43 1 6 9 15 18 26 32 43 Original Sequence Sorted Sequence
  • 5. Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n
  • 6. Using Divide and Conquer: Mergesort strategy Sorted Merge Sorted Sorted Sort recursively by Mergesort Sort recursively by Mergesort first last (first  last)2
  • 7. Merge Sort Algorithm • Divide: Partition ‘n’ elements array into two sub lists with n/2 elements each • Conquer: Sort sub list1 and sub list2 • Combine: Merge sub list1 and sub list2
  • 8. Merge Sort Example 99 6 86 15 58 35 86 4 0
  • 9. Merge Sort Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0
  • 10. Merge Sort Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 86 15 99 6 58 35 86 4 0
  • 11. Merge Sort Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 86 15 99 6 58 35 86 4 0 99 6 86 15 58 35 86 4 0
  • 12. Merge Sort Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 86 15 99 6 58 35 86 4 0 99 6 86 15 58 35 86 4 0 4 0
  • 13. Merge Sort Example 99 6 86 15 58 35 86 0 4 4 0 Merge
  • 14. Merge Sort Example 15 86 6 99 35 58 0 4 86 99 6 86 15 58 35 86 0 4 Merge
  • 15. Merge Sort Example 6 15 86 99 0 4 35 58 86 15 86 6 99 35 58 0 4 86 Merge
  • 16. Merge Sort Example 0 4 6 15 35 58 86 86 99 6 15 86 99 0 4 35 58 86 Merge
  • 17. Merge Sort Example 0 4 6 15 35 58 86 86 99
  • 18. Program #include<iostream.h> #include<conio.h> class MS { private: int A[10]; public: int n; int low, high; void get_data(); void Mergesort(int low, int high); void Combine(int low, int mid, int high); void Display(); }; void MS::get_data() { cout<<"n Enter the length of the list"; cin>>n; cout<<"n Enter the list elements: n"; for(int i = 0; i<n; i++) { cin>>A[i]; } } void MS::Mergesort(int low, int high) { int mid; if(low<high) { mid = (low+high)/2; //split the list at mid Mergesort(low, mid); //first sublist Mergesort(mid+1, high); //second sublist Combine(low, mid, high); //merging two sublists } }
  • 19. Program void MS::Combine(int low, int mid, int high) { int i,j,k; int temp[10]; i = low; j = mid+1; k = low; while(i <= mid && j <= high) { if(A[i] <= A[j]) { temp[k] = A[i]; i++; k++; } else { temp[k] = A[j]; j++; k++; } } while(i<=mid) { temp[k] = A[i]; i++; k++; } while(j<=high) { temp[k] = A[j]; j++; k++; } for(k = low; k <= high; k++) { A[k] = temp[k]; } }
  • 20. Program void MS::Display() { cout<<"n The sorted array is:n"; for(int i = 0; i<n; i++) { cout<<" "<<A[i]; } } int main() { MS obj; obj.get_data(); obj.low=0; obj.high=(obj.n)-1; obj.Mergesort(obj.low, obj.high); obj.Display(); getch(); return 0; }
  • 21. Time Complexity Analysis • Worst case: O(nlog2n) • Average case: O(nlog2n​​) • Best case: O(nlog2n​​)