Jump to content

Sort disjoint sublist: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 27:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F sort_disjoint_sublist(&data, indices)
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)</langsyntaxhighlight>
 
{{out}}
Line 44:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC PrintArray(INT ARRAY a INT size)
INT i
 
Line 108:
Test(arr,8,ind,3)
RETURN</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, GNAT.Bubble_Sort;
use Ada.Text_IO;
 
Line 177:
New_Line;
 
end DisjointSort;</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
Uses the quicksort procedure from the Sorting Algorithms/Quicksort task and a variant for indexed sorting.
<langsyntaxhighlight lang="algolw">begin % sort a disjoint sub-set of a list %
% Quicksorts in-place the array of integers v, from lb to ub %
procedure quicksort ( integer array v( * )
Line 264:
writeon( " ]" )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 271:
 
=={{header|APL}}==
<langsyntaxhighlight lang="apl">
∇SDS[⎕]∇
Line 278:
[2] Z←L
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 292:
Uses Foundation framework (through the ObjC interface), and works with versions of AppleScript from OS X 10.10 onwards.
 
<langsyntaxhighlight AppleScriptlang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 488:
return lst
end tell
end zipWith</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight AppleScriptlang="applescript">{7, 0, 5, 4, 3, 2, 1, 6}</langsyntaxhighlight>
 
===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.
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS 10.9 (Mavericks) or later.
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</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{7, 0, 5, 4, 3, 2, 1, 6}</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">sortDisjoint: function [data, indices][
result: new data
inds: new indices
Line 543:
d: [7 6 5 4 3 2 1 0]
print sortDisjoint d [6 1 7]</langsyntaxhighlight>
 
{{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.)
 
<langsyntaxhighlight ATSlang="ats">#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"
 
Line 714:
println! values;
free values
end</langsyntaxhighlight>
 
{{out}}
Line 722:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Sort_disjoint_sublist(Values, Indices){
A := [], B:=[], C := [], D := []
for k, v in Indices
Line 733:
Values[C[A_Index]] := D[A_Index]
return Values
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">Values := [7, 6, 5, 4, 3, 2, 1, 0]
Indices := [7, 2, 8]
Values := Sort_disjoint_sublist(Values, Indices)
for k, v in Values
output .= v ", "
MsgBox % "[" Trim(output, ", ") "]" ; show output</langsyntaxhighlight>
{{out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 745:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB"
Sort% = FN_sortinit(0,0) : REM Ascending
Line 776:
o$ += STR$(l%(i%)) + ", "
NEXT
= LEFT$(LEFT$(o$)) + "]"</langsyntaxhighlight>
'''Output:'''
<pre>
Line 783:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">7 6 5 4 3 2 1 0:?values
& 6 1 7:?indices
& 0:?sortedValues:?sortedIndices
Line 798:
& !A !value !Z:?values
)
& out$!values;</langsyntaxhighlight>
Output:
<pre>7 0 5 4 3 2 1 6</pre>
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
/* yes, bubble sort */
Line 835:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Collections.Generic;
Line 866:
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iterator>
Line 902:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 910:
{{trans|Go}}
Solution that sorts using a custom iterator that iterates a disjoint sublist.
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iterator>
Line 971:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 978:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn disjoint-sort [coll idxs]
(let [val-subset (keep-indexed #(when ((set idxs) %) %2) coll)
replacements (zipmap (set idxs) (sort val-subset))]
(apply assoc coll (flatten (seq replacements)))))</langsyntaxhighlight>
 
{{out}}
Line 988:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun disjoint-sort (values indices)
"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)</langsyntaxhighlight>
{{out}}
<pre>CL-USER> (disjoint-sort #(7 6 5 4 3 2 1 0) #(6 1 7))
Line 1,002:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.algorithm, std.range, std.array;
 
void main() {
Line 1,011:
 
assert(data == [7, 0, 5, 4, 3, 2, 1, 6]);
}</langsyntaxhighlight>
===Lower Level version===
<langsyntaxhighlight lang="d">import std.algorithm: swap;
 
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]);
}</langsyntaxhighlight>
 
===Simple Alternative Version===
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm;
 
void main() {
Line 1,074:
 
assert(data == [7.0, 0.0, 5.0, 4.0, 3.0, 2.0, 1.0, 6.0]);
}</langsyntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(define (sort-disjoint values indices)
(define sorted (list-sort <
Line 1,094:
(task)
→ (7 0 5 4 3 2 1 6)
</syntaxhighlight>
</lang>
 
=={{header|Elena}}==
ELENA 5.0 :
<langsyntaxhighlight lang="elena">import extensions;
import system'routines;
import system'culture;
Line 1,126:
console.printLine(list.sortSublist(new int[]{6, 1, 7}).asEnumerable())
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,133:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Sort_disjoint do
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)</langsyntaxhighlight>
 
{{out}}
Line 1,159:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<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>
</lang>
{{out}}
<pre>
Line 1,189:
 
=={{header|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM DISJOINT
 
DIM LST%[7],INDICES%[2]
Line 1,236:
ShowList(LST%[]->O$)
PRINT(O$)
END PROGRAM</langsyntaxhighlight>
{{out}}
<pre>
Line 1,243:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">include sort.e
 
function uniq(sequence s)
Line 1,271:
 
constant data = {7, 6, 5, 4, 3, 2, 1, 0}
constant indexes = {7, 2, 8}</langsyntaxhighlight>
 
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#.
<langsyntaxhighlight lang="fsharp">let sortDisjointSubarray data indices =
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]))</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">: disjoint-sort! ( values indices -- values' )
over <enumerated> nths unzip swap [ natural-sort ] bi@
pick [ set-nth ] curry 2each ;</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="factor">IN: scratchpad { 7 6 5 4 3 2 1 0 } { 6 1 7 } disjoint-sort! .
{ 7 0 5 4 3 2 1 6 }</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program Example
implicit none
 
Line 1,336:
end subroutine Isort
end program Example</langsyntaxhighlight>
Output
<pre> 7 0 5 4 3 2 1 6</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim as integer value(0 to 7) = {7, 6, 5, 4, 3, 2, 1, 0}
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</langsyntaxhighlight>
{{out}}<pre> 7 0 5 4 3 2 1 6</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,392:
}
fmt.Println("sorted: ", values)
}</langsyntaxhighlight>
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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,457:
sort.Sort(s)
fmt.Println("sorted: ", s.values)
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Groovy allows List-valued indexing to "gather" and "scatter" arbitrary sublists, making the solution almost trivial.
<langsyntaxhighlight lang="groovy">def sparseSort = { a, indices = ([] + (0..<(a.size()))) ->
indices.sort().unique()
a[indices] = a[indices].sort()
a
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">def a = [7, 6, 5, 4, 3, 2, 1, 0]
 
println a
Line 1,476:
println a
println sparseSort(a) // default == sort all
println a</langsyntaxhighlight>
 
Output:
Line 1,490:
Here are three variations on the solution: using ordinary lists, immutable "boxed" arrays, and mutable "unboxed" arrays.
 
<langsyntaxhighlight lang="haskell">import Control.Monad
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</langsyntaxhighlight>
 
 
Or, in terms of Data.Map:
<langsyntaxhighlight lang="haskell">import Data.Map as M (fromList, keys, lookup)
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]</langsyntaxhighlight>
{{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).
 
<langsyntaxhighlight lang="icon">
link sort # get the 'isort' procedure for sorting a list
 
Line 1,588:
write ()
end
</syntaxhighlight>
</lang>
 
Output:
Line 1,597:
</pre>
 
The expression <code>!indices</code> generates the value of each index in turn, so the line <langsyntaxhighlight lang="icon">every put (values, result[!indices])</langsyntaxhighlight> effectively loops through each index, putting <code>result[index]</code> into the list 'values'.
 
=={{header|Io}}==
Io does not come with a set type.
<langsyntaxhighlight Iolang="io">List disjointSort := method(indices,
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</langsyntaxhighlight>
{{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.
 
<langsyntaxhighlight lang="j"> 7 6 5 4 3 2 1 0 (/:~@:{`[`]}~ /:~@~.) 6 1 7
7 0 5 4 3 2 1 6</langsyntaxhighlight>
 
Compare this with:
<langsyntaxhighlight lang="j"> 6 1 7 /:~@:{`[`]} 7 6 5 4 3 2 1 0
7 1 5 4 3 2 0 6</langsyntaxhighlight>
 
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.
<langsyntaxhighlight lang="java5">import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Line 1,654:
System.out.println(list);
}
}</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Line 1,685:
System.out.println(list);
}
}</langsyntaxhighlight>
{{out}}
<pre>[7, 6, 5, 4, 3, 2, 1, 0]
Line 1,695:
 
Does not check for duplicate indices.
<langsyntaxhighlight JavaScriptlang="javascript">function sort_disjoint(values, indices) {
var sublist = [];
indices.sort(function(a, b) { return a > b; });
Line 1,710:
 
return values;
}</langsyntaxhighlight>
 
====Functional====
 
<langsyntaxhighlight JavaScriptlang="javascript">(function () {
'use strict';
 
Line 1,740:
return disjointSort([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])
 
})();</langsyntaxhighlight>
 
{{Out}}
Line 1,747:
===ES6===
 
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,844:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{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:
<langsyntaxhighlight lang="jq">def setpaths(indices; values):
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) ;</langsyntaxhighlight>
Example:<langsyntaxhighlight lang="jq">
[7, 6, 5, 4, 3, 2, 1, 0] | disjointSort( [6, 1, 7] )</langsyntaxhighlight>produces:
[7,0,5,4,3,2,1,6]
 
Line 1,868:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function sortselected(a::AbstractVector{<:Real}, s::AbstractVector{<:Integer})
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")</langsyntaxhighlight>
 
{{out}}
Line 1,890:
 
=={{header|K}}==
<syntaxhighlight lang="k">
<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>
</lang>
 
===Another way===
<syntaxhighlight lang="k">
<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>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
/* in place sort */
Line 1,923:
println("Sorted array : ${values.asList()}")
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,932:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 1,979:
done
 
print "After sort: ${arr_Val[*]}"</langsyntaxhighlight>
{{out}}<pre>
Before sort: 7 6 5 4 3 2 1 0
Line 1,985:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">values = { 7, 6, 5, 4, 3, 2, 1, 0 }
indices = { 6, 1, 7 }
 
Line 2,018:
for i = 1, #values do
io.write( values[i], " " )
end</langsyntaxhighlight>
<pre>7 0 5 4 3 2 1 6</pre>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">sortDisjoint := proc(values, indices::set)
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});</langsyntaxhighlight>
{{out}}
<pre>[7 0 5 4 3 2 1 6]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Values = { 7, 6, 5, 4, 3, 2, 1, 0} ; Indices = { 7, 2, 8 };
Values[[Sort[Indices]]] = Sort[Values[[Indices]]];
Values</langsyntaxhighlight>
<pre>{ 7, 0, 5, 4, 3, 2, 1, 6 }</pre>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,085:
say 'Idx:' iList.space
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,097:
{{works with|Q'Nial Version 6.3}}
 
<syntaxhighlight lang="nial">
<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>
</lang>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import algorithm
 
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</langsyntaxhighlight>
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.
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
@interface DisjointSublistView : NSMutableArray {
Line 2,176:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>(
Line 2,193:
With arrays:
 
<langsyntaxhighlight lang="ocaml">let disjoint_sort cmp values indices =
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()</langsyntaxhighlight>
 
With lists:
 
<langsyntaxhighlight lang="ocaml">let disjoint_sort cmp values indices =
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()</langsyntaxhighlight>
 
=={{header|ooRexx}}==
<langsyntaxhighlight ooRexxlang="oorexx">data = .array~of(7, 6, 5, 4, 3, 2, 1, 0)
-- 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</langsyntaxhighlight>
{{out}}
<pre>Sorted data is: [7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
=={{header|Order}}==
<langsyntaxhighlight lang="c">#include <order/interpreter.h>
 
#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))
)</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">sortsome(v,which)={
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
};</langsyntaxhighlight>
 
=={{header|pascal}}==
Tested free pascal.
<langsyntaxhighlight lang="pascal">
 
program disjointsort;
Line 2,360:
readln;
end.
</syntaxhighlight>
</lang>
<pre>
Before
Line 2,370:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">#!/usr/bin/perl -w
use strict ;
 
Line 2,383:
my @indices = ( 6 , 1 , 7 ) ;
disjointSort( \@values , @indices ) ;
print "[@values]\n" ;</langsyntaxhighlight>
Output:
<pre>[7 0 5 4 3 2 1 6]</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,412:
 
=== Shorter Alternative Version ===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
same output.
 
=={{header|PicoLisp}}==
The indices are incremented here, as PicoLisp is 1-based
<langsyntaxhighlight PicoLisplang="picolisp">(let (Values (7 6 5 4 3 2 1 0) Indices (7 2 8))
(mapc
'((V I) (set (nth Values I) V))
(sort (mapcar '((N) (get Values N)) Indices))
(sort Indices) )
Values )</langsyntaxhighlight>
Output:
<pre>-> (7 0 5 4 3 2 1 6)</pre>
Line 2,438:
=== Using only predicates marked as "builtin" ===
 
<syntaxhighlight lang="prolog">
<lang Prolog>
% ===
% Problem description
Line 2,532:
insert_fresh_vars(_,[],V,[],[],V).
</syntaxhighlight>
</lang>
 
=== 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">
<lang Prolog>
% ===
% Main predicate
Line 2,568:
length(Front, N),
append(Front, [Elem|Back], List).
</syntaxhighlight>
</lang>
 
=== 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">
<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>
</lang>
 
=={{header|PowerShell}}==
Line 2,610:
{{works with|PowerShell|4.0}}
 
<syntaxhighlight lang="powershell">
<lang PowerShell>
function sublistsort($values, $indices) {
$indices = $indices | sort
Line 2,620:
$indices = 6, 1, 7
"$(sublistsort $values $indices)"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 2,628:
=={{header|PureBasic}}==
Based on the C implementation
<langsyntaxhighlight PureBasiclang="purebasic">Procedure Bubble_sort(Array idx(1), n, Array buf(1))
Protected i, j
SortArray(idx(),#PB_Sort_Ascending)
Line 2,667:
EndProcedure
 
main()</langsyntaxhighlight>
<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.
 
<langsyntaxhighlight lang="python">>>> def sort_disjoint_sublist(data, indices):
indices = sorted(indices)
values = sorted(data[i] for i in indices)
Line 2,694:
 
>>> </langsyntaxhighlight>
 
 
Or, checking a dictionary for sublist indices, and returning a new (rather than mutated) list:
<langsyntaxhighlight lang="python">'''Disjoint sublist sorting'''
 
 
Line 2,759:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{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.
 
<langsyntaxhighlight Rlang="r"> values=c(7,6,5,4,3,2,1,0)
indices=c(7,2,8)
values[sort(indices)]=sort(values[indices])
print(values)</langsyntaxhighlight>
Output:
<pre> 7 0 5 4 3 2 1 6</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<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>
</lang>
 
=={{header|Raku}}==
Line 2,797:
===Inline===
Using L-value slice of the array, and `sort` as a mutating method:
<syntaxhighlight lang="raku" perl6line>my @values = 7, 6, 5, 4, 3, 2, 1, 0;
my @indices = 6, 1, 7;
 
@values[ @indices.sort ] .= sort;
 
say @values;</langsyntaxhighlight>
Output:<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
===Iterative===
<syntaxhighlight lang="raku" perl6line>sub disjointSort( @values, @indices --> List ) {
my @sortedValues = @values[ @indices ].sort ;
for @indices.sort -> $insert {
Line 2,817:
my @indices = ( 6 , 1 , 7 ) ;
my @sortedValues = disjointSort( @values , @indices ) ;
say @sortedValues ;</langsyntaxhighlight>
Output:
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 2,827:
 
The REXX language normally uses a one-based index.
<langsyntaxhighlight lang="rexx">/*REXX program uses a disjointed sublist to sort a random list of values. */
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 $</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,867:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
aList = [7, 6, 5, 4, 3, 2, 1, 0]
indList = [7, 2, 8]
Line 2,887:
next
? "[" + left(svect, len(svect) - 1) + "]"
</syntaxhighlight>
</lang>
{{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).
<langsyntaxhighlight lang="ruby">def sort_disjoint_sublist!(ar, indices)
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)</langsyntaxhighlight>
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
<langsyntaxhighlight lang="runbasic">sortData$ = "7, 6, 5, 4, 3, 2, 1, 0"
sortIdx$ = "7, 2, 8"
 
Line 2,938:
next i
wend
RETURN</langsyntaxhighlight>
 
=={{header|Rust}}==
 
 
<langsyntaxhighlight lang="rust">use std::collections::BTreeSet;
 
fn disjoint_sort(array: &mut [impl Ord], indices: &[usize]) {
Line 2,963:
println!("{:?}", array);
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,970:
 
=={{header|Scala}}==
{{libheader|Scala}}<langsyntaxhighlight lang="scala">import scala.compat.Platform
 
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]")
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
{{works with|Gauche Scheme}}
<langsyntaxhighlight Schemelang="scheme">(use gauche.sequence)
(define num-list '(7 6 5 4 3 2 1 0))
(define indices '(6 1 7))
Line 3,005:
num-list)
<
car))</langsyntaxhighlight>
{{output}}
<pre>
Line 3,012:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func disjointSort(values, indices) {
values[indices.sort] = [values[indices]].sort...
}
Line 3,020:
 
disjointSort(values, indices);
say values;</langsyntaxhighlight>
{{out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 3,027:
{{works with|SML/NJ}}
{{trans|Go}}
<langsyntaxhighlight lang="sml">functor SortDisjointFn (A : MONO_ARRAY) : sig
val sort : (A.elem * A.elem -> order) -> (A.array * int array) -> unit
end = struct
Line 3,068:
DisjointViewSort.sort cmp (arr, indices)
)
end</langsyntaxhighlight>
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.
<langsyntaxhighlight lang="swift">struct DisjointSublistView<T> : MutableCollectionType {
let array : UnsafeMutablePointer<T>
let indexes : [Int]
Line 3,119:
let ind = [6, 1, 7]
sortDisjointSublist(&a, ind)
println(a)</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
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
}</langsyntaxhighlight>
Demonstration:
<langsyntaxhighlight lang="tcl">set values {7 6 5 4 3 2 1 0}
set indices {6 1 7}
puts \[[join [disjointSort $values $indices] ", "]\]</langsyntaxhighlight>
Output:
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
Line 3,149:
=={{header|TUSCRIPT}}==
TUSCRIPT indexing is one based
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
values="7'6'5'4'3'2'1'0"
Line 3,160:
ENDLOOP
PRINT values
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,167:
 
=={{header|Ursala}}==
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
Line 3,174:
#cast %nL
 
t = disjoint_sort({6,1,7},<7,6,5,4,3,2,1,0>)</langsyntaxhighlight>
output:
<pre><7,0,5,4,3,2,1,6></pre>
Line 3,180:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<langsyntaxhighlight lang="ecmascript">import "/sort" for Sort
 
// sorts values in place, leaves indices unsorted
Line 3,199:
System.print("Initial: %(values)")
sortDisjoint.call(values, indices)
System.print("Sorted : %(values)")</langsyntaxhighlight>
 
{{out}}
Line 3,208:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include xpllib; \for Sort routine
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, ^ )];
]</langsyntaxhighlight>
 
{{out}}
Line 3,230:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">values :=T(7, 6, 5, 4, 3, 2, 1, 0);
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</langsyntaxhighlight>
This is an create-new-object version. An in place version is almost identical:
<langsyntaxhighlight lang="zkl">values :=L(7, 6, 5, 4, 3, 2, 1, 0);
 
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</langsyntaxhighlight>
{{out}}<pre>L(7,0,5,4,3,2,1,6)</pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.