Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
10-liner: Robotic Sorting solution in Clear category for Robot Sort by Stensen
def swapsort(array, answer=''):
""" Robotic Sorting Algorithm"""
arr = list(array) # convert the input from tuples to lists
# Run an infinite loop to check whether arr is sorted or not
while arr != sorted(arr):
for i in range(len(arr)-1): # Loop through each element and its neighbor
if arr[i] > arr[i+1]:
answer += f'{i}{i+1},'
arr[i:i+2] = arr[i:i+2][::-1] # Swap each sorted pair
return answer[:-1]
Oct. 31, 2020