Quick Sort Code

//Quick Sort

#include<stdio.h>
void quick_sort(int*,int,int);
int a[5],i;
main()
{
 
 printf("Enter Five values\n\n");
 for(i=0;i<5;i++)
 {
  scanf("%d",&a[i]);
 }
 quick_sort(a,0,4);
 printf("\nFinal Sorted values\n\n");
 for(i=0;i<5;i++)
 {
  printf("%d",a[i]);
 }
 return 0;
}
void quick_sort(int*p,int first,int last)
{
 int low,high,temp,pivot;
 if(first<last){
 low=first;high=last;pivot=first;
 while(low<high)
 {
      while(p[low]<=p[pivot])
        low++;
      while(p[high]>p[pivot])
         high--;
         if(low<high)
        {
      
        temp=p[low];
        p[low]=p[high];
        p[high]=temp;
    
        }
        
    }
         temp=p[pivot];
         p[pivot]=p[high];
         p[high]=temp;
        quick_sort(p,first,high-1);
        quick_sort(p,high+1,last);
         
    }
}

No comments:

Post a Comment