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

48 Daa Lab2

The document contains C code for implementing three variations of quicksort: 1) Using a deterministic pivot element 2) Using a random pivot element with multiple iterations 3) Using a randomly permuted array with multiple iterations The code tracks the number of comparisons made during sorting and outputs the average comparison matrix after a specified number of iterations for variations 2 and 3.

Uploaded by

JIYAN PATIL
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)
24 views9 pages

48 Daa Lab2

The document contains C code for implementing three variations of quicksort: 1) Using a deterministic pivot element 2) Using a random pivot element with multiple iterations 3) Using a randomly permuted array with multiple iterations The code tracks the number of comparisons made during sorting and outputs the average comparison matrix after a specified number of iterations for variations 2 and 3.

Uploaded by

JIYAN PATIL
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/ 9

Name: Patil Jiyan

Afasarpasha
Roll. No: 48
PRN: 12220055
Class: TY CS-B

Assignment 2: Experimental analysis of Quick sort and


variants

Code 1: Quick Sort using Deterministic Pivot

#include<conio.h>
#include<stdio.h>
void quick_sort(int a[] , int , int);
int partition(int a[] ,int , int);
int cmp[50][50];
int main(){
int n , arr[50];
printf("Enter the number of elements in the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cmp[i][j] = cmp[j][i] = 0;
}

quick_sort(arr,0,n-1);
printf("\nThe array received after implementing quick Sort is\n");
for (int i = 0; i < n; i++)
{
printf("%d\t" , arr[i]);
}
printf("\nThe number of comparisons made are as follows\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d " , cmp[i][j]);
}
printf("\n");
}

}
void quick_sort(int a[] , int beg , int end){
int loc;
if(beg<end){
loc = partition(a , beg , end);
quick_sort(a , beg , loc-1);
quick_sort(a , loc+1 , end);
}
}
int partition(int a[] , int beg , int end){
int loc = beg , right = end , left = beg, flag = 0;
while(flag!=1){
while ((a[loc]<=a[right]) && (loc!=right))
{
cmp[a[loc]][a[right]]++;
cmp[a[right]][a[loc]]++;
right--;
}
if(loc==right){
flag = 1;
}
else if(a[loc]>a[right]){
int temp;
temp = a[loc];
a[loc] = a[right];
a[right]= temp;
loc = right;
}
if(flag!= 1){
while (a[loc]>=a[left] && (loc!=left))
{
cmp[a[left]][a[loc]]++;
cmp[a[loc]][a[left]]++;
left++;
}
if(loc==left){
flag = 1;
}
else if(a[loc]<a[left]){
int temp;
temp = a[loc];
a[loc] = a[left];
a[left]= temp;
loc = left;
}
}
}
return loc;
}

Output:

Code 2: Quick Sort using random Pivot element with multiple


iterations

#include <conio.h>
#include <stdio.h>
void quick_sort(int a[], int, int);
int partition(int a[], int, int);
float cmp[50][50];
int n;
int main()
{
int c;
printf("Enter the number of iterations\n");
scanf("%d", &c);
int n, arr[50];
printf("Enter the number of elements in the array\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cmp[i][j] = cmp[j][i] = 0;
}
}
for (size_t i = 0; i < c; i++)
{
for (int i = 0; i < n; i++)
{
arr[i] = i;
}
quick_sort(arr, 0, n - 1);
}
printf("The average matrix formed after %d number of
iterations is\n" , c);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%.2f ", (cmp[i][j] / c));
}
printf("\n");
}
}
void quick_sort(int a[], int beg, int end)
{
int loc;
if (beg < end)
{
loc = partition(a, beg, end);
quick_sort(a, beg, loc - 1);
quick_sort(a, loc + 1, end);
}
}
int partition(int a[], int beg, int end)
{
int right = end, left = beg, flag = 0;
int loc = (rand() % (end - beg + 1)) + beg;
while (flag != 1)
{
while ((a[loc] <= a[right]) && (loc != right))
{
cmp[a[loc]][a[right]]++;
cmp[a[right]][a[loc]]++;
right--;
}
if (loc == right)
{
flag = 1;
}
else if (a[loc] > a[right])
{
int temp;
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if (flag != 1)
{
while (a[loc] >= a[left] && (loc != left))
{
cmp[a[left]][a[loc]]++;
cmp[a[loc]][a[left]]++;
left++;
}
if (loc == left)
{
flag = 1;
}
else if (a[loc] < a[left])
{
int temp;
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
return loc;
}

Output:

Code 3: Quick sort using random Permutated Array with multiple iterations
#include <conio.h>
#include <stdio.h>
void quick_sort(int a[], int, int);
int partition(int a[], int, int);
float cmp[50][50];
int main()
{
int c;
printf("Enter the number of iterations\n");
scanf("%d", &c);
int n, arr[50];
printf("Enter the number of elements in the array\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cmp[i][j] = cmp[j][i] = 0;
}
}
for (int i = 0; i < c; i++)
{
for (int i = 0; i < n; i++)
{
arr[i] = i;
}
for (int i = n - 1; i >= 1; i--)
{
int k = rand() % i;
// printf("%d" , k);
int temp = arr[k];
arr[k] = arr[i];
arr[i] = temp;
}
quick_sort(arr, 0, n - 1);
}

// printf("The random array formed is \n");


// for (int i = 0; i < n; i++)
// {
// printf("%d\t" , arr[i]);
// }
// quick_sort(arr,0,n-1);
// printf("\nThe array received after implementing quick Sort
is\n");
// for (int i = 0; i < n; i++)
// {
// printf("%d\t" , arr[i]);
// }
printf("\nThe number of comparisons made after %d iterations are as
follows\n",c);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%.2f ", (cmp[i][j] / c));
}
printf("\n");
}
}
void quick_sort(int a[], int beg, int end)
{
int loc;
if (beg < end)
{
loc = partition(a, beg, end);
quick_sort(a, beg, loc - 1);
quick_sort(a, loc + 1, end);
}
}
int partition(int a[], int beg, int end)
{
int loc = beg, right = end, left = beg, flag = 0;
while (flag != 1)
{
while ((a[loc] <= a[right]) && (loc != right))
{
cmp[a[loc]][a[right]]++;
cmp[a[right]][a[loc]]++;
right--;
}
if (loc == right)
{
flag = 1;
}
else if (a[loc] > a[right])
{
int temp;
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if (flag != 1)
{
while (a[loc] >= a[left] && (loc != left))
{
cmp[a[left]][a[loc]]++;
cmp[a[loc]][a[left]]++;
left++;
}
if (loc == left)
{
flag = 1;
}
else if (a[loc] < a[left])
{
int temp;
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
return loc;
}

Output:

You might also like