>>> arr = [5,4,3,2,1] >>> circle_sort_util(lst, 0, 2) True >>> arr [3, 4, 5, 2, 1]
(collection: list, low: int, high: int)
| 31 | return collection |
| 32 | |
| 33 | def circle_sort_util(collection: list, low: int, high: int) -> bool: |
| 34 | """ |
| 35 | >>> arr = [5,4,3,2,1] |
| 36 | >>> circle_sort_util(lst, 0, 2) |
| 37 | True |
| 38 | >>> arr |
| 39 | [3, 4, 5, 2, 1] |
| 40 | """ |
| 41 | |
| 42 | swapped = False |
| 43 | |
| 44 | if low == high: |
| 45 | return swapped |
| 46 | |
| 47 | left = low |
| 48 | right = high |
| 49 | |
| 50 | while left < right: |
| 51 | if collection[left] > collection[right]: |
| 52 | collection[left], collection[right] = ( |
| 53 | collection[right], |
| 54 | collection[left], |
| 55 | ) |
| 56 | swapped = True |
| 57 | |
| 58 | left += 1 |
| 59 | right -= 1 |
| 60 | |
| 61 | if left == right and collection[left] > collection[right + 1]: |
| 62 | collection[left], collection[right + 1] = ( |
| 63 | collection[right + 1], |
| 64 | collection[left], |
| 65 | ) |
| 66 | |
| 67 | swapped = True |
| 68 | |
| 69 | mid = low + int((high - low) / 2) |
| 70 | left_swap = circle_sort_util(collection, low, mid) |
| 71 | right_swap = circle_sort_util(collection, mid + 1, high) |
| 72 | |
| 73 | return swapped or left_swap or right_swap |
| 74 | |
| 75 | is_not_sorted = True |
| 76 |