Proper solution based on quick sort algorithm.