Remove boilerplate comments and unused variables (#2073)

This commit is contained in:
mateuszz0000 2020-06-07 23:05:22 +02:00 committed by GitHub
parent 20b21e5ec9
commit 6752e9c737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,42 +18,32 @@ def cycle_sort(array: list) -> list:
>>> cycle_sort([]) >>> cycle_sort([])
[] []
""" """
ans = 0 array_len = len(array)
for cycle_start in range(0, array_len - 1):
item = array[cycle_start]
# Pass through the array to find cycles to rotate. pos = cycle_start
for cycleStart in range(0, len(array) - 1): for i in range(cycle_start + 1, array_len):
item = array[cycleStart]
# finding the position for putting the item.
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
if array[i] < item: if array[i] < item:
pos += 1 pos += 1
# If the item is already present-not a cycle. if pos == cycle_start:
if pos == cycleStart:
continue continue
# Otherwise, put the item there or right after any duplicates.
while item == array[pos]: while item == array[pos]:
pos += 1 pos += 1
array[pos], item = item, array[pos] array[pos], item = item, array[pos]
ans += 1 while pos != cycle_start:
pos = cycle_start
# Rotate the rest of the cycle. for i in range(cycle_start + 1, array_len):
while pos != cycleStart:
# Find where to put the item.
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
if array[i] < item: if array[i] < item:
pos += 1 pos += 1
# Put the item there or right after any duplicates.
while item == array[pos]: while item == array[pos]:
pos += 1 pos += 1
array[pos], item = item, array[pos] array[pos], item = item, array[pos]
ans += 1
return array return array