Algorithm to find the ith Smallest Number