Sorting algorithms/Strand sort: Difference between revisions
Content added Content deleted
Line 303: | Line 303: | ||
Before: -2,0,-2,5,5,3,-1,-3,5,5,0,2,-4,4,2 |
Before: -2,0,-2,5,5,3,-1,-3,5,5,0,2,-4,4,2 |
||
After : -4,-3,-2,-2,-1,0,0,2,2,3,4,5,5,5,5</pre> |
After : -4,-3,-2,-2,-1,0,0,2,2,3,4,5,5,5,5</pre> |
||
=={{header|Python}}== |
|||
<lang Python>def merge_list(a, b): |
|||
out = [] |
|||
while len(a) and len(b): |
|||
if a[0] < b[0]: |
|||
out.append(a.pop(0)) |
|||
else: |
|||
out.append(b.pop(0)) |
|||
out[len(out):] = a |
|||
out[len(out):] = b |
|||
return out |
|||
def strand(a): |
|||
i, s = 0, [a.pop(0)] |
|||
while i < len(a): |
|||
if a[i] > s[-1]: |
|||
s.append(a.pop(i)) |
|||
else: |
|||
i = i + 1 |
|||
return s |
|||
def strand_sort(a): |
|||
out = strand(a) |
|||
while len(a): |
|||
out = merge_list(out, strand(a)) |
|||
return out |
|||
print strand_sort([1, 6, 3, 2, 1, 7, 5, 3])</lang> |
|||
Output:<lang>[1, 1, 2, 3, 3, 5, 6, 7]</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 346: | Line 376: | ||
puts [strandSort {3 1 5 4 2}]</lang> |
puts [strandSort {3 1 5 4 2}]</lang> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |