Sort disjoint sublist: Difference between revisions
Content added Content deleted
(Grouping BASIC dialects) |
mNo edit summary |
||
Line 1,596: | Line 1,596: | ||
{{works with|Delphi|6.0}} |
{{works with|Delphi|6.0}} |
||
{{libheader|SysUtils,StdCtrls}} |
{{libheader|SysUtils,StdCtrls}} |
||
<syntaxhighlight lang="Delphi">{Test values} |
|||
<syntaxhighlight lang="Delphi"> |
|||
{Test values} |
|||
var Values: array [0..7] of integer = (7, 6, 5, 4, 3, 2, 1, 0); |
var Values: array [0..7] of integer = (7, 6, 5, 4, 3, 2, 1, 0); |
||
Line 1,653: | Line 1,650: | ||
L2.Free; |
L2.Free; |
||
end; |
end; |
||
end;</syntaxhighlight> |
|||
end; |
|||
{{out}}<pre> |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[7 0 5 4 3 2 1 6] |
[7 0 5 4 3 2 1 6] |
||
Elapsed Time: 0.626 ms. |
Elapsed Time: 0.626 ms.</pre> |
||
</pre> |
|||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
Line 1,717: | Line 1,707: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>7,0,5,4,3,2,1,6</pre> |
|||
<pre> |
|||
7,0,5,4,3,2,1,6 |
|||
</pre> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
Line 1,741: | Line 1,729: | ||
indices = [6, 1, 7] |
indices = [6, 1, 7] |
||
IO.inspect Sort_disjoint.sublist(values, indices)</syntaxhighlight> |
IO.inspect Sort_disjoint.sublist(values, indices)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre> |
|||
<pre> |
|||
[7, 0, 5, 4, 3, 2, 1, 6] |
|||
</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Line 1,772: | Line 1,757: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>20> sort_disjoint:task(). |
|||
<pre> |
|||
[7,0,5,4,3,2,1,6]</pre> |
|||
20> sort_disjoint:task(). |
|||
[7,0,5,4,3,2,1,6] |
|||
</pre> |
|||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
Line 1,827: | Line 1,810: | ||
END PROGRAM</syntaxhighlight> |
END PROGRAM</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[ 7, 0, 5, 4, 3, 2, 1, 6]</pre> |
|||
<pre> |
|||
[ 7, 0, 5, 4, 3, 2, 1, 6] |
|||
</pre> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
Line 1,861: | Line 1,842: | ||
constant data = {7, 6, 5, 4, 3, 2, 1, 0} |
constant data = {7, 6, 5, 4, 3, 2, 1, 0} |
||
constant indexes = {7, 2, 8}</syntaxhighlight> |
constant indexes = {7, 2, 8}</syntaxhighlight> |
||
{{out}} |
|||
pre>{7,0,5,4,3,2,1,6}</pre> |
|||
Output: |
|||
<pre>{7,0,5,4,3,2,1,6} |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Line 1,967: | Line 1,946: | ||
fmt.Println("sorted: ", values) |
fmt.Println("sorted: ", values) |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>initial: [7 6 5 4 3 2 1 0] |
|||
<pre> |
|||
sorted: [7 0 5 4 3 2 1 6]</pre> |
|||
sorted: [7 0 5 4 3 2 1 6] |
|||
</pre> |
|||
Alternative algorithm, sorting in place through the extra level of indirection. |
Alternative algorithm, sorting in place through the extra level of indirection. |
||
Line 2,133: | Line 2,110: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Icon's lists are 1-based, so the example uses (7, 2, 8) as the indices, not (6, 1 7). |
Icon's lists are 1-based, so the example uses (7, 2, 8) as the indices, not (6, 1 7). |
||
<syntaxhighlight lang="icon"> |
<syntaxhighlight lang="icon">link sort # get the 'isort' procedure for sorting a list |
||
link sort # get the 'isort' procedure for sorting a list |
|||
procedure sortDisjoint (items, indices) |
procedure sortDisjoint (items, indices) |
||
Line 2,161: | Line 2,136: | ||
every writes (!result || " ") |
every writes (!result || " ") |
||
write () |
write () |
||
end</syntaxhighlight> |
|||
end |
|||
{{out}}<pre>7 6 5 4 3 2 1 0 |
|||
</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
7 6 5 4 3 2 1 0 |
|||
2 7 8 |
2 7 8 |
||
7 0 5 4 3 2 1 6 |
7 0 5 4 3 2 1 6</pre> |
||
</pre> |
|||
The expression <code>!indices</code> generates the value of each index in turn, so the line <syntaxhighlight lang="icon">every put (values, result[!indices])</syntaxhighlight> effectively loops through each index, putting <code>result[index]</code> into the list 'values'. |
The expression <code>!indices</code> generates the value of each index in turn, so the line <syntaxhighlight lang="icon">every put (values, result[!indices])</syntaxhighlight> effectively loops through each index, putting <code>result[index]</code> into the list 'values'. |
||
Line 2,267: | Line 2,237: | ||
===ES5=== |
===ES5=== |
||
====Iterative==== |
====Iterative==== |
||
Does not check for duplicate indices. |
Does not check for duplicate indices. |
||
<syntaxhighlight lang="javascript">function sort_disjoint(values, indices) { |
<syntaxhighlight lang="javascript">function sort_disjoint(values, indices) { |
||
Line 2,287: | Line 2,256: | ||
====Functional==== |
====Functional==== |
||
<syntaxhighlight lang="javascript">(function () { |
<syntaxhighlight lang="javascript">(function () { |
||
'use strict'; |
'use strict'; |
||
Line 2,315: | Line 2,283: | ||
})();</syntaxhighlight> |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre> |
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre> |
||
===ES6=== |
===ES6=== |
||
<syntaxhighlight lang="javascript">(() => { |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 2,423: | Line 2,389: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
We define a jq function, disjointSort, that accepts the array of values as input, |
We define a jq function, disjointSort, that accepts the array of values as input, |
||
but for clarity we first define a utility function for updating an array at multiple places: |
but for clarity we first define a utility function for updating an array at multiple places: |
||
Line 2,441: | Line 2,406: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
<syntaxhighlight lang="julia">function sortselected(a::AbstractVector{<:Real}, s::AbstractVector{<:Integer}) |
<syntaxhighlight lang="julia">function sortselected(a::AbstractVector{<:Real}, s::AbstractVector{<:Integer}) |
||
sel = unique(sort(s)) |
sel = unique(sort(s)) |
||
Line 2,457: | Line 2,421: | ||
println("Original: $a\n\tsorted on $sel\n -> sorted array: $b")</syntaxhighlight> |
println("Original: $a\n\tsorted on $sel\n -> sorted array: $b")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Original: [7, 6, 5, 4, 3, 2, 1, 0] |
<pre>Original: [7, 6, 5, 4, 3, 2, 1, 0] |
||
Line 2,470: | Line 2,433: | ||
===Another way=== |
===Another way=== |
||
<syntaxhighlight lang="k"> |
<syntaxhighlight lang="k">sort : {x[<x]} |
||
sort : {x[<x]} |
|||
nums : 7 6 5 4 3 2 1 0 |
nums : 7 6 5 4 3 2 1 0 |
||
i : sort 6 1 7 |
i : sort 6 1 7 |
||
nums[i] : sort nums[i] |
nums[i] : sort nums[i] |
||
nums |
nums |
||
7 0 5 4 3 2 1 6 |
7 0 5 4 3 2 1 6</syntaxhighlight> |
||
</syntaxhighlight> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 2,496: | Line 2,457: | ||
values.sortDisjoint(indices) |
values.sortDisjoint(indices) |
||
println("Sorted array : ${values.asList()}") |
println("Sorted array : ${values.asList()}") |
||
}</syntaxhighlight> |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre>Original array : [7, 6, 5, 4, 3, 2, 1, 0] sorted on indices [6, 1, 7] |
|||
<pre> |
|||
Sorted array : [7, 0, 5, 4, 3, 2, 1, 6]</pre> |
|||
Sorted array : [7, 0, 5, 4, 3, 2, 1, 6] |
|||
</pre> |
|||
=={{header|Ksh}}== |
=={{header|Ksh}}== |
||
Line 2,593: | Line 2,550: | ||
io.write( values[i], " " ) |
io.write( values[i], " " ) |
||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
{{out}} |
|||
<pre>7 0 5 4 3 2 1 6</pre> |
<pre>7 0 5 4 3 2 1 6</pre> |
||
Line 2,613: | Line 2,571: | ||
Values[[Sort[Indices]]] = Sort[Values[[Indices]]]; |
Values[[Sort[Indices]]] = Sort[Values[[Indices]]]; |
||
Values</syntaxhighlight> |
Values</syntaxhighlight> |
||
{{out}} |
|||
<pre>{ 7, 0, 5, 4, 3, 2, 1, 6 }</pre> |
<pre>{ 7, 0, 5, 4, 3, 2, 1, 6 }</pre> |
||
Line 2,661: | Line 2,620: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>In: 7 6 5 4 3 2 1 0 |
|||
<pre> |
|||
In: 7 6 5 4 3 2 1 0 |
|||
Out: 7 0 5 4 3 2 1 6 |
Out: 7 0 5 4 3 2 1 6 |
||
Idx: 7 2 8 |
Idx: 7 2 8</pre> |
||
</pre> |
|||
=={{header|Nial}}== |
=={{header|Nial}}== |
||
{{works with|Q'Nial Version 6.3}} |
{{works with|Q'Nial Version 6.3}} |
||
<syntaxhighlight lang="nial"> |
<syntaxhighlight lang="nial"> |
||
values := [7, 6, 5, 4, 3, 2, 1, 0] |
values := [7, 6, 5, 4, 3, 2, 1, 0] |
||
Line 2,694: | Line 2,649: | ||
sortDisjoinSublist(d, @[6, 1, 7]) |
sortDisjoinSublist(d, @[6, 1, 7]) |
||
echo d</syntaxhighlight> |
echo d</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>@[7, 0, 5, 4, 3, 2, 1, 6]</pre> |
<pre>@[7, 0, 5, 4, 3, 2, 1, 6]</pre> |
||
Line 2,764: | Line 2,719: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
===With arrays:=== |
|||
With arrays: |
|||
<syntaxhighlight lang="ocaml">let disjoint_sort cmp values indices = |
<syntaxhighlight lang="ocaml">let disjoint_sort cmp values indices = |
||
let temp = Array.map (Array.get values) indices in |
let temp = Array.map (Array.get values) indices in |
||
Line 2,780: | Line 2,733: | ||
print_newline()</syntaxhighlight> |
print_newline()</syntaxhighlight> |
||
With lists: |
===With lists:=== |
||
<syntaxhighlight lang="ocaml">let disjoint_sort cmp values indices = |
<syntaxhighlight lang="ocaml">let disjoint_sort cmp values indices = |
||
let indices = List.sort compare indices in |
let indices = List.sort compare indices in |
||
Line 2,935: | Line 2,887: | ||
end. |
end. |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
<pre> |
<pre>Before |
||
Before |
|||
7 6 5 4 3 2 1 0 |
7 6 5 4 3 2 1 0 |
||
After |
After |
||
7 0 5 4 3 2 1 6 |
7 0 5 4 3 2 1 6</pre> |
||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 2,958: | Line 2,907: | ||
disjointSort( \@values , @indices ) ; |
disjointSort( \@values , @indices ) ; |
||
print "[@values]\n" ;</syntaxhighlight> |
print "[@values]\n" ;</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>[7 0 5 4 3 2 1 6]</pre> |
<pre>[7 0 5 4 3 2 1 6]</pre> |
||
Line 2,981: | Line 2,930: | ||
<!--</syntaxhighlight>--> |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre>{7,0,5,4,3,2,1,6}</pre> |
|||
<pre> |
|||
{7,0,5,4,3,2,1,6} |
|||
</pre> |
|||
=== Shorter Alternative Version === |
=== Shorter Alternative Version === |
||
Line 2,995: | Line 2,942: | ||
<span style="color: #0000FF;">?</span><span style="color: #000000;">disjoint_sort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">})</span> |
<span style="color: #0000FF;">?</span><span style="color: #000000;">disjoint_sort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">})</span> |
||
<!--</syntaxhighlight>--> |
<!--</syntaxhighlight>--> |
||
{{out}} |
|||
same output. |
same output. |
||
Line 3,005: | Line 2,953: | ||
(sort Indices) ) |
(sort Indices) ) |
||
Values )</syntaxhighlight> |
Values )</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>-> (7 0 5 4 3 2 1 6)</pre> |
<pre>-> (7 0 5 4 3 2 1 6)</pre> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
=== Using only predicates marked as "builtin" === |
=== Using only predicates marked as "builtin" === |
||
<syntaxhighlight lang="prolog"> |
<syntaxhighlight lang="prolog"> |
||
% === |
% === |
||
Line 3,109: | Line 3,055: | ||
=== Alternatively, using predicates from <code>library(list)</code> === |
=== Alternatively, using predicates from <code>library(list)</code> === |
||
Using <code>append/3</code> from SWi-Prolog's [https://eu.swi-prolog.org/pldoc/man?section=lists library(list)] |
Using <code>append/3</code> from SWi-Prolog's [https://eu.swi-prolog.org/pldoc/man?section=lists library(list)] |
||
Line 3,145: | Line 3,090: | ||
=== Unit Tests === |
=== Unit Tests === |
||
Test code using the [https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/plunit.html%27) Unit Testing framework of SWI Prolog]. |
Test code using the [https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/plunit.html%27) Unit Testing framework of SWI Prolog]. |
||
Line 3,195: | Line 3,139: | ||
"$(sublistsort $values $indices)" |
"$(sublistsort $values $indices)" |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{out}} |
|||
<b>Output:</b> |
|||
<pre>7 0 5 4 3 2 1 6</pre> |
|||
<pre> |
|||
7 0 5 4 3 2 1 6 |
|||
</pre> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
Line 3,242: | Line 3,184: | ||
main()</syntaxhighlight> |
main()</syntaxhighlight> |
||
{{out}} |
|||
<pre>Before sort: |
<pre>Before sort: |
||
7 6 5 4 3 2 1 0 |
7 6 5 4 3 2 1 0 |
||
Line 3,351: | Line 3,294: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
<syntaxhighlight lang="racket">#lang racket |
||
#lang racket |
|||
(define (sort-disjoint l is) |
(define (sort-disjoint l is) |
||
Line 3,363: | Line 3,305: | ||
(sort-disjoint '(7 6 5 4 3 2 1 0) '(6 1 7)) |
(sort-disjoint '(7 6 5 4 3 2 1 0) '(6 1 7)) |
||
;; --> '(7 0 5 4 3 2 1 6) |
;; --> '(7 0 5 4 3 2 1 6)</syntaxhighlight> |
||
</syntaxhighlight> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 3,433: | Line 3,374: | ||
return $</syntaxhighlight> |
return $</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> list of indices: 7 2 8 |
|||
<pre> |
|||
list of indices: 7 2 8 |
|||
unsorted list: 7 6 5 4 3 2 1 0 |
unsorted list: 7 6 5 4 3 2 1 0 |
||
sorted list: 7 0 5 4 3 2 1 6 |
sorted list: 7 0 5 4 3 2 1 6</pre> |
||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
Line 3,463: | Line 3,402: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre> |
|||
<pre> |
|||
[7, 0, 5, 4, 3, 2, 1, 6] |
|||
</pre> |
|||
=={{header|RPL}}== |
=={{header|RPL}}== |
||
Line 3,479: | Line 3,416: | ||
{ 7 6 5 4 3 2 1 0 } { 6 1 7 } <span style="color:blue">'''SUBSORT'''</span> |
{ 7 6 5 4 3 2 1 0 } { 6 1 7 } <span style="color:blue">'''SUBSORT'''</span> |
||
{{out}} |
{{out}} |
||
<pre>1: { 7 0 5 4 3 2 1 6 }</pre> |
|||
<pre> |
|||
1: { 7 0 5 4 3 2 1 6 } |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |