| 12 | |
| 13 | |
| 14 | def 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 | |
| 36 | if __name__ == "__main__": |