0% found this document useful (0 votes)
363 views4 pages

Mergesort (Arr, L, R) : Write A C++ Program To Sort The Given Data Using Merge Sort

The document describes a C++ program to implement merge sort. It includes the algorithm, flowchart and code to sort an array of integers using merge sort. The program takes input from the user, calls the mergeSort function to recursively sort halves of the array and merge them, and outputs the sorted array.

Uploaded by

Grasp A Bit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
363 views4 pages

Mergesort (Arr, L, R) : Write A C++ Program To Sort The Given Data Using Merge Sort

The document describes a C++ program to implement merge sort. It includes the algorithm, flowchart and code to sort an array of integers using merge sort. The program takes input from the user, calls the mergeSort function to recursively sort halves of the array and merge them, and outputs the sorted array.

Uploaded by

Grasp A Bit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CHANDIGARH UNIVERSITY DATA STRUCTURE LAB (CSP-209)

PRACTICAL NO. 7
OBJECTIVE
Write a C++ program to sort the given data using merge Sort.

ALGORITHM

MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
FLOWCHART

CODE

Amitabh Kumar
16Bcs1181 1|Page
CHANDIGARH UNIVERSITY DATA STRUCTURE LAB (CSP-209)

#include <iostream>
using namespace std;
void Merge(int a[], int low, int high, int mid)
{int i=low, j=mid+1, k=0, temp[high-low+1];
while (i <= mid && j <= high)
{if (a[i] < a[j])
{temp[k] = a[i];
k++;
i++;}
else
{temp[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{temp[k] = a[i];
k++;
i++;
}
while (j <= high)
{temp[k] = a[j];
k++;
j++;
}

Amitabh Kumar
16Bcs1181 2|Page
CHANDIGARH UNIVERSITY DATA STRUCTURE LAB (CSP-209)

for (i = low; i <= high; i++)


{a[i] = temp[i-low];
}
}
void MergeSort(int a[], int low, int high)
{int mid;
if (low < high)
{mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
int main()
{int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
MergeSort(arr, 0, n-1);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];

Amitabh Kumar
16Bcs1181 3|Page
CHANDIGARH UNIVERSITY DATA STRUCTURE LAB (CSP-209)

return 0;
}

OUTPUT

Amitabh Kumar
16Bcs1181 4|Page

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy