| 3 | |
| 4 | |
| 5 | def stooge_sort_(arr, l, h): |
| 6 | if l >= h: |
| 7 | return 0 |
| 8 | |
| 9 | # If first element is smaller than last, then swap |
| 10 | |
| 11 | if arr[l] > arr[h]: |
| 12 | t = arr[l] |
| 13 | arr[l] = arr[h] |
| 14 | arr[h] = t |
| 15 | |
| 16 | # If there are more than 2 elements in array |
| 17 | if h - l + 1 > 2: |
| 18 | t = (int)((h - l + 1) / 3) |
| 19 | |
| 20 | # Recursively sort first 2 / 3 elements |
| 21 | stooge_sort_(arr, l, (h - t)) |
| 22 | |
| 23 | # Recursively sort last 2 / 3 elements |
| 24 | stooge_sort_(arr, l + t, (h)) |
| 25 | |
| 26 | # Recursively sort first 2 / 3 elements |
| 27 | stooge_sort_(arr, l, (h - t)) |
| 28 | |
| 29 | |
| 30 | arr = [2, 4, 5, 3, 1] |