//Quicksort program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
const int MAX_ELEMENTS = 10;
void swap(int *a,int *B)/>
{
int temp=*a;
*a=*b;
*b=temp;
}
void printlist(int list[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",list[i]);
}
void partition(int list[],int low,int high,int pivot)
{
int x,left,right;
x=list[low];
right=high;
left=low+1;
while(left<right)
{
while((list[left]<=x) && (left<right))
left++;
while(list[right]>x)
right--;
if(list[left]<list[right])
{
swap(&list[right],&list[left]);
left++;
right--;
}
}
swap(&list[low],&list[right]);
}
int quicksort(int list[],int low,int high)
{
if(low>high)
return 0;
int pos=low;
partition(list,low,high,pos);
quicksort(list,low,pos-1);
quicksort(list,pos+1,high);
}
int main()
{
int list[MAX_ELEMENTS];
int i = 0;
for(i = 0; i < MAX_ELEMENTS; i++ )
list[i] = rand();
printf("The list before sorting is:\n");
printlist(list,MAX_ELEMENTS);
quicksort(list,0,MAX_ELEMENTS-1);
printf("The list after sorting using quicksort algorithm:\n");
printlist(list,MAX_ELEMENTS);
return 0;
}
I think there is a logical problem with the code( plus i have a suspicion that the recursive call is not working because i am not doing the call correctly). I am not clear on the concept of quicksort. If someone could point out the mistakes in the code it would be appreciated. Thank you

New Topic/Question
Reply



MultiQuote





|