• Now Online : 58
  • admin@codemyne.net

Introduction

Sorting: The term sorting means bringing some orderliness in the data, which are otherwise not ordered.

Quick sort: It is considered to be a fast method to sort the elements. The method is also called partition exchange sorting. The method is based on divide-and-conquer technique i.e. the entire list is divided into various partitions and sorting applied again and again on the partitions.
In this method, the list is divided into two, based on an element called the pivot element. Usually the first element considered to be pivot element. Now move the pivot element into its correct position in the list. The elements to the left of the pivot are less than the pivot while the elements to the right of the pivot are greater than the pivot. The process is reapplied to each of these partitions. This process proceeds till we get the sorted list of elements.

    #include <stdio.h>
    #define size 5
    int partition(int *ap,int left,int right, int pi )
    {
        int pv,i,temp,si=left;
        pv=ap[pi];
        temp=ap[pi];
       ap[pi]=ap[right];
       ap[right]=temp;
       si=left;
        for(i=left;i<=right-1;i++)
        {
            if(ap[i]<=pv)
            {
               temp=ap[i];
               ap[i]=ap[si];
               ap[si]=temp;
               si=si+1;
           }
       }
    temp=ap[right];
    ap[right]=ap[si];
    ap[si]=temp;
    return si;
    }
    void quicksort(int *a,int left,int right)
    {
        int pi,pinew;
        if(left<right)
        {
            pi=(left+right)/2;
            pinew=partition(a,left,right,pi);
            quicksort(a,pinew+1,right);
            quicksort(a,left,pinew-1);
        }
    }
    void aread(int *p ,int n)
    {
        int i;
        for(i=0;i<n;i++)
        scanf("%d",(p+i));
    }
    void aprint(int *p ,int n)
    {
            int i;
            for(i=0;i<n;i++)
            printf("%d ",*(p+i));
    }
    void main()
    {
        int a[size],i;
        printf("enter the values for A");
        aread(a,size);
        
        quicksort(a,0,size);
        aprint(a,size);
    }

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment