Sort disjoint sublist: Difference between revisions

m
no edit summary
(Grouping BASIC dialects)
mNo edit summary
Line 1,596:
{{works with|Delphi|6.0}}
{{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);
Line 1,653 ⟶ 1,650:
L2.Free;
end;
end;</syntaxhighlight>
end;
{{out}}<pre>
 
 
</syntaxhighlight>
{{out}}
<pre>
[7 0 5 4 3 2 1 6]
Elapsed Time: 0.626 ms.</pre>
 
</pre>
 
 
=={{header|EchoLisp}}==
Line 1,717 ⟶ 1,707:
}</syntaxhighlight>
{{out}}
<pre>7,0,5,4,3,2,1,6</pre>
<pre>
7,0,5,4,3,2,1,6
</pre>
 
=={{header|Elixir}}==
Line 1,741 ⟶ 1,729:
indices = [6, 1, 7]
IO.inspect Sort_disjoint.sublist(values, indices)</syntaxhighlight>
 
{{out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
<pre>
[7, 0, 5, 4, 3, 2, 1, 6]
</pre>
 
=={{header|Erlang}}==
Line 1,772 ⟶ 1,757:
</syntaxhighlight>
{{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}}==
Line 1,827 ⟶ 1,810:
END PROGRAM</syntaxhighlight>
{{out}}
<pre>[ 7, 0, 5, 4, 3, 2, 1, 6]</pre>
<pre>
[ 7, 0, 5, 4, 3, 2, 1, 6]
</pre>
 
=={{header|Euphoria}}==
Line 1,861 ⟶ 1,842:
constant data = {7, 6, 5, 4, 3, 2, 1, 0}
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#}}==
Line 1,967 ⟶ 1,946:
fmt.Println("sorted: ", values)
}</syntaxhighlight>
{{out}}
Output:
<pre>initial: [7 6 5 4 3 2 1 0]
<pre>
initialsorted: [7 60 5 4 3 2 1 06]</pre>
sorted: [7 0 5 4 3 2 1 6]
</pre>
Alternative algorithm, sorting in place through the extra level of indirection.
 
Line 2,133 ⟶ 2,110:
 
=={{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).
 
<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)
Line 2,161 ⟶ 2,136:
every writes (!result || " ")
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
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'.
Line 2,267 ⟶ 2,237:
===ES5===
====Iterative====
 
Does not check for duplicate indices.
<syntaxhighlight lang="javascript">function sort_disjoint(values, indices) {
Line 2,287 ⟶ 2,256:
 
====Functional====
 
<syntaxhighlight lang="javascript">(function () {
'use strict';
Line 2,315 ⟶ 2,283:
 
})();</syntaxhighlight>
 
{{Out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
===ES6===
 
<syntaxhighlight lang="javascript">(() => {
'use strict';
Line 2,423 ⟶ 2,389:
 
=={{header|jq}}==
 
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:
Line 2,441 ⟶ 2,406:
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<syntaxhighlight lang="julia">function sortselected(a::AbstractVector{<:Real}, s::AbstractVector{<:Integer})
sel = unique(sort(s))
Line 2,457 ⟶ 2,421:
 
println("Original: $a\n\tsorted on $sel\n -> sorted array: $b")</syntaxhighlight>
 
{{out}}
<pre>Original: [7, 6, 5, 4, 3, 2, 1, 0]
Line 2,470 ⟶ 2,433:
 
===Another way===
<syntaxhighlight lang="k">sort : {x[<x]}
sort : {x[<x]}
nums : 7 6 5 4 3 2 1 0
i : sort 6 1 7
nums[i] : sort nums[i]
nums
7 0 5 4 3 2 1 6</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Kotlin}}==
Line 2,496 ⟶ 2,457:
values.sortDisjoint(indices)
println("Sorted array : ${values.asList()}")
}</syntaxhighlight>
}
</syntaxhighlight>
 
{{out}}
<pre>Original array : [7, 6, 5, 4, 3, 2, 1, 0] sorted on indices [6, 1, 7]
<pre>
OriginalSorted array : [7, 60, 5, 4, 3, 2, 1, 0] sorted on indices [6, 1, 7]</pre>
Sorted array : [7, 0, 5, 4, 3, 2, 1, 6]
</pre>
 
=={{header|Ksh}}==
Line 2,593 ⟶ 2,550:
io.write( values[i], " " )
end</syntaxhighlight>
{{out}}
<pre>7 0 5 4 3 2 1 6</pre>
 
Line 2,613 ⟶ 2,571:
Values[[Sort[Indices]]] = Sort[Values[[Indices]]];
Values</syntaxhighlight>
{{out}}
<pre>{ 7, 0, 5, 4, 3, 2, 1, 6 }</pre>
 
Line 2,661 ⟶ 2,620:
</syntaxhighlight>
{{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
Idx: 7 2 8</pre>
</pre>
 
=={{header|Nial}}==
 
{{works with|Q'Nial Version 6.3}}
 
<syntaxhighlight lang="nial">
values := [7, 6, 5, 4, 3, 2, 1, 0]
Line 2,694 ⟶ 2,649:
sortDisjoinSublist(d, @[6, 1, 7])
echo d</syntaxhighlight>
{{out}}
Output:
<pre>@[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
Line 2,764 ⟶ 2,719:
 
=={{header|OCaml}}==
===With arrays:===
 
With arrays:
 
<syntaxhighlight lang="ocaml">let disjoint_sort cmp values indices =
let temp = Array.map (Array.get values) indices in
Line 2,780 ⟶ 2,733:
print_newline()</syntaxhighlight>
 
===With lists:===
 
<syntaxhighlight lang="ocaml">let disjoint_sort cmp values indices =
let indices = List.sort compare indices in
Line 2,935 ⟶ 2,887:
end.
</syntaxhighlight>
<pre>Before
Before
7 6 5 4 3 2 1 0
After
7 0 5 4 3 2 1 6</pre>
 
</pre>
 
=={{header|Perl}}==
Line 2,958 ⟶ 2,907:
disjointSort( \@values , @indices ) ;
print "[@values]\n" ;</syntaxhighlight>
{{out}}
Output:
<pre>[7 0 5 4 3 2 1 6]</pre>
 
Line 2,981 ⟶ 2,930:
<!--</syntaxhighlight>-->
{{out}}
<pre>{7,0,5,4,3,2,1,6}</pre>
<pre>
{7,0,5,4,3,2,1,6}
</pre>
 
=== Shorter Alternative Version ===
Line 2,995 ⟶ 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>
<!--</syntaxhighlight>-->
{{out}}
same output.
 
Line 3,005 ⟶ 2,953:
(sort Indices) )
Values )</syntaxhighlight>
{{out}}
Output:
<pre>-> (7 0 5 4 3 2 1 6)</pre>
 
=={{header|Prolog}}==
 
=== Using only predicates marked as "builtin" ===
 
<syntaxhighlight lang="prolog">
% ===
Line 3,109 ⟶ 3,055:
 
=== 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)]
 
Line 3,145 ⟶ 3,090:
 
=== 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].
 
Line 3,195 ⟶ 3,139:
"$(sublistsort $values $indices)"
</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}}==
Line 3,242 ⟶ 3,184:
 
main()</syntaxhighlight>
{{out}}
<pre>Before sort:
7 6 5 4 3 2 1 0
Line 3,351 ⟶ 3,294:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
#lang racket
 
(define (sort-disjoint l is)
Line 3,363 ⟶ 3,305:
 
(sort-disjoint '(7 6 5 4 3 2 1 0) '(6 1 7))
;; --> '(7 0 5 4 3 2 1 6)</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Raku}}==
Line 3,433 ⟶ 3,374:
return $</syntaxhighlight>
{{out|output|text=&nbsp; 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
sorted list: 7 0 5 4 3 2 1 6</pre>
</pre>
 
=={{header|Ring}}==
Line 3,463 ⟶ 3,402:
</syntaxhighlight>
{{out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
<pre>
[7, 0, 5, 4, 3, 2, 1, 6]
</pre>
 
=={{header|RPL}}==
Line 3,479 ⟶ 3,416:
{ 7 6 5 4 3 2 1 0 } { 6 1 7 } <span style="color:blue">'''SUBSORT'''</span>
{{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}}==
2,130

edits