Our Feeds

Monday 16 November 2015

AJITH KP

Quicksort - Lomuto partition scheme in C++

Hello GuyZ,
     This is another question you have to be solved in your MCA/BCA/BTech cource. The source code is to solve Quicksort in Lomuto partition scheme.


Source Code



/*
     Coded by Ajith Kp [ajithkp560]
     [*] www.terminalcoders.blogspot.com [*]
*/
#include <iostream>
using namespace std;
class quick
{
    int a[100], n, i, j;
    public:
        void read()
        {
            cout<<"Enter the limit: ";
            cin>>n;
            cout<<"Enter "<<n<<" Elements: ";
            for(i=0;i<n;i++)
            {
                cin>>a[i];
            }
            quick_sort(0, n-1);
            cout<<"Sorted List: ";
            for(i=0;i<n;i++)
            {
                cout<<a[i]<<" ";
            }
            cout<<"\n";
        }
        
        void quick_sort(int f, int l)
        {
            int j;
            if(f<l)
            {
                j = partition(f, l);
                quick_sort(f, j-1);
                quick_sort(j+1, l);
            }
        }
        int partition(int f, int l)
        {
            int pivot, i, j, tmp;
            pivot = a[l];
            i = f;
            for(j=f;j<l;j++){
                if(a[j]<=pivot){
                    swap(i, j);
                    i++;
                }
            }
            swap(i, l);
            return i;
        }
        void swap(int i, int j)
        {
            int tmp;
            tmp = a[i];
            a[i] = a[j];
            a[j] = tmp;
        }
};
int main()
{
    quick q;
    q.read();
    cin.get();
}