fixed the code to work best in almost sorted arrays, and added the extra feature that original order among themselves will be maintained for values which are equal