fork download
  1. import timeit
  2. def cycleSort(vector):
  3. writes = 0
  4.  
  5. # Loop through the vector to find cycles to rotate.
  6. for cycleStart, item in enumerate(vector):
  7.  
  8. # Find where to put the item.
  9. pos = cycleStart
  10. for item2 in vector[cycleStart + 1:]:
  11. if item2 < item:
  12. pos += 1
  13.  
  14. # If the item is already there, this is not a cycle.
  15. if pos == cycleStart:
  16. continue
  17.  
  18. # Otherwise, put the item there or right after any duplicates.
  19. while item == vector[pos]:
  20. pos += 1
  21. vector[pos], item = item, vector[pos]
  22. writes += 1
  23.  
  24. # Rotate the rest of the cycle.
  25. while pos != cycleStart:
  26.  
  27. # Find where to put the item.
  28. pos = cycleStart
  29. for item2 in vector[cycleStart + 1:]:
  30. if item2 < item:
  31. pos += 1
  32.  
  33. # Put the item there or right after any duplicates.
  34. while item == vector[pos]:
  35. pos += 1
  36. vector[pos], item = item, vector[pos]
  37. writes += 1
  38.  
  39. return writes
  40.  
  41.  
  42. x = [0, 1, 2, 2, 2, 2, 1, 9, 3.5, 5, 8, 4, 7, 0, 6]
  43. w = timeit.timeit(cycleSort(x), number = 10000)
  44. print w, x
Runtime error #stdin #stdout #stderr 0s 23304KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 43, in <module>
  File "/usr/lib/python2.7/timeit.py", line 237, in timeit
    return Timer(stmt, setup, timer).timeit(number)
  File "/usr/lib/python2.7/timeit.py", line 156, in __init__
    raise ValueError("stmt is neither a string nor callable")
ValueError: stmt is neither a string nor callable