Quick Sort (Data Structures)

 #include<stdio.h>

#include<conio.h>


quick_sort(int a[],int low,int high)

{


 int i=low,j=high,temp,pivot,m=(low+high)/2;

 pivot=a[m];

 while(a[i]<pivot)

 i++;

 while(a[j]>pivot)

 j--;

 if(i<=j)

 {

  temp=a[i];

  a[i]=a[j];

  a[j]=temp;

  i++;

  j--;

 }

 if(low<j)

 {

 quick_sort(a,low,j);

 }

 if(i<high)

 {

 quick_sort(a,i,high);

 }


}

void main()


{

 int a[20],i;

 clrscr();

   // printf("Enter Array Size:");

   // scanf("%d",&n);

 printf("Enter Element:");

 for(i=0;i<=4;i++)

 scanf("%d",&a[i]);

 quick_sort(a,0,5-1);

 printf("Sorted Elements");

 for(i=0;i<=4;i++)

 printf("\n%d\n",a[i]);



getch();

}

Previous Post Next Post