omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / strand-sort / oracle.py
| 1 | # Independent reference for "Sorting algorithms/Strand sort" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | def strand_sort(a): |
| 4 | a = list(a) |
| 5 | out = [] |
| 6 | while a: |
| 7 | strand, rest = [a[0]], [] |
| 8 | for v in a[1:]: |
| 9 | (strand if v >= strand[-1] else rest).append(v) |
| 10 | merged = [] |
| 11 | i = j = 0 |
| 12 | while i < len(out) or j < len(strand): |
| 13 | if j >= len(strand) or (i < len(out) and out[i] <= strand[j]): |
| 14 | merged.append(out[i]); i += 1 |
| 15 | else: |
| 16 | merged.append(strand[j]); j += 1 |
| 17 | out, a = merged, rest |
| 18 | return out |
| 19 | a = [5, 1, 4, 2, 0, 9, 6, 3, 8, 7, -2, 11] |
| 20 | print(*a) |
| 21 | s = strand_sort(a) |
| 22 | assert s == sorted(a) |
| 23 | print(*s) |