Skip to content

Removed boilerplate comments and unused variables in cycle_sort #2073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions sorts/cycle_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,32 @@ def cycle_sort(array: list) -> list:
>>> 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.
for cycleStart in range(0, len(array) - 1):
item = array[cycleStart]

# finding the position for putting the item.
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
pos = cycle_start
for i in range(cycle_start + 1, array_len):
if array[i] < item:
pos += 1

# If the item is already present-not a cycle.
if pos == cycleStart:
if pos == cycle_start:
continue

# Otherwise, put the item there or right after any duplicates.
while item == array[pos]:
pos += 1
array[pos], item = item, array[pos]
ans += 1

# Rotate the rest of the cycle.
while pos != cycleStart:

# Find where to put the item.
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
array[pos], item = item, array[pos]
while pos != cycle_start:
pos = cycle_start
for i in range(cycle_start + 1, array_len):
if array[i] < item:
pos += 1

# Put the item there or right after any duplicates.
while item == array[pos]:
pos += 1

array[pos], item = item, array[pos]
ans += 1

return array

Expand Down