0% found this document useful (0 votes)
17 views

Quick Sort

The document describes an implementation of quicksort to sort integers, strings, and 2D arrays. It provides code snippets for functions to perform quicksort on each data type by partitioning the data around a pivot element and recursively sorting subarrays.

Uploaded by

GirishRaguvir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Quick Sort

The document describes an implementation of quicksort to sort integers, strings, and 2D arrays. It provides code snippets for functions to perform quicksort on each data type by partitioning the data around a pivot element and recursively sorting subarrays.

Uploaded by

GirishRaguvir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

void quicksort (int first, int last )

{
int pivot,j,i,temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot] &&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(first,j-1);
quicksort(j+1,last);
}
}
For strings
void quicksort (unsigned long int first, unsigned long int last )
{
unsigned long int pivot,j,i;
char temp[5010];
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(strcmp(x[i],x[pivot])<=0 &&i<last)
i++;
while(strcmp(x[j],x[pivot])>0)
j--;
if(i<j){
strcpy(temp,x[i]);
strcpy(x[i],x[j]);
strcpy(x[j],temp);
}
}
strcpy(temp,x[pivot]);
strcpy(x[pivot],x[j]);
strcpy(x[j],temp);

quicksort(first,j-1);
quicksort(j+1,last);
}
}
For 2-D array
void quicksort (long long int first, long long int last )
{
long long int pivot,j,i;long double temp,k;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i][1]<=x[pivot][1] &&i<last)
i++;
while(x[j][1]>x[pivot][1])
j--;
if(i<j){
temp=x[i][1];
x[i][1]=x[j][1];
x[j][1]=temp;
k=x[i][0];
x[i][0]=x[j][0];
x[j][0]=k;
}
}
temp=x[pivot][1];
x[pivot][1]=x[j][1];
x[j][1]=temp;
k=x[pivot][0];
x[pivot][0]=x[j][0];
x[j][0]=k;
quicksort(first,j-1);
quicksort(j+1,last);
}
}

You might also like