Coding Lab - TechOnTechnology

Program in C++ to demonstrate Quick Sort.




#include<iostream.h>
#include<conio.h>
void Display(int* array, int n);
void QuickSort(int* array, int low, int high);
int Partition(int* array, int ItemValue, int low, int high);
void swap(int &a, int &b);
void main()
{
    int i,n,array[50];
    clrscr();
    cout<<"\nHow many numbers you have ?\n";
    cin>>n;
    cout<<"Enter "<<n<<" Values :\n";
    for( i = 0; i < n; i++)
    {cin>>array[i];}
    cout<<"\nThe given list of numbers : \n";
    Display(array,n);
    QuickSort(array,0,n-1);
    cout<<"\nThe sorted list of numbers : \n";
    Display(array,n);
    getch();
}
void swap(int &a, int &b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}
void Display(int* array, int n)
{
    int i;
    for( i = 0; i < n; i++) cout<<array[i]<<"  ";
}
void QuickSort(int* array, int low, int high)
{
    int Item = array[low];
    int splitPoint;
    if(high > low)
    {
        splitPoint = Partition(array, Item, low, high);
        array[splitPoint] = Item;
        QuickSort(array, low, splitPoint-1);
        QuickSort(array, splitPoint+1, high);
    }
}
int Partition(int* array, int Item, int low, int high)
{
    int L = low;
    int R = high;
    while(L < R)
    {
         while( Item < array[R] && R > L){R--;}
         swap(array[L], array[R]);
         while( Item >= array[L] && L < R){L++;}
         swap(array[R], array[L]);
    }
    return L;
}

Name

Email *

Message *