0% found this document useful (0 votes)
41 views9 pages

Name:Nama N.V Reg:17Cec0 03: Que1: Merge Sort

The document contains code for implementing merge sort and quick sort algorithms. It includes the merge sort functions - Merge to merge two sorted arrays and MergeSort for recursively sorting the input array. For quick sort, it includes the partition function to partition the array around a pivot, and quicksort for recursively calling partition and sorting subarrays. Sample outputs are printed during each step to demonstrate how the algorithms work.

Uploaded by

Naman Veer
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)
41 views9 pages

Name:Nama N.V Reg:17Cec0 03: Que1: Merge Sort

The document contains code for implementing merge sort and quick sort algorithms. It includes the merge sort functions - Merge to merge two sorted arrays and MergeSort for recursively sorting the input array. For quick sort, it includes the partition function to partition the array around a pivot, and quicksort for recursively calling partition and sorting subarrays. Sample outputs are printed during each step to demonstrate how the algorithms work.

Uploaded by

Naman Veer
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/ 9

Name:Nama

n.V
Reg:17CEc0
03
QUE1: Merge sort

Code:

#include <iostream>

#include<stdio.h>

using namespace std;

void Merge(int *a, int low, int high, int mid)

int i, j, k, temp[high-low+1];

i = low;

k = 0;

j = mid + 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++;

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;

cout<<"Left Partition: ";

for(int i=low;i<=mid;i++)

cout<<a[i]<<" ";

cout<<"\nRight Partition: ";

for(int i=mid+1;i<=high;i++)

cout<<a[i]<<" ";

cout<<endl<<endl;

MergeSort(a, low, mid);

MergeSort(a, mid+1, high);


Merge(a, low, high, mid);

int main()

int n;

n=rand()%100;

int arr[n];

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

arr[i]=rand()%100+1;

MergeSort(arr, 0, n-1);

cout<<"\nSorted Data ";

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

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

return 0;

Output:
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
QUE2: Quick sort
Code:

#include<iostream>

#include<stdio.h>

using namespace std;

int partition(int a[],int l, int r)

int p=l;

r++;

do

do

l++;
}while(a[l]<a[p]);

do

r--;

}while(a[r]>a[p]);

swap(a[l],a[r]);

}while(l<r);

swap(a[l],a[r]);

swap(a[p],a[r]);

return r;

void quicksort(int a[], int l, int r,int n)

if(l>=r)

return;

cout<<"Considered Array:\t";

for(int i=l;i<=r;i++)

cout<<a[i]<<" ";

cout<<endl;
int p=partition(a,l,r);

cout<<"\nLeft partition: ";

for(int i=l;i<p;i++)

cout<<a[i]<<" ";

cout<<"\nPivot after partition: ";

cout<<"|"<<a[p]<<"| ";

cout<<"\nRight partition: ";

for(int i=p+1;i<=r;i++)

cout<<a[i]<<" ";

cout<<endl<<endl;

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

cout<<a[i]<<" ";

cout<<endl<<endl;

quicksort(a,l,p-1,n);

quicksort(a,p+1,r,n);

int main()
{

// int a[]={5,3,1,9,8,2,4,7};

// quicksort(a,0,7);

// for(int i=0;i<8;i++)

// {

// cout<<a[i]<<" ";

// }

int n=100;

int a[n];

a[0]=rand()%1000;

for(int i=1;i<n;i++)

a[i]=a[i-1]+rand()%1000;

quicksort(a,0,n-1,n);

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

cout<<a[i]<<" ";

Output:

You might also like