MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / circle_sort_util

Function circle_sort_util

sorts/circle_sort.py:33–73  ·  view source on GitHub ↗

>>> 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)

Source from the content-addressed store, hash-verified

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

Callers 1

circle_sortFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected