5/01/2009

C Program Quick Sort


/*C Program Quick Sort*/
#include<stdio.h>
#include<conio.h>
int partition(int a[],int low,int high)
{
    int i,j,temp,key;
    key=a[low];
    i=low+1;
    j=high;
    while(1)
    {
        while(i<high&&key>=a[i])
            i++;
        while(key<a[j])
            j--;
        if(i<j)
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
        else
        {
            temp=a[low];
            a[low]=a[j];
            a[j]=temp;
            return j;
        }
    }
}
void quick_sort(int a[],int low,int high)
{
    int j;
    if(low<high)
    {
        j=partition(a,low,high);
        quick_sort(a,low,j-1);
        quick_sort(a,j+1,high);
    }
}
void main()
{
    int i,n,a[20];

    printf(" enter no. of ele \n");
    scanf("%d",&n);
    printf("enter the ele \n");
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    quick_sort(a,0,n-1);
    printf("sorted list is \n");
    for(i=0; i<n; i++)
        printf(" %d \n",a[i]);

}
Out Put:
Enter number of elements:
8
enter the ele
32
45
8
15
78
122
35
47
sorted list is
 8
 15
 32
 35
 45
 47
 78
 122

No comments:

Post a Comment