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

Function stooge

sorts/stooge_sort.py:14–33  ·  view source on GitHub ↗
(arr: list[int], i: int, h: int)

Source from the content-addressed store, hash-verified

12
13
14def stooge(arr: list[int], i: int, h: int) -> None:
15 if i >= h:
16 return
17
18 # If first element is smaller than the last then swap them
19 if arr[i] > arr[h]:
20 arr[i], arr[h] = arr[h], arr[i]
21
22 # If there are more than 2 elements in the array
23 if h - i + 1 > 2:
24 t = (int)((h - i + 1) / 3)
25
26 # Recursively sort first 2/3 elements
27 stooge(arr, i, (h - t))
28
29 # Recursively sort last 2/3 elements
30 stooge(arr, i + t, (h))
31
32 # Recursively sort first 2/3 elements
33 stooge(arr, i, (h - t))
34
35
36if __name__ == "__main__":

Callers 1

stooge_sortFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected