0% found this document useful (0 votes)
2 views7 pages

DAA File

The document contains two C++ programs that implement sorting algorithms: merge sort and quick sort. The merge sort program sorts an array by dividing it into halves, sorting each half, and merging them back together, while the quick sort program uses a pivot to partition the array and recursively sort the partitions. Both programs include user input for the number of elements and the elements themselves, displaying the array before and after sorting.

Uploaded by

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

DAA File

The document contains two C++ programs that implement sorting algorithms: merge sort and quick sort. The merge sort program sorts an array by dividing it into halves, sorting each half, and merging them back together, while the quick sort program uses a pivot to partition the array and recursively sort the partitions. Both programs include user input for the number of elements and the elements themselves, displaying the array before and after sorting.

Uploaded by

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

Program 6

To implement merge sort


#include<iostream>

using namespace std;

void swapping(int &a, int &b) {

int temp;

temp = a;

a = b;

b = temp;

void display(int *array, int size) {

for(int i = 0; i<size; i++)

cout << array[i] << " ";

cout << endl;

void merge(int *array, int l, int m, int r) {

int i, j, k, nl, nr;

nl = m-l+1; nr = r-m;

int larr[nl], rarr[nr];

for(i = 0; i<nl; i++)

larr[i] = array[l+i];

for(j = 0; j<nr; j++)

rarr[j] = array[m+1+j];
i = 0; j = 0; k = l;

while(i < nl && j<nr) {

if(larr[i] <= rarr[j]) {

array[k] = larr[i];

i++;

}else{

array[k] = rarr[j];

j++;

k++;

while(i<nl) {

array[k] = larr[i];

i++; k++;

while(j<nr) {

array[k] = rarr[j];

j++; k++;

void mergeSort(int *array, int l, int r) {

int m;

if(l < r) {
int m = l+(r-l)/2;

mergeSort(array, l, m);

mergeSort(array, m+1, r);

merge(array, l, m, r);

int main() {

int n;

cout << "Enter the number of elements: ";

cin >> n;

int arr[n];

cout << "Enter elements:" << endl;

for(int i = 0; i<n; i++) {

cin >> arr[i];

cout << "Array before Sorting: ";

display(arr, n);

mergeSort(arr, 0, n-1);

cout << "Array after Sorting: ";

display(arr, n);

}
Program 7
To implement Quick sort
#include<iostream>

#include<cstdlib>

using namespace std;

void swap(int *a, int *b) {

int temp;

temp = *a;

*a = *b;

*b = temp;

int Partition(int a[], int l, int h) {

int pivot, index, i;

index = l;

pivot = h;

for(i = l; i < h; i++) {

if(a[i] < a[pivot]) {

swap(&a[i], &a[index]);

index++;

}
}

swap(&a[pivot], &a[index]);

return index;

int RandomPivotPartition(int a[], int l, int h) {

int pvt, n, temp;

n = rand();

pvt = l + n%(h-l+1);

swap(&a[h], &a[pvt]);

return Partition(a, l, h);

int QuickSort(int a[], int l, int h) {

int pindex;

if(l < h) {

pindex = RandomPivotPartition(a, l, h);

QuickSort(a, l, pindex-1);

QuickSort(a, pindex+1, h);

return 0;

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];

QuickSort(arr, 0, n-1);

cout<<"\nSorted Data ";

for (i = 0; i < n; i++)

cout<<"->"<<arr[i];

return 0;

You might also like