function xs = qsort(x)
% Sort input vector x in ascending order

	N=length(x); % size of the input vector
	if(N<=1)
		% it seems to be strange to expect values less than 1
		% but at the end we call this function with potentially empty vectors
		% besides an empty vector is still a vector
		xs=x;
		return;
	end

	pivot_index=floor(N/2); % index must be integer thus we take floor
	pivot_value=x(pivot_index);

	% now we will create two initially empty vectors 
	lesser=[];
	larger=[];
	% they will be filled with values larger and smaller then pivot value
	for i=1:N
		if (i == pivot_index)
			% this will skip one iteration since pivot point is special
			continue; 
		end
		if ( x(i) <= pivot_value )
			% notice how we concatenate value into a vector
			lesser=[lesser,x(i)]; 
		else
			larger=[larger,x(i)];
		end
	end
	% here we concatenate sorted by recursive call of qsort
	% the lesser and larger vectors with
	% the pivot value
	xs=[qsort(lesser), pivot_value, qsort(larger)];
end

		
