interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / strand-sort / oracle.py

23 lines · 762 bytes Raw Blame History
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.
3def 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
19a = [5, 1, 4, 2, 0, 9, 6, 3, 8, 7, -2, 11]
20print(*a)
21s = strand_sort(a)
22assert s == sorted(a)
23print(*s)