Sorting algorithms/Strand sort: Difference between revisions
Content added Content deleted
(→{{header|Euphoria}}: Euphoria example added) |
No edit summary |
||
Line 324: | Line 324: | ||
[1, 2, 3, 3, 4, 5] |
[1, 2, 3, 3, 4, 5] |
||
[1, 2, 3, 3, 3, 4, 5, 6]</pre> |
[1, 2, 3, 3, 3, 4, 5, 6]</pre> |
||
=={{header|NetRexx}}== |
|||
<lang NetRexx>/* NetRexx */ |
|||
options replace format comments java crossref savelog symbols binary |
|||
import java.util.List |
|||
placesList = [String - |
|||
"UK London", "US New York", "US Boston", "US Washington" - |
|||
, "UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" - |
|||
] |
|||
lists = [ - |
|||
placesList - |
|||
, strandSort(String[] Arrays.copyOf(placesList, placesList.length)) - |
|||
] |
|||
loop ln = 0 to lists.length - 1 |
|||
cl = lists[ln] |
|||
loop ct = 0 to cl.length - 1 |
|||
say cl[ct] |
|||
end ct |
|||
say |
|||
end ln |
|||
return |
|||
method strandSort(A = String[]) public constant binary returns String[] |
|||
rl = String[A.length] |
|||
al = List strandSort(Arrays.asList(A)) |
|||
al.toArray(rl) |
|||
return rl |
|||
method strandSort(Alst = List) public constant binary returns ArrayList |
|||
A = ArrayList(Alst) |
|||
result = ArrayList() |
|||
loop label A_ while A.size > 0 |
|||
sublist = ArrayList() |
|||
sublist.add(A.get(0)) |
|||
A.remove(0) |
|||
loop i_ = 0 while i_ < A.size - 1 |
|||
if (Comparable A.get(i_)).compareTo(Comparable sublist.get(sublist.size - 1)) > 0 then do |
|||
sublist.add(A.get(i_)) |
|||
A.remove(i_) |
|||
end |
|||
end i_ |
|||
result = merge(result, sublist) |
|||
end A_ |
|||
return result |
|||
method merge(left = List, right = List) public constant binary returns ArrayList |
|||
result = ArrayList() |
|||
loop label mx while left.size > 0 & right.size > 0 |
|||
if (Comparable left.get(0)).compareTo(Comparable right.get(0)) <= 0 then do |
|||
result.add(left.get(0)) |
|||
left.remove(0) |
|||
end |
|||
else do |
|||
result.add(right.get(0)) |
|||
right.remove(0) |
|||
end |
|||
end mx |
|||
if left.size > 0 then do |
|||
result.addAll(left) |
|||
end |
|||
if right.size > 0 then do |
|||
result.addAll(right) |
|||
end |
|||
return result |
|||
</lang> |
|||
;Output |
|||
<pre> |
|||
UK London |
|||
US New York |
|||
US Boston |
|||
US Washington |
|||
UK Washington |
|||
US Birmingham |
|||
UK Birmingham |
|||
UK Boston |
|||
UK Birmingham |
|||
UK Boston |
|||
UK London |
|||
UK Washington |
|||
US Birmingham |
|||
US Boston |
|||
US New York |
|||
US Washington |
|||
</pre> |
|||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |