Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -27,11 +27,14 @@ inspired from http://www.ics.uci.edu/~eppstein/161/python/peters-selection.py · 26 unmodified lines | |||
| 27 | """ | 27 | """ |
| 28 | 28 | ||
| 29 | def short_find(a, rank): | 29 | def short_find(a, rank): |
| 30 | a.sort() | 30 | """ find the rank-th value in sorted a """ |
| 31 | return a[rank - 1] | 31 | # copy to b before sorting |
| 32 | b = a[:] | ||
| 33 | b.sort() | ||
| 34 | return b[rank - 1] | ||
| 32 | 35 | ||
| 33 | # Find the rank'th-smallest value in a, in worst-case linear time. | ||
| 34 | def percental(a, rank): | 36 | def percental(a, rank): |
| 37 | """ Find the rank'th-smallest value in a, in worst-case linear time. """ | ||
| 35 | n = len(a) | 38 | n = len(a) |
| 36 | assert 1 <= rank <= n | 39 | assert 1 <= rank <= n |
| 37 | if n <= 7: | 40 | if n <= 7: |