Write a C Program to implement the concept of Quick Sort

Program:-

#include<stdio.h>
void QuickSort(int array[],int start,int end);
main()
{
int i,j,array[100],num;
printf("Enter Number of Elements in Array\n");
scanf("%d",&num);
printf("Enter Elements of Array\n");
for(i=0;i<num;i++) scanf("%d",&array[i]);
QuickSort(array,0,num);
printf("Array after Sorting\n");
for(i=0;i<num;i++) printf("%d ",array[i]);
printf("\n");

}
void QuickSort(int array[],int start,int end)
{
int index=start,i,pivot=array[end],temp;
if(start<end)
{
for(i=start;i<end;i++)
{
if(array[i]<=pivot)
{
temp=array[i];
array[i]=array[index];
array[index]=temp;
index++;

}

}
temp=array[index];
array[index]=array[end];
array[end]=temp;
QuickSort(array,start,index-1);
QuickSort(array,index+1,end);

}

}

Output:-

Enter Number of Elements in Array
5
Enter Elements of Array
35
30
45
12
90
Array after Sorting
12 30 35 45 90

Post a Comment

0 Comments