Sunday, October 20, 2013

Quick Sort for an array

Write a program to do quick sort for an array.

1 comment:

  1. #include
    #include

    void swap(int *a, int *b)
    {
    int temp=*a;
    *a=*b;
    *b=temp;
    }
    int partition(int a[], int p, int r)
    {
    int x=a[r];
    int i=p-1,j;
    for(j=p;j<r;j++)
    {
    if(a[j]<=x)
    {
    i++;
    swap(&a[i], &a[j]);
    }
    }
    swap(&a[i+1], &a[r]);
    return i+1;
    }
    void quickSort(int a[], int p, int r)
    {
    if(p<r)
    {
    int q=partition(a, p, r);
    quickSort(a, p, q-1);
    quickSort(a, q+1,r);
    }
    }
    int main()
    {
    int a[]={10, 9, 11, 2, 5, 1},i;
    int n=sizeof(a)/sizeof(a[0]);
    quickSort(a,0,n-1);
    for(i=0;i< n;i++)
    printf("%d ", a[i]);
    getch();
    return 0;
    }

    ReplyDelete