sorted algorithm in python implementation