Code:-: Name: Afzal Pathan

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

NAME : AFZAL PATHAN

REG.NO : 21BCI0315

SUBJECT CODE : BCSE204P

TITLE: : DIGITAL ASSIGNMENT 4

SLOT : L29+L30

Code :-
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<limits.h>
#include<unistd.h>
#define INF INT_MAX
void swap(int* a,int* b)
{
int temp=*a;
*a=*b;
*b=temp;
}
//QUICK SORT with right pivot
int rPartition(int a[],int p,int r)
{
int pivot=a[r];
int i=(p-1);
int j;
for(j=p;j<=r-1;j++)
{
if(a[j]<pivot)
{
i++;
int temp=a[i]; //swap
a[i]=a[j];
a[j]=temp;
}
}
int temp=a[i+1]; //swap
a[i+1]=a[r];
a[r]=temp;
return (i+1);
}
void rQuickSort(int a[],int p,int r)
{
if(p<r)
{
int q=rPartition(a,p,r);
rQuickSort(a,p,q-1);
rQuickSort(a,q+1,r);
}
}
//Quick Sort with middle element as pivot
int mPartition(int a[],int p,int r)
{
int mid=(p+r)/2;
swap(&a[mid],&a[r]);
int pivot=a[r];
int i=(p-1);
int j;
for(j=p;j<=r-1;j++)
{
if(a[j]<pivot)
{
i++;
int temp=a[i]; //swap
a[i]=a[j];
a[j]=temp;
}
}
int temp=a[i+1]; //swap
a[i+1]=a[r];
a[r]=temp;
return (i+1);
}
void mQuickSort(int a[],int p,int r)
{
if(p<r)
{
int q=mPartition(a,p,r);
mQuickSort(a,p,q-1);
mQuickSort(a,q+1,r);
}
}
//Random Quick Sort
int Random_partition(int a[],int p, int r)
{
int random=p+rand()%(r-p+1);
//printf("r is %d",random);
swap(&a[random],&a[r]);
int q=rPartition(a,p,r);
return q;
}
void random_QuickSort(int a[],int p,int r)
{
if(p<r)
{
int q=Random_partition(a,p,r);
random_QuickSort(a,p,q-1);
random_QuickSort(a,q+1,r);
}
}
int main()
{
int x,i,j;
int a[100000];
double table[3][10];
for(x=0;x<3;x++)
{
int k=0;
for(i=10000;i<=100000;i=i+10000)
{
for(j=0;j<i;j++)
{
a[j]=rand();
}
clock_t begin = clock();
switch(x)
{
case 0:
rQuickSort(a,0,i-1);
break;
case 1:
mQuickSort(a,0,i-1);
break;
case 2:
random_QuickSort(a,0,i-1);
}
clock_t end=clock();
double time = ((double)(end-begin))/CLOCKS_PER_SEC; // in seconds
table[x][k++]=time;
}
}
printf("\nThe table is :\n");
for(i=0;i<3;i++)
{
if(i==0)
{
printf("Right Element as pivot ");
}
else if(i==1)
{
printf("Middle Element as pivot ");
}
else if(i==2)
{
printf("Random Element as pivot ");
}
for(j=0;j<10;j++)
{
printf("%.2lf ",table[i][j]);
}
printf("\n");
}
return 0;
}
Graph :-

In python:
OUTPUT:
The key process in quickSort is a partition().
It picks an element as a pivot and partitions the given array around the
picked pivot. There are many different versions of quickSort that pick pivot in
different ways.
• Always pick the first element as a pivot.
• Always pick the last element as a pivot
• Pick a random element as a pivot.
• Pick median as the pivot.
T(n) = T(k) + T(n-k-1) + (n)
Worst Case:
The worst case occurs when the partition process always picks the greatest
or smallest element as the pivot. If we consider the above partition strategy
where the last element is always picked as a pivot, the worst case would
occur when the array is already sorted in increasing or decreasing order.
T(n) = T(0) + T(n-1) + (n)which is equivalent to T(n) = T(n-1) + (n)
The solution to the above recurrence is (n2).
Best Case:
The best case occurs when the partition process always picks the middle
element as the pivot. The following is recurrence for the best case.
T(n) = 2T(n/2) + (n)

Average Case:
To do average case analysis, We can get an idea of average case by
considering the case when partition puts O(n/9) elements in one set and
O(9n/10) elements in other set. Following is recurrence for this case.
T(n) = T(n/9) + T(9n/10) + (n)

Randomized quick Sort :-


Randomized QuickSort
1) Pick a pivot element uniformly at random from the array
2) Split array into 3 subarrays: those smaller than pivot, those larger than pivot, and
the pivot itself.
3) Recursively sort the subarrays, and concatenate them.
Conclusion :-
“This graph is successfully verified by the above plot…..”

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