CrazyEngineers
  • Brief explanation of Quick Sort algorithm

    Manish Goyal

    Member

    Updated: Oct 22, 2024
    Views: 1.0K
    On request of one user from ce,i am going to explain one of the most commonly used sorting algorithm ie quick sort

    It is based on Divide and conquer approach.

    It is one of the common practical example of recursion

    There are generally 3 steps in this

    1:-Divide a problem a multiple sub problems
    2:-Solve them individually.
    3:-Merge those sub problems to find solution of main problem

    Algorithm

    1. Choose any key element in an array which is know a pivot.
    2. Reorder the array such that that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it. After the partitioning, the pivot is in its final position.
    3. Recursively reorder two sub-lists: the sub-list of lesser elements and the sub-list of greater elements.


    Below is the program written in c++ ,it can help you for better understanding

    //Program for Quicksort
    #include<iostream.h>
    #include<conio.h>
    int partition(int a[],int,int);
    void quick(int a[],int,int);
    void main()
    {
    int *a,n,i;
    cout<<"Enter size of array"<<endl;
    cin>>n;
    a=new int[n];
    cout<<"Please enter elements for array"<<endl;
    for(i=0;i<n;i++)
    {
    cin>>a;
    }
    quick(a,0,n-1);
    cout<<"Elments are"<<endl;
    for(i=0;i<n;i++)
    {
    cout<<a<<endl;
    }
    getch();
    }
    void quick(int a[],int start,int finish)
    {
    if(start<finish)
    {
    int q=partition(a,start,finish);
    quick(a,start,q-1);
    quick(a,q+1,finish);
    }
    }
    int partition(int a[],int start,int finish)
    {
    int x=a[finish],temp,temp1,j;
    int i=start-1;
    for(j=start;j<=finish-1;j++)
    {
    if(a[j]<=x)
    {
    i=i+1;
    temp=a;
    a=a[j];
    a[j]=temp;
    }
    }
    temp1=a[i+1];
    a[i+1]=a[j];
    a[j]=temp1;
    return (i+1);
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • rishi0922

    MemberSep 2, 2010

    its very helpful nd clearing many doubts
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberSep 2, 2010

    Nice post goyal.
    Hope you come with more in future.
    Are you sure? This action cannot be undone.
    Cancel
  • paresh006

    MemberSep 4, 2010

    Quick sort like merge sort,is based on divide and conquer paradigm..
    Three steps are required to be done these are:-

    Divide:-Suppose an array is A[p...r]...rearrange the array into two(possibly empty)subarrays A[p....q-1]and A[q+1...r] such that each element of A[p....q-1]is less than or equal to A[q],which is,in turn, less than or equal to each element of A[q+1...r]..compute the index q as part of this partitioning procedure.

    Conquer:-Sort the two subarrays A[p...q-1]and A[q+1...r]by recursive calls to quicksort.

    Combine:-Since the subarrays are sorted in place,no work is needed to combine them.the entire array A[p..r]is now sorted..

    QUICKSORT(A,p,r)

    1:if p<r
    2: then q<-PARTITIONED(A,p,r)
    3: QUICKSORT(A,p,q-1)
    4: QUICKSORT(A,q+1,r)
    To sort an entire array A,the initial call is QUICKSORT(A,1,length[A]).

    Partitioning the array

    PARTITION(A,p,r)
    1:x<-A[r]
    2:i<-p-1
    3: for j<-p to r-1
    4: do if A[j]<=x
    5: then i<-i+1
    6: exchange A<->A[j]
    7:exchangeA[i+1]<->A[r]
    8:return i+1
    Are you sure? This action cannot be undone.
    Cancel
  • BCA_GIRL

    MemberSep 4, 2010

    Thanks..........really its very useful.
    Are you sure? This action cannot be undone.
    Cancel
  • anandkumarjha

    MemberSep 4, 2010

    Fabulous job Goyal...It's easy to learn by this method

    hope that you will bribg such useful stuff regularly
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register