Merge Sort
Algorithm
OVERVIEW
02
Visual
Example
Summary
05
08
Core Concept
03
Code Example
Questions
06
09
Algorithm
01
Working
Procedure
Graphical
Example
04
07
Merge Sort
• A step-by-stepp procedure to solve
any given problem.
ALGORITHM
• Set of instruction , design to
accomplish any task
Definition:
01
MERGE
SORT
Definition:
The Merge Sort algorithm is a
divide-and-conquer algorithm that
sorts an array by first breaking it
down into smaller arrays, and then
building the array back together the
correct way so that it is sorted..
02
CORE
CONCEPT
CONCEPT 1 :DIVIDE
The algorithm starts with breaking up the
array into smaller and smaller pieces until
one such sub-array only consists of one
element..
CONCEPT 2 : CONQUER
The algorithm merges the small pieces of
the array back together by putting the
lowest values first, resulting in a sorted
array.
CONCEPT 3 : RECURSIVENESS
The breaking down and building up of the
array to sort the array is done recursively.
03
Advantages VS Disadvantages
• Merge sort can Sort large
amounts of data
• Have faster time
complexity
• Stable sorting algorithm
• Consume large space
because merge sort requires
an array as same size as the
list to sort the given data
• It is slower in case of dealing
with smaller sets to sort out
05 GRAPHIC
EXAMPLE
06 CODE EXAMPLE
#include<iostream>
using namespace std;
// Function to merge two
sorted subarrays
void merge(int arr[], int left, int
mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
// Temporary arrays
int L[n1], R[n2];
// Copy data to temporary
subarrays
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int i = 0; i < n2; i++)
R[i] = arr[mid + 1 + i];
/
CODE
EXAMPLE
// Merge the temporary arrays back
into the original array
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;}
k++; }
// Copy remaining elements of L, if any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}}
// Recursive function to implement merge
sort
void mergeSort(int arr[], int left, int right) {
if (left < right) {
// Find the midpoint
int mid = left + (right - left) / 2;
// Recursively sort the first and second halves
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
// Merge the sorted halves
merge(arr, left, mid, right);
}
}
int main() {
// Example input array
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
CODE
EXAMPLE
06 CODE EXAMPLE
// Perform merge sort
mergeSort(arr, 0, n - 1);
// Output the sorted array
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
VISUAL
EXAMPLE
Time Complexity
The time complexity for Merge Sort is
Where “n” is the number of elements to sort
And “ O” is the Space complexity of the algorithm
• Various Visual examples
• Advantages Vs
Disadvantages
SUMMARY
Part 1 Part 2
• Divide the given string
until until element in
array gets equals to one
• Puts the elements
back ,in ascending form
08 Merge Sort Algorithm-> divide-and-conquer algorithm
THANK YOU
ANY
QUESTION ?

dsa presentation on merge sorting in C++.pptx

  • 1.
  • 2.
  • 3.
    • A step-by-steppprocedure to solve any given problem. ALGORITHM • Set of instruction , design to accomplish any task Definition: 01
  • 4.
    MERGE SORT Definition: The Merge Sortalgorithm is a divide-and-conquer algorithm that sorts an array by first breaking it down into smaller arrays, and then building the array back together the correct way so that it is sorted.. 02
  • 5.
    CORE CONCEPT CONCEPT 1 :DIVIDE Thealgorithm starts with breaking up the array into smaller and smaller pieces until one such sub-array only consists of one element.. CONCEPT 2 : CONQUER The algorithm merges the small pieces of the array back together by putting the lowest values first, resulting in a sorted array. CONCEPT 3 : RECURSIVENESS The breaking down and building up of the array to sort the array is done recursively. 03
  • 6.
    Advantages VS Disadvantages •Merge sort can Sort large amounts of data • Have faster time complexity • Stable sorting algorithm • Consume large space because merge sort requires an array as same size as the list to sort the given data • It is slower in case of dealing with smaller sets to sort out
  • 7.
  • 8.
    06 CODE EXAMPLE #include<iostream> usingnamespace std; // Function to merge two sorted subarrays void merge(int arr[], int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; // Temporary arrays int L[n1], R[n2]; // Copy data to temporary subarrays for (int i = 0; i < n1; i++) L[i] = arr[left + i]; for (int i = 0; i < n2; i++) R[i] = arr[mid + 1 + i]; /
  • 9.
    CODE EXAMPLE // Merge thetemporary arrays back into the original array int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++;} k++; } // Copy remaining elements of L, if any while (i < n1) { arr[k] = L[i]; i++; k++; }}
  • 10.
    // Recursive functionto implement merge sort void mergeSort(int arr[], int left, int right) { if (left < right) { // Find the midpoint int mid = left + (right - left) / 2; // Recursively sort the first and second halves mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); // Merge the sorted halves merge(arr, left, mid, right); } } int main() { // Example input array int arr[] = {12, 11, 13, 5, 6, 7}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Original array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; CODE EXAMPLE
  • 11.
    06 CODE EXAMPLE //Perform merge sort mergeSort(arr, 0, n - 1); // Output the sorted array cout << "Sorted array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; return 0; }
  • 12.
  • 13.
    Time Complexity The timecomplexity for Merge Sort is Where “n” is the number of elements to sort And “ O” is the Space complexity of the algorithm
  • 14.
    • Various Visualexamples • Advantages Vs Disadvantages SUMMARY Part 1 Part 2 • Divide the given string until until element in array gets equals to one • Puts the elements back ,in ascending form 08 Merge Sort Algorithm-> divide-and-conquer algorithm
  • 15.