#include<iostream>
using namespace std ;
int partition (int x[] , int lb , int ub )
{
int a , i , j , temp;
i = lb -1;
j = ub +1 ;
a = x[ub] ;
while (1)
{
do
{
j--;
}while (x[j] >= a); // repeat this loop until you dont get a value that is a[j] <= a
do
{
i++;
}while (x[i] <= a); // repeat this loop until you dont get a value that is a[i] >= a
if (i <j) // i think it is less than or equal to (but no equal to doesnt need to be swapped
{
temp = x[j] ;
x[j]= x[i] ;
x[i] = temp;
}
else if ( i > j )
{
return j ;
}
}
}
void quicksort ( int x[] ,int lb , int ub)
{
int newp;
if ( lb < ub )
{
newp = partition (x , lb , ub );
quicksort (x , lb , newp ) ;
quicksort (x, newp + 1 , ub ) ;
return ;
}
else if (lb >= ub )
{
return ;
}
}
void display (int x[] , int size)
{
for (int i =0 ; i < size ;i++)
{
cout << x[i] << endl;
}
}
int main ()
{
int size;
int num;
int a[100] ;
cout << "Enter a size of array :" ;
cin >> size;
cout << "Enter the values inside the array: " ;
for (int i =0 ; i< size ;i++)
{
cin >> num ;
a[i] = num ;
}
quicksort (a , 0 , size -1 ) ;
display (a, size);
return 0 ;
}