(a)
| 3 | |
| 4 | |
| 5 | def pigeonhole_sort(a): |
| 6 | # (number of pigeonholes we need) |
| 7 | my_min = min(a) |
| 8 | my_max = max(a) |
| 9 | size = my_max - my_min + 1 |
| 10 | |
| 11 | # total pigeonholes |
| 12 | holes = [0] * size |
| 13 | |
| 14 | # filling up the pigeonholes. |
| 15 | for x in a: |
| 16 | holes[x - my_min] += 1 |
| 17 | |
| 18 | # Put the elements back into the array in order. |
| 19 | i = 0 |
| 20 | for count in range(size): |
| 21 | while holes[count] > 0: |
| 22 | holes[count] -= 1 |
| 23 | a[i] = count + my_min |
| 24 | i += 1 |
| 25 | |
| 26 | |
| 27 | a = [10, 3, 2, 7, 4, 6, 8] |