#include<stdio.h>
#define size 10
int a[size];

int partition(int beg, int end)
{
	int left, right, temp, loc, flag;
	loc = left = beg;
	right = end;
	flag = 0;
	while(flag != 1)
	{
		while((a[loc] <= a[right]) && (loc != right))
			right--;
		if(loc == right)
			flag = 1;
		else if(a[loc] > a[right])
		{
			temp = a[loc];
			a[loc] = a[right];
			a[right] = temp;
			loc = right;
		}
		if(flag != 1)
		{
			while((a[loc] >= a[left]) && (loc != left))
				left--;
			if(loc == left)
				flag = 1;
			else if(a[loc] < a[left])
			{
				temp = a[loc];
				a[loc] = a[left];
				a[left] = temp;
				loc = left;
			}
		}
	}
	return loc;
}

void quick_sort(int beg, int end)
{
	int loc;
	if(beg < end)
	{
		loc = partition(beg, end);
		quick_sort(beg, loc-1);
		quick_sort(loc+1, end);
	}
}

void main()
{
	int i, n;
	printf("Plesae enter the total number of elements :\n");
	scanf("%d",&n);
	printf("Please enter the elements of the array : \n");
	for(i=0; i<n; i++)
		scanf("%d", &a[i]);
	quick_sort(0, n-1);
	printf("The sorted arrary : \n");
	for(i=0; i<n; i++)
		printf("%d\t", a[i]);
}
