I am trying to implement sorting of array of structures using quick sort but its not even taking input correctly. The exact question is: We have to sort grocery items according to their names whose names and sales price is given to us, in increasing order.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct comp
{
int salep;
char pname[50];
};
void swap(struct comp *x,struct comp *y)
{
struct comp temp=*x;
*x=*y;
*y=temp;
}
int partition(struct comp item[50],int lb,int ub)
{
char pivot[50];
strcpy(pivot,item[lb].pname);
int start=lb;
int end=ub;
while(start<end)
{
while(item[start].pname<=pivot)
start++;
while(item[end].pname>pivot)
end--;
if(start<end)
swap(&item[start],&item[end]);
}
}
quickSort(struct comp item[50],int lb,int ub)
{
if(lb<ub)
{
int loc=partition(item,lb,ub);
quickSort(item,lb,loc-1);
quickSort(item,loc+1,ub);
}
}
int main()
{
struct comp item[50];
int n;
printf("Enter number of items:\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter product %d name:\n",i);
scanf("%[^\n]",item[i].pname);
printf("Enter sales percentage:\n");
scanf("%d",&item[i].salep);
}
quickSort(item,0,n);
printf("Sorted List:\n");
for(int i=0;i<n;i++)
{
puts(item[i].pname);
printf(item[i].salep);
}
return 0;
}
I am getting the following result
Enter number of items:
2
Enter product 0 name:
Enter sales percentage: