Sort disjoint sublist: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 27:
{{trans|Python}}
<
V sindices = sorted(indices)
V values = sorted(sindices.map(i -> @data[i]))
Line 36:
V i = [6, 1, 7]
sort_disjoint_sublist(&d, i)
print(d)</
{{out}}
Line 44:
=={{header|Action!}}==
<
INT i
Line 108:
Test(arr,8,ind,3)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_disjoint_sublist.png Screenshot from Atari 8-bit computer]
Line 121:
=={{header|Ada}}==
<
use Ada.Text_IO;
Line 177:
New_Line;
end DisjointSort;</
=={{header|ALGOL W}}==
Uses the quicksort procedure from the Sorting Algorithms/Quicksort task and a variant for indexed sorting.
<
% Quicksorts in-place the array of integers v, from lb to ub %
procedure quicksort ( integer array v( * )
Line 264:
writeon( " ]" )
end
end.</
{{out}}
<pre>
Line 271:
=={{header|APL}}==
<
∇SDS[⎕]∇
∇
Line 278:
[2] Z←L
∇
</syntaxhighlight>
{{out}}
<pre>
Line 292:
Uses Foundation framework (through the ObjC interface), and works with versions of AppleScript from OS X 10.10 onwards.
<
use framework "Foundation"
use scripting additions
Line 488:
return lst
end tell
end zipWith</
{{Out}}
<
===Idiomatic, vanilla===
Line 496:
This works with Mac OS 10.9 or later, and with older systems if an earlier method's used to load the insertion sort handler or that handler's copied into the script.
<
use sorter : script "Insertion sort" -- https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript.
Line 522:
set indices to {7, 2, 8} -- AppleScript indices are one-based.
sortValuesAtIndices(values, indices)
return values</
{{output}}
<
=={{header|Arturo}}==
<
result: new data
inds: new indices
Line 543:
d: [7 6 5 4 3 2 1 0]
print sortDisjoint d [6 1 7]</
{{out}}
Line 553:
Much of the code here is proofs, unfolding and re-folding of lists, necessary assertions, memory management enforced by the type system, etc. (Some might say I am somewhat lax in my handling of pointers, using the simplest type '''ptr''' instead of more elaborate types.)
<
staload UN = "prelude/SATS/unsafe.sats"
Line 714:
println! values;
free values
end</
{{out}}
Line 722:
=={{header|AutoHotkey}}==
<
A := [], B:=[], C := [], D := []
for k, v in Indices
Line 733:
Values[C[A_Index]] := D[A_Index]
return Values
}</
Examples:<
Indices := [7, 2, 8]
Values := Sort_disjoint_sublist(Values, Indices)
for k, v in Values
output .= v ", "
MsgBox % "[" Trim(output, ", ") "]" ; show output</
{{out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 745:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
Sort% = FN_sortinit(0,0) : REM Ascending
Line 776:
o$ += STR$(l%(i%)) + ", "
NEXT
= LEFT$(LEFT$(o$)) + "]"</
'''Output:'''
<pre>
Line 783:
=={{header|Bracmat}}==
<
& 6 1 7:?indices
& 0:?sortedValues:?sortedIndices
Line 798:
& !A !value !Z:?values
)
& out$!values;</
Output:
<pre>7 0 5 4 3 2 1 6</pre>
=={{header|C}}==
<
/* yes, bubble sort */
Line 835:
return 0;
}</
=={{header|C sharp}}==
<
using System.Linq;
using System.Collections.Generic;
Line 866:
}
}</
=={{header|C++}}==
<
#include <iostream>
#include <iterator>
Line 902:
return 0;
}</
{{out}}
<pre>
Line 910:
{{trans|Go}}
Solution that sorts using a custom iterator that iterates a disjoint sublist.
<
#include <iostream>
#include <iterator>
Line 971:
return 0;
}</
{{out}}
<pre>
Line 978:
=={{header|Clojure}}==
<
(let [val-subset (keep-indexed #(when ((set idxs) %) %2) coll)
replacements (zipmap (set idxs) (sort val-subset))]
(apply assoc coll (flatten (seq replacements)))))</
{{out}}
Line 988:
=={{header|Common Lisp}}==
<
"Destructively perform a disjoin sublist sort on VALUES with INDICES."
(loop :for element :in
Line 996:
:for index :across (sort indices '<)
:do (setf (svref values index) element))
values)</
{{out}}
<pre>CL-USER> (disjoint-sort #(7 6 5 4 3 2 1 0) #(6 1 7))
Line 1,002:
=={{header|D}}==
<
void main() {
Line 1,011:
assert(data == [7, 0, 5, 4, 3, 2, 1, 6]);
}</
===Lower Level version===
<
void disjointSort(T, U)(T[] arr, U[] indexes)
Line 1,053:
disjointSort(data, indexes);
assert(data == [7.0, 0.0, 5.0, 4.0, 3.0, 2.0, 1.0, 6.0]);
}</
===Simple Alternative Version===
<
void main() {
Line 1,074:
assert(data == [7.0, 0.0, 5.0, 4.0, 3.0, 2.0, 1.0, 6.0]);
}</
=={{header|EchoLisp}}==
<
(define (sort-disjoint values indices)
(define sorted (list-sort <
Line 1,094:
(task)
→ (7 0 5 4 3 2 1 6)
</syntaxhighlight>
=={{header|Elena}}==
ELENA 5.0 :
<
import system'routines;
import system'culture;
Line 1,126:
console.printLine(list.sortSublist(new int[]{6, 1, 7}).asEnumerable())
}</
{{out}}
<pre>
Line 1,133:
=={{header|Elixir}}==
<
def sublist(values, indices) when is_list(values) and is_list(indices) do
indices2 = Enum.sort(indices)
Line 1,151:
values = [7, 6, 5, 4, 3, 2, 1, 0]
indices = [6, 1, 7]
IO.inspect Sort_disjoint.sublist(values, indices)</
{{out}}
Line 1,159:
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module( sort_disjoint ).
Line 1,181:
merge( Index, {[_H | Values], [{Index, Value} | Sorted_indices_values]} ) -> {Value, {Values, Sorted_indices_values}};
merge( _Index, {[H | Values], Sorted_indices_values} ) -> {H, {Values, Sorted_indices_values}}.
</syntaxhighlight>
{{out}}
<pre>
Line 1,189:
=={{header|ERRE}}==
<
DIM LST%[7],INDICES%[2]
Line 1,236:
ShowList(LST%[]->O$)
PRINT(O$)
END PROGRAM</
{{out}}
<pre>
Line 1,243:
=={{header|Euphoria}}==
<
function uniq(sequence s)
Line 1,271:
constant data = {7, 6, 5, 4, 3, 2, 1, 0}
constant indexes = {7, 2, 8}</
Output:
Line 1,280:
{{trans|Python}}
Works with arrays instead of lists because this algorithm is more efficient with a random access collection type. Returns a copy of the array, as is usually preferred in F#.
<
let indices = Set.toArray indices // creates a sorted array
let result = Array.copy data
Line 1,289:
printfn "%A" (sortDisjointSubarray [|7;6;5;4;3;2;1;0|] (set [6;1;7]))</
=={{header|Factor}}==
<
over <enumerated> nths unzip swap [ natural-sort ] bi@
pick [ set-nth ] curry 2each ;</
{{out}}
<
{ 7 0 5 4 3 2 1 6 }</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 1,336:
end subroutine Isort
end program Example</
Output
<pre> 7 0 5 4 3 2 1 6</pre>
=={{header|FreeBASIC}}==
<
dim as integer index(0 to 2) = {6, 1, 7}, i
Line 1,352:
for i = 0 to 7
print value(i);
next i : print</
{{out}}<pre> 7 0 5 4 3 2 1 6</pre>
=={{header|Go}}==
<
import (
Line 1,392:
}
fmt.Println("sorted: ", values)
}</
Output:
<pre>
Line 1,401:
Compared to the strategy of extract-sort-replace, this strategy avoids the space overhead of the work area and the time overhead of extracting and reinserting elements. At some point however, the cost of indirection multiplied by O(log n) would dominate, and extract-sort-replace would become preferable.
<
import (
Line 1,457:
sort.Sort(s)
fmt.Println("sorted: ", s.values)
}</
=={{header|Groovy}}==
Groovy allows List-valued indexing to "gather" and "scatter" arbitrary sublists, making the solution almost trivial.
<
indices.sort().unique()
a[indices] = a[indices].sort()
a
}</
Test:
<
println a
Line 1,476:
println a
println sparseSort(a) // default == sort all
println a</
Output:
Line 1,490:
Here are three variations on the solution: using ordinary lists, immutable "boxed" arrays, and mutable "unboxed" arrays.
<
import qualified Data.Array as A
import Data.Array.IArray
Line 1,535:
print $ disSort1 xs is
print $ disSort2 xs is
print $ disSort3 xs is</
Or, in terms of Data.Map:
<
import Control.Applicative ((<|>))
import Data.Maybe (mapMaybe)
Line 1,554:
main :: IO ()
main = print $ disjointSort [6, 1, 7] [7, 6, 5, 4, 3, 2, 1, 0]</
{{Out}}
<pre>[7,0,5,4,3,2,1,6]</pre>
Line 1,562:
Icon's lists are 1-based, so the example uses (7, 2, 8) as the indices, not (6, 1 7).
<
link sort # get the 'isort' procedure for sorting a list
Line 1,588:
write ()
end
</syntaxhighlight>
Output:
Line 1,597:
</pre>
The expression <code>!indices</code> generates the value of each index in turn, so the line <
=={{header|Io}}==
Io does not come with a set type.
<
sortedIndices := indices unique sortInPlace
sortedValues := sortedIndices map(idx,at(idx)) sortInPlace
Line 1,608:
)
list(7,6,5,4,3,2,1,0) disjointSort(list(6,1,7)) println</
{{output}}
<pre>list(7, 0, 5, 4, 3, 2, 1, 6)</pre>
Line 1,615:
Note that the task requires us to ignore the order of the indices.
<
7 0 5 4 3 2 1 6</
Compare this with:
<
7 1 5 4 3 2 0 6</
Here, the order of the indices specifies the order we want the selected items to be sorted in: 7 '''''1''''' 5 4 3 2 '''''0 6'''''
Line 1,627:
{{works with|Java|1.5+}}
This function will modify the index array and the values list.
<
import java.util.Arrays;
import java.util.Collections;
Line 1,654:
System.out.println(list);
}
}</
{{out}}
<pre>[7, 6, 5, 4, 3, 2, 1, 0]
Line 1,662:
{{trans|Go}}
Shorter solution that sorts a list "wrapper" which represents a "view" into the disjoint sublist of the list.
<
import java.util.Collections;
import java.util.List;
Line 1,685:
System.out.println(list);
}
}</
{{out}}
<pre>[7, 6, 5, 4, 3, 2, 1, 0]
Line 1,695:
Does not check for duplicate indices.
<
var sublist = [];
indices.sort(function(a, b) { return a > b; });
Line 1,710:
return values;
}</
====Functional====
<
'use strict';
Line 1,740:
return disjointSort([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])
})();</
{{Out}}
Line 1,747:
===ES6===
<
'use strict';
Line 1,844:
// MAIN ---
return main();
})();</
{{Out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 1,852:
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:
<
reduce range(0; indices|length) as $i
(.; .[indices[$i]] = values[$i]);
Line 1,860:
# Set $sorted to the sorted array of values at $ix:
| ([ .[ $ix[] ] ] | sort) as $sorted
| setpaths( $ix; $sorted) ;</
Example:<
[7, 6, 5, 4, 3, 2, 1, 0] | disjointSort( [6, 1, 7] )</
[7,0,5,4,3,2,1,6]
Line 1,868:
{{works with|Julia|0.6}}
<
sel = unique(sort(s))
if sel[1] < 1 || length(a) < sel[end]
Line 1,882:
b = sortselected(a, sel)
println("Original: $a\n\tsorted on $sel\n -> sorted array: $b")</
{{out}}
Line 1,890:
=={{header|K}}==
<syntaxhighlight lang="k">
{@[x;y@<y;:;a@<a:x@y]}[7 6 5 4 3 2 1 0;6 1 7]
7 0 5 4 3 2 1 6
</syntaxhighlight>
===Another way===
<syntaxhighlight lang="k">
sort : {x[<x]}
nums : 7 6 5 4 3 2 1 0
Line 1,903:
nums
7 0 5 4 3 2 1 6
</syntaxhighlight>
=={{header|Kotlin}}==
<
/* in place sort */
Line 1,923:
println("Sorted array : ${values.asList()}")
}
</syntaxhighlight>
{{out}}
Line 1,932:
=={{header|Ksh}}==
<
#!/bin/ksh
Line 1,979:
done
print "After sort: ${arr_Val[*]}"</
{{out}}<pre>
Before sort: 7 6 5 4 3 2 1 0
Line 1,985:
=={{header|Lua}}==
<
indices = { 6, 1, 7 }
Line 2,018:
for i = 1, #values do
io.write( values[i], " " )
end</
<pre>7 0 5 4 3 2 1 6</pre>
=={{header|Maple}}==
<
local vals,inds,i:
vals := sort([seq(values[i], i in indices)]):
Line 2,031:
end proc:
tst := Array([7,6,5,4,3,2,1,0]):
sortDisjoint(tst,{7,2,8});</
{{out}}
<pre>[7 0 5 4 3 2 1 6]</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Values[[Sort[Indices]]] = Sort[Values[[Indices]]];
Values</
<pre>{ 7, 0, 5, 4, 3, 2, 1, 6 }</pre>
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols nobinary
Line 2,085:
say 'Idx:' iList.space
return
</syntaxhighlight>
{{out}}
<pre>
Line 2,097:
{{works with|Q'Nial Version 6.3}}
<syntaxhighlight lang="nial">
values := [7, 6, 5, 4, 3, 2, 1, 0]
indices := sortup [6, 1, 7]
values#indices := sortup values#indices
7 0 5 4 3 2 1 6
</syntaxhighlight>
=={{header|Nim}}==
<
proc sortDisjoinSublist[T](data: var seq[T], indices: seq[int]) =
Line 2,119:
var d = @[7, 6, 5, 4, 3, 2, 1, 0]
sortDisjoinSublist(d, @[6, 1, 7])
echo d</
Output:
<pre>@[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 2,126:
{{trans|Go}}
Sorts an array "wrapper" which represents a "view" into the disjoint sublist of the array.
<
@interface DisjointSublistView : NSMutableArray {
Line 2,176:
}
return 0;
}</
{{out}}
<pre>(
Line 2,193:
With arrays:
<
let temp = Array.map (Array.get values) indices in
Array.sort cmp temp;
Line 2,204:
disjoint_sort compare values indices;
Array.iter (Printf.printf " %d") values;
print_newline()</
With lists:
<
let indices = List.sort compare indices in
let rec aux acc j = function
Line 2,238:
let res = disjoint_sort compare values indices in
List.iter (Printf.printf " %d") res;
print_newline()</
=={{header|ooRexx}}==
<
-- this could be a list, array, or queue as well because of polymorphism
-- also, ooRexx arrays are 1-based, so using the alternate index set for the
Line 2,277:
-- the two and returning the sign, we give the expected
-- results for the compares
return (left - right)~sign</
{{out}}
<pre>Sorted data is: [7, 0, 5, 4, 3, 2, 1, 6]</pre>
=={{header|Order}}==
<
#define ORDER_PP_DEF_8sort_disjoint_sublist ORDER_PP_FN( \
Line 2,300:
ORDER_PP(
8sort_disjoint_sublist(8seq(7, 6, 5, 4, 3, 2, 1, 0), 8tuple(6, 1, 7))
)</
=={{header|PARI/GP}}==
<
my(x=sum(i=1,#which,1<<(which[i]-1)),u=vecextract(v,x));
u=vecsort(u);
Line 2,309:
for(i=1,#which,v[which[i]]=u[i]);
v
};</
=={{header|pascal}}==
Tested free pascal.
<
program disjointsort;
Line 2,360:
readln;
end.
</syntaxhighlight>
<pre>
Before
Line 2,370:
=={{header|Perl}}==
<
use strict ;
Line 2,383:
my @indices = ( 6 , 1 , 7 ) ;
disjointSort( \@values , @indices ) ;
print "[@values]\n" ;</
Output:
<pre>[7 0 5 4 3 2 1 6]</pre>
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">disjoint_sort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">)</span>
Line 2,405:
<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>
<!--</
{{out}}
<pre>
Line 2,412:
=== Shorter Alternative Version ===
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">disjoint_sort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">)</span>
Line 2,420:
<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>
<!--</
same output.
=={{header|PicoLisp}}==
The indices are incremented here, as PicoLisp is 1-based
<
(mapc
'((V I) (set (nth Values I) V))
(sort (mapcar '((N) (get Values N)) Indices))
(sort Indices) )
Values )</
Output:
<pre>-> (7 0 5 4 3 2 1 6)</pre>
Line 2,438:
=== Using only predicates marked as "builtin" ===
<syntaxhighlight lang="prolog">
% ===
% Problem description
Line 2,532:
insert_fresh_vars(_,[],V,[],[],V).
</syntaxhighlight>
=== Alternatively, using predicates from <code>library(list)</code> ===
Line 2,538:
Using <code>append/3</code> from SWi-Prolog's [https://eu.swi-prolog.org/pldoc/man?section=lists library(list)]
<syntaxhighlight lang="prolog">
% ===
% Main predicate
Line 2,568:
length(Front, N),
append(Front, [Elem|Back], List).
</syntaxhighlight>
=== Unit Tests ===
Line 2,574:
Test code using the [https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/plunit.html%27) Unit Testing framework of SWI Prolog].
<syntaxhighlight lang="prolog">
% We use the "R" intermediate var to decouple processing by the predicate
% from subsequent checking against expected result.
Line 2,604:
rt :- run_tests(sort_disjoint_sublist).
</syntaxhighlight>
=={{header|PowerShell}}==
Line 2,610:
{{works with|PowerShell|4.0}}
<syntaxhighlight lang="powershell">
function sublistsort($values, $indices) {
$indices = $indices | sort
Line 2,620:
$indices = 6, 1, 7
"$(sublistsort $values $indices)"
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 2,628:
=={{header|PureBasic}}==
Based on the C implementation
<
Protected i, j
SortArray(idx(),#PB_Sort_Ascending)
Line 2,667:
EndProcedure
main()</
<pre>Before sort:
7 6 5 4 3 2 1 0
Line 2,677:
The function modifies the input data list in-place and follows the Python convention of returning None in such cases.
<
indices = sorted(indices)
values = sorted(data[i] for i in indices)
Line 2,694:
>>> </
Or, checking a dictionary for sublist indices, and returning a new (rather than mutated) list:
<
Line 2,759:
if __name__ == '__main__':
main()</
{{Out}}
<pre>Disjoint sublists at indices [6, 1, 7] sorted:
Line 2,769:
R lets you access elements of vectors with a vector of indices.
<
indices=c(7,2,8)
values[sort(indices)]=sort(values[indices])
print(values)</
Output:
<pre> 7 0 5 4 3 2 1 6</pre>
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
Line 2,790:
(sort-disjoint '(7 6 5 4 3 2 1 0) '(6 1 7))
;; --> '(7 0 5 4 3 2 1 6)
</syntaxhighlight>
=={{header|Raku}}==
Line 2,797:
===Inline===
Using L-value slice of the array, and `sort` as a mutating method:
<syntaxhighlight lang="raku"
my @indices = 6, 1, 7;
@values[ @indices.sort ] .= sort;
say @values;</
Output:<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
===Iterative===
<syntaxhighlight lang="raku"
my @sortedValues = @values[ @indices ].sort ;
for @indices.sort -> $insert {
Line 2,817:
my @indices = ( 6 , 1 , 7 ) ;
my @sortedValues = disjointSort( @values , @indices ) ;
say @sortedValues ;</
Output:
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 2,827:
The REXX language normally uses a one-based index.
<
parse arg old ',' idx /*obtain the optional lists from the CL*/
if old='' then old= 7 6 5 4 3 2 1 0 /*Not specified: Then use the default.*/
Line 2,857:
$= @.1; do j=2 to n; $= $ @.j
end /*j*/
return $</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,867:
=={{header|Ring}}==
<
aList = [7, 6, 5, 4, 3, 2, 1, 0]
indList = [7, 2, 8]
Line 2,887:
next
? "[" + left(svect, len(svect) - 1) + "]"
</syntaxhighlight>
{{out}}
<pre>
Line 2,895:
=={{header|Ruby}}==
By convention, the exlamation mark in the method name indicates that something potentially dangerous can happen. (In this case, the in place modification).
<
values = ar.values_at(*indices).sort
indices.sort.zip(values).each{ |i,v| ar[i] = v }
Line 2,903:
values = [7, 6, 5, 4, 3, 2, 1, 0]
indices = [6, 1, 7]
p sort_disjoint_sublist!(values, indices)</
Output
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 2,909:
=={{header|Run BASIC}}==
Normally we sort with SQLite in memory. Faster and less code
<
sortIdx$ = "7, 2, 8"
Line 2,938:
next i
wend
RETURN</
=={{header|Rust}}==
<
fn disjoint_sort(array: &mut [impl Ord], indices: &[usize]) {
Line 2,963:
println!("{:?}", array);
}
</syntaxhighlight>
{{out}}
Line 2,970:
=={{header|Scala}}==
{{libheader|Scala}}<
object SortedDisjointSubList extends App {
Line 2,986:
assert(sortSubList(subListIndex, list) == List(7, 0, 5, 4, 3, 2, 1, 6), "Incorrect sort")
println(s"List in sorted order.\nSuccessfully completed without errors. [total ${Platform.currentTime - executionStart} ms]")
}</
=={{header|Scheme}}==
{{works with|Gauche Scheme}}
<
(define num-list '(7 6 5 4 3 2 1 0))
(define indices '(6 1 7))
Line 3,005:
num-list)
<
car))</
{{output}}
<pre>
Line 3,012:
=={{header|Sidef}}==
<
values[indices.sort] = [values[indices]].sort...
}
Line 3,020:
disjointSort(values, indices);
say values;</
{{out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 3,027:
{{works with|SML/NJ}}
{{trans|Go}}
<
val sort : (A.elem * A.elem -> order) -> (A.array * int array) -> unit
end = struct
Line 3,068:
DisjointViewSort.sort cmp (arr, indices)
)
end</
Usage:
<pre>- structure IntArray = struct
Line 3,094:
{{trans|Go}}
Sorts an array "wrapper" which represents a "view" into the disjoint sublist of the array.
<
let array : UnsafeMutablePointer<T>
let indexes : [Int]
Line 3,119:
let ind = [6, 1, 7]
sortDisjointSublist(&a, ind)
println(a)</
{{out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 3,125:
=={{header|Tcl}}==
This returns the sorted copy of the list; this is idiomatic for Tcl programs where values are immutable.
<
proc disjointSort {values indices args} {
# Ensure that we have a unique list of integers, in order
Line 3,139:
# The updated list is the result
return $values
}</
Demonstration:
<
set indices {6 1 7}
puts \[[join [disjointSort $values $indices] ", "]\]</
Output:
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 3,149:
=={{header|TUSCRIPT}}==
TUSCRIPT indexing is one based
<
$$ MODE TUSCRIPT
values="7'6'5'4'3'2'1'0"
Line 3,160:
ENDLOOP
PRINT values
</syntaxhighlight>
Output:
<pre>
Line 3,167:
=={{header|Ursala}}==
<
#import nat
Line 3,174:
#cast %nL
t = disjoint_sort({6,1,7},<7,6,5,4,3,2,1,0>)</
output:
<pre><7,0,5,4,3,2,1,6></pre>
Line 3,180:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<
// sorts values in place, leaves indices unsorted
Line 3,199:
System.print("Initial: %(values)")
sortDisjoint.call(values, indices)
System.print("Sorted : %(values)")</
{{out}}
Line 3,208:
=={{header|XPL0}}==
<
int Values, Indices, J, I, T;
[Values:= [7, 6, 5, 4, 3, 2, 1, 0];
Line 3,222:
for I:= 0 to 8-1 do
[IntOut(0, Values(I)); ChOut(0, ^ )];
]</
{{out}}
Line 3,230:
=={{header|zkl}}==
<
indices:=T(6, 1, 7);
Line 3,236:
.zip(indices.sort()) //-->(v,i) == L(L(0,1),L(1,6),L(6,7))
.reduce(fcn(newList,[(v,i)]){ newList[i]=v; newList },values.copy())
.println(); // new list</
This is an create-new-object version. An in place version is almost identical:
<
indices.apply(values.get).sort() // a.get(0) == a[0]
Line 3,244:
.apply2(fcn([(v,i)],list){ list[i]=v },values);
values.println(); // modified list</
{{out}}<pre>L(7,0,5,4,3,2,1,6)</pre>
|