Advertisement
EXTREMEXPLOIT

CycleSort

Jan 14th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. def CycleSort(MyArray):
  2.   Writes = 0
  3.    
  4.   for cycleStart in range(0, len(MyArray) - 1):
  5.     Item = MyArray[cycleStart]
  6.      
  7.     Position = cycleStart
  8.     for i in range(cycleStart + 1, len(MyArray)):
  9.       if MyArray[i] < Item:
  10.         Position += 1
  11.  
  12.     if Position == cycleStart:
  13.       continue
  14.  
  15.     while Item == MyArray[Position]:
  16.       Position += 1
  17.     MyArray[Position], Item = Item, MyArray[Position]
  18.     Writes += 1
  19.  
  20.     while Position != cycleStart:
  21.       Position = cycleStart
  22.       for i in range(cycleStart + 1, len(MyArray)):
  23.         if MyArray[i] < Item:
  24.           Position += 1
  25.        
  26.       while Item == MyArray[Position]:
  27.         Position += 1
  28.       MyArray[Position], Item = Item, MyArray[Position]
  29.       Writes += 1
  30.    
  31.   return MyArray
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement