From ed7c0d5be034a085c7f32c0e00f93c3a95e7c1b8 Mon Sep 17 00:00:00 2001 From: mateuszz0000 Date: Fri, 5 Jun 2020 09:47:11 +0200 Subject: [PATCH] Remove boilerplate comments and unused variables --- sorts/cycle_sort.py | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/sorts/cycle_sort.py b/sorts/cycle_sort.py index d731ea838425..806f40441d79 100644 --- a/sorts/cycle_sort.py +++ b/sorts/cycle_sort.py @@ -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