Sorting algorithms/Permutation sort: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
No edit summary |
||
Line 1,039: | Line 1,039: | ||
[18, 2, 19, 10, 17, 10, 14, 8, 3] |
[18, 2, 19, 10, 17, 10, 14, 8, 3] |
||
[2, 3, 8, 10, 10, 14, 17, 18, 19] |
[2, 3, 8, 10, 10, 14, 17, 18, 19] |
||
</pre> |
|||
=={{header|EMal}}== |
|||
{{trans|Java}} |
|||
<syntaxhighlight lang="emal"> |
|||
type PermutationSort |
|||
fun isSorted = logic by List a |
|||
for int i = 1; i < a.length; ++i |
|||
if a[i - 1] > a[i] do return false end |
|||
end |
|||
return true |
|||
end |
|||
fun permute = void by List a, int n, List lists |
|||
if n == 1 |
|||
List b = int[] |
|||
for int i = 0; i < a.length; ++i |
|||
b.append(a[i]) |
|||
end |
|||
lists.append(b) |
|||
return |
|||
end |
|||
int i = 0 |
|||
while i < n |
|||
a.swap(i, n - 1) |
|||
permute(a, n - 1, lists) |
|||
a.swap(i, n - 1) |
|||
i = i + 1 |
|||
end |
|||
end |
|||
fun sort = List by List a |
|||
List lists = List[] |
|||
permute(a, a.length, lists) |
|||
for each List list in lists |
|||
if isSorted(list) do return list end |
|||
end |
|||
return a |
|||
end |
|||
type Main |
|||
List a = int[3,2,1,8,9,4,6] |
|||
writeLine("Unsorted: " + a) |
|||
a = PermutationSort.sort(a) |
|||
writeLine(" Sorted: " + a) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Unsorted: [3,2,1,8,9,4,6] |
|||
Sorted: [1,2,3,4,6,8,9] |
|||
</pre> |
</pre> |
||