Implement Quick Sort and Display corresponding list in each pass. (with Recursion)
#include<stdio.h>
#include<string.h>
int partition(int arr[100],int low,int high)
{
float temp;
int pivot,i,j;
pivot=low;
i=low;
j=high;
while(i<j)
{
while((arr[i]<=arr[pivot])&&(i<high))
{
i++;
}
while(arr[j]>arr[pivot])
{
j--;
}
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
temp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=temp;
return j;
}
void quicksort(int arr[100],int low,int high)
{
int i,j;
if(low<high)
{
j=partition(arr,low,high);
printf("\n");
for(i=0;i<5;i++)
printf("%d ",arr[i]);
quicksort(arr,low,j-1);
quicksort(arr,j+1,high);
}
}
int main()
{
setbuf(stdout, NULL);
int arr[100];
int i,n;
printf("\nEnter no of student:");
scanf("%d",&n);
printf("\nEnter student scores:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
quicksort(arr,0,n-1);
printf("\n*****After quick sort top five Scores****\n");
for(i=0;i<5;i++)
printf("%d ",arr[i]);
return 0;
}
Output:
Enter no of student:5
Enter student scores:10
2
5
7
34
7 2 5 10 34
5 2 7 10 34
2 5 7 10 34
*****After quick sort top five Scores****
2 5 7 10 34
No comments:
Post a Comment