Quicksort
It take on an average O(nlogn) (big O notation) to sort n items. However, in the worst case, it makes O(n^2) comparisons. Quicksort is faster in practice than other O(nlogn) algorithms.
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b)
{
printf("in swap %d %d",*a,*b);
int c=0;
c=*a;
*a=*b;
*b=c;
}
int quicksort(int *arr,int lb,int ub)
{
int i;
for(i=0;i<5;i++)
printf(" %d ",arr[i]);
if(ub>lb)
{
int pivotvalue = arr[lb];
printf("\nlowest value is %d",arr[lb]);
getch();
swap(&arr[lb],&arr[ub]);
getch();
printf(" value is %d , %d ",arr[lb],arr[ub]);
getch();
int storeindex = lb;
printf("\n storeindex = %d",storeindex);
getch();
int i;
for(i=lb;i<=ub-1;i++)
{
if(arr[i]<= pivotvalue)
{
getch();
printf("\n the value swapped is %d ,%d",arr[i],arr[storeindex]);
swap(&arr[i],&arr[storeindex]);
printf("\n the value after swapped is %d ,%d",arr[i],arr[storeindex]);
storeindex++;
printf("\n increment in store index = %d",storeindex);
getch();
}
}
swap(&arr[storeindex],&arr[ub]);
printf("\n finally the value swapped is %d ,%d at storeindex = %d",arr[ub],arr[storeindex],storeindex);
getch();
printf("recur1");
quicksort(arr,lb,storeindex-1);
getch();
printf("\nrecur2");
quicksort(arr,storeindex+1,ub);
printf("\nend recur");
getch();
}
else
{
getch();
printf("\nin else");
return 0;
}
}
int main()
{
int arr[5]={3,7,8,5,2};
quicksort(arr,0,4);
printf(" \n back in main");
int i;
for(i=0;i<5;i++)
printf(" %d " ,arr[i]);
getch();
return 0;
}
Download Program


