C Program to sort elements using QUICK SORT

C programming code for  QUICK SORT  to sort numbers or arrange them in ascending order.

#include <stdio.h>
 #include <conio.h>
void quicksort(int [],int,int);
 int split(int [],int,int);
int main()
 {
 int arr[20],i,n;
 printf("*******QUICK SORT********nn ");
 printf("Enter the size of the array : ");
 scanf("%d",&n);
printf("nEnter the elements : n");
 for(i=0;i<n;i++)
 scanf("%d",&arr[i]);
printf("nOriginal array : ");
 for(int i=0;i<n;i++)
 printf("%dt",arr[i]);
quicksort(arr,0,n);
printf("nnSorted Array : ");
 for(i=0;i<n;i++)
 printf("%dt",arr[i]);
//return 0;
 getch();
 }
void quicksort(int a[],int lower,int upper)
 {
 int i;
 if(upper>lower)
 {
 i=split(a,lower,upper);
 quicksort(a,lower,i-1);
 quicksort(a,i+1,upper);
 }
 }
int split(int a[],int lower,int upper)
 {
 int i,p,q,t;
 p=lower+1;
 q=upper;
 i=a[lower];
while(q>=p)
 {
 while(a[p]<i)
 p++;
while(a[q]>i)
 q--;
if(q>p)
 {
 t=a[p];
 a[p]=a[q];
 a[q]=t;
 }
 }
t=a[lower];
 a[lower]=a[q];
 a[q]=t;
return q;
 }

OUTPUT

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s