Lab2 CS-B 54

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Name: Hrishikesh Kadival

Roll. No: 54
PRN: 12110438
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

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