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

Superior University Lahore: Usman Afzal Bcsm-F17-063 Cs 4B

The document describes code implementations for quicksort and merge sort algorithms to sort arrays of integers. It includes functions to implement the sorting logic, swap values, partition arrays for quicksort, and merge sorted subarrays for merge sort. Code snippets are provided to test the sorting algorithms on sample integer arrays and output the results.

Uploaded by

USMAN AFZAL
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)
21 views7 pages

Superior University Lahore: Usman Afzal Bcsm-F17-063 Cs 4B

The document describes code implementations for quicksort and merge sort algorithms to sort arrays of integers. It includes functions to implement the sorting logic, swap values, partition arrays for quicksort, and merge sorted subarrays for merge sort. Code snippets are provided to test the sorting algorithms on sample integer arrays and output the results.

Uploaded by

USMAN AFZAL
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/ 7

Superior University Lahore

Assignment
Design and Algorithm

Name: Usman Afzal


Roll No: Bcsm-F17-063
Section: CS 4B
Quick sort
#include <iostream>
using namespace std;
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}

Merge sort
#include<iostream>
using namespace std;
int Merge(int A[],int p, int q,int r)
{
int n1,n2,i,j,k;
n1=q-p+1;
n2=r-q;
int L[n1],R[n2];
for(i=0;i<n1;i++)
{
L[i]=A[p+i];
}
for(j=0;j<n2;j++)
{
R[j]=A[q+j+1];
}
i=0,j=0;
for(k=p;i<n1&&j<n2;k++)
{
if(L[i]<R[j])
{
A[k]=L[i++];
}
else
{
A[k]=R[j++];
}
}
while(i<n1)
{
A[k++]=L[i++];
}
while(j<n2)
{
A[k++]=R[j++];
}
}
int MergeSort(int A[],int p,int r)
{
int q;
if(p<r)
{
q=(p+r)/2;
MergeSort(A,p,q);
MergeSort(A,q+1,r);
Merge(A,p,q,r);
}
}
int main()
{
int n;
cout<<"Enter size of the Array: ";
cin>>n;
int A[n],i;
cout<<"Enter array values:\n";
for(i=0;i<n;i++)
cin>>A[i];
MergeSort(A,0,n-1);
cout<<"The Sorted List is\n";
for(i=0;i<n;i++)
{
cout<<A[i]<<" ";
}
return 0;
}

You might also like