Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions sorting/3_way_quicksort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdio.h>
int common=0;
void swap (int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
return;
}

int partition(int *a,int start,int end)
{
int i=0;
int pivot_index=0;
int pivot=a[end];
if (start==end)
return pivot_index;

for (i=0;i<end;i++)
{
if (a[i]<pivot)
{
swap(&a[i],&a[pivot_index]);
pivot_index++;
}
}
int temp=pivot_index;
for (i=0;i<end;i++)
{
if (a[i]==pivot)
{
swap(&a[i],&a[pivot_index]);
pivot_index++;
}
}
common=pivot_index-temp;
swap(&a[pivot_index],&a[end]);
return pivot_index;
}

void quicksort (int * a, int start,int end)
{
if (start>=end)
return;

int pivot=partition(a,start,end);
int temp=common;
common=0;
quicksort(a,start,pivot-temp-1);
quicksort(a,pivot+1,end);
}

void print(int *a,int n)
{
int i=0;
for(i=0;i<n;i++)
{
printf("%d",a[i]);
if(i==n-1)
printf(". \n");
else printf(", ");
}
}

int main(void)
{
int i=0;
printf("Please enter the size of array:\n");
int n;
scanf("%d",&n);
int a[n];
printf("Please enter the elements of array:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
print(a,n);
quicksort(a,0,n-1);
print(a,n);

return 0;
}