Sorting the required segment
p = A [ : i ] NEW_LINE q = A [ i : i + j + 1 ] NEW_LINE r = A [ i + j + 1 : len ( A ) ] NEW_LINE q . sort ( reverse = False ) NEW_LINE A = p + q + r NEW_LINE i = j NEW_LINE