Program to sort given element using Quick Sort in C | Wave the world

Program to sort given element using Quick Sort in C

/* Quick Sort Algorithm
    Created By: Pirate
*/

#include<stdio.h>
#include<conio.h>
int a[100];
void quick_sort(int a[],int p,int r);
int partition(int a[],int p,int r);
void main()
{
            int size,i;
            clrscr();
            printf("** QUICK SORT **\n");
            printf("Enter the no of element of array\n");
            scanf("%d",&size);
            printf("\nEnter %d elements\n",size);
            for(i=0;i<size;i++)
            {
                        scanf("%d",&a[i]);
            }
            quick_sort(a,0,size-1);
            printf("**********************************\n");
            printf("After Sorting\n");
            for(i=0;i<size;i++)
            {
                        printf("%d\t",a[i]);
            }
            getch();
}
void quick_sort(int a[],int p,int r)
{
            int q;
            if(p<r)
            {
                        q=partition(a,p,r);
                        quick_sort(a,p,q-1);
                        quick_sort(a,q+1,r);
            }
}
int partition(int a[],int p,int r)
{
            int x,i,j,t;
            x=a[r];
            i=p-1;
            for(j=p;j<=r-1;j++)
            {
                        if(a[j]<=x)
                        {
                                    i=i+1;
                                    t=a[i];
                                    a[i]=a[j];
                                    a[j]=t;
                        }
            }
            t=a[i+1];
            a[i+1]=a[r];
            a[r]=t;
            return i+1;
}


Output


























0 comments:

Post a Comment

 

Pro

About