Sort disjoint sublist: Difference between revisions

m
(Grouping BASIC dialects)
m (→‎{{header|Wren}}: Minor tidy)
 
(10 intermediate revisions by 5 users not shown)
Line 1,228:
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|PureBasic}}===
Based on the C implementation
<syntaxhighlight lang="purebasic">Procedure Bubble_sort(Array idx(1), n, Array buf(1))
Protected i, j
SortArray(idx(),#PB_Sort_Ascending)
For i=0 To n
For j=i+1 To n
If buf(idx(j)) < buf(idx(i))
Swap buf(idx(j)), buf(idx(i))
EndIf
Next
Next
EndProcedure
 
Procedure main()
DataSection
values: Data.i 7, 6, 5, 4, 3, 2, 1, 0
indices:Data.i 6, 1, 7
EndDataSection
Dim values.i(7) :CopyMemory(?values, @values(), SizeOf(Integer)*8)
Dim indices.i(2):CopyMemory(?indices,@indices(),SizeOf(Integer)*3)
If OpenConsole()
Protected i
PrintN("Before sort:")
For i=0 To ArraySize(values())
Print(Str(values(i))+" ")
Next
PrintN(#CRLF$+#CRLF$+"After sort:")
Bubble_sort(indices(), ArraySize(indices()), values())
For i=0 To ArraySize(values())
Print(Str(values(i))+" ")
Next
Print(#CRLF$+#CRLF$+"Press ENTER to exit")
Input()
EndIf
EndProcedure
 
main()</syntaxhighlight>
{{out}}
<pre>Before sort:
7 6 5 4 3 2 1 0
 
After sort:
7 0 5 4 3 2 1 6</pre>
 
==={{header|Run BASIC}}===
Line 1,296 ⟶ 1,345:
 
END</syntaxhighlight>
{{out}}
<pre>Same as BASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">dim values(7)
values(0) = 7 : values(1) = 6 : values(2) = 5
values(3) = 4 : values(4) = 3 : values(5) = 2
values(6) = 1 : values(7) = 0
 
dim indices(2)
indices(0) = 6 : indices(1) = 1 : indices(2) = 7
 
print "Before sort:"
for i = 0 to arraysize(values(),1)
print values(i), " ";
next i
 
print "\n\nAfter sort:"
for i = 0 to 1
if values(indices(i)) > values(indices(i + 1)) then
temp = values(indices(i)) : values(indices(i)) = values(indices(i+1)) : values(indices(i+1)) = temp
end if
next i
for i = 0 to arraysize(values(),1)
print values(i), " ";
next i
end</syntaxhighlight>
{{out}}
<pre>Same as BASIC entry.</pre>
Line 1,596 ⟶ 1,672:
{{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,726:
L2.Free;
end;
end;</syntaxhighlight>
end;
{{out}}<pre>
 
[7 0 5 4 3 2 1 6]
Elapsed Time: 0.626 ms.</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
val[] = [ 7 6 5 4 3 2 1 0 ]
ind[] = [ 7 2 8 ]
#
for i = 1 to len ind[] - 1
for j = i + 1 to len ind[]
if ind[j] < ind[i]
swap ind[j] ind[i]
.
.
.
for i = 1 to len ind[] - 1
for j = i + 1 to len ind[]
if val[ind[j]] < val[ind[i]]
swap val[ind[j]] val[ind[i]]
.
.
.
print val[]
</syntaxhighlight>
{{out}}
<pre>
[7 0 5 4 3 2 1 6]
Elapsed Time: 0.626 ms.
 
</pre>
 
 
=={{header|EchoLisp}}==
Line 1,686 ⟶ 1,774:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,695 ⟶ 1,783:
sortSublist(indices)
{
var subList := indices.orderBy::(x => x)
.zipBy(indices.selectBy::(i => self[i])
.orderBy::(x => x), (index,val => new{ Index = index; Value = val; }))
.toArray();
var list := self.clone();
subList.forEach::(r)
{
list[r.Index] := r.Value
Line 1,717 ⟶ 1,805:
}</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,827:
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,855:
</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,908:
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,940:
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 ⟶ 2,044:
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,208:
 
=={{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,234:
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,335:
===ES5===
====Iterative====
 
Does not check for duplicate indices.
<syntaxhighlight lang="javascript">function sort_disjoint(values, indices) {
Line 2,287 ⟶ 2,354:
 
====Functional====
 
<syntaxhighlight lang="javascript">(function () {
'use strict';
Line 2,315 ⟶ 2,381:
 
})();</syntaxhighlight>
 
{{Out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
===ES6===
 
<syntaxhighlight lang="javascript">(() => {
'use strict';
Line 2,423 ⟶ 2,487:
 
=={{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,504:
=={{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,519:
 
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,531:
 
===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,555:
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,648:
io.write( values[i], " " )
end</syntaxhighlight>
{{out}}
<pre>7 0 5 4 3 2 1 6</pre>
 
Line 2,613 ⟶ 2,669:
Values[[Sort[Indices]]] = Sort[Values[[Indices]]];
Values</syntaxhighlight>
{{out}}
<pre>{ 7, 0, 5, 4, 3, 2, 1, 6 }</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
sortDisjointSublist = function(arr, indexes)
indexes.sort
newArr = arr[:]
sublist = []
for i in indexes
sublist.push(arr[i])
end for
sublist.sort
for i in range(0, indexes.len - 1)
arrIx = indexes[i]
newArr[arrIx] = sublist[i]
end for
return newArr
end function
 
print sortDisjointSublist([7,6,5,4,3,2,1,0], [6,1,7])
</syntaxhighlight>
{{out}}
<pre>
[7, 0, 5, 4, 3, 2, 1, 6]
</pre>
 
=={{header|NetRexx}}==
Line 2,661 ⟶ 2,742:
</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,771:
sortDisjoinSublist(d, @[6, 1, 7])
echo d</syntaxhighlight>
{{out}}
Output:
<pre>@[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
Line 2,764 ⟶ 2,841:
 
=={{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,855:
print_newline()</syntaxhighlight>
 
===With lists:===
 
<syntaxhighlight lang="ocaml">let disjoint_sort cmp values indices =
let indices = List.sort compare indices in
Line 2,935 ⟶ 3,009:
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 ⟶ 3,029:
disjointSort( \@values , @indices ) ;
print "[@values]\n" ;</syntaxhighlight>
{{out}}
Output:
<pre>[7 0 5 4 3 2 1 6]</pre>
 
Line 2,981 ⟶ 3,052:
<!--</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 ⟶ 3,064:
<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.
<pre>same output.</pre>
 
=={{header|PicoLisp}}==
Line 3,005 ⟶ 3,075:
(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,177:
 
=== 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,212:
 
=== 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,261:
"$(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}}==
Based on the C implementation
<syntaxhighlight lang="purebasic">Procedure Bubble_sort(Array idx(1), n, Array buf(1))
Protected i, j
SortArray(idx(),#PB_Sort_Ascending)
For i=0 To n
For j=i+1 To n
If buf(idx(j)) < buf(idx(i))
Swap buf(idx(j)), buf(idx(i))
EndIf
Next
Next
EndProcedure
 
Procedure main()
DataSection
values: Data.i 7, 6, 5, 4, 3, 2, 1, 0
indices:Data.i 6, 1, 7
EndDataSection
Dim values.i(7) :CopyMemory(?values, @values(), SizeOf(Integer)*8)
Dim indices.i(2):CopyMemory(?indices,@indices(),SizeOf(Integer)*3)
If OpenConsole()
Protected i
PrintN("Before sort:")
For i=0 To ArraySize(values())
Print(Str(values(i))+" ")
Next
PrintN(#CRLF$+#CRLF$+"After sort:")
Bubble_sort(indices(), ArraySize(indices()), values())
For i=0 To ArraySize(values())
Print(Str(values(i))+" ")
Next
Print(#CRLF$+#CRLF$+"Press ENTER to exit")
Input()
EndIf
EndProcedure
 
main()</syntaxhighlight>
<pre>Before sort:
7 6 5 4 3 2 1 0
 
After sort:
7 0 5 4 3 2 1 6</pre>
 
=={{header|Python}}==
Line 3,339 ⟶ 3,355:
[7, 6, 5, 4, 3, 2, 1, 0] -> [7, 0, 5, 4, 3, 2, 1, 6]
['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'] -> ['h', 'a', 'f', 'e', 'd', 'c', 'b', 'g']</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ sort tuck [] unrot
witheach
[ dip dup peek
rot join swap ]
swap sort
dip swap witheach
[ over i^ peek
dip rot poke
swap ]
drop ] is sortdisjointsublist ( [ [ --> [ )
 
' [ 7 6 5 4 3 2 1 0 ] ' [ 6 1 7 ] sortdisjointsublist echo
</syntaxhighlight>
 
{{out}}
 
<pre>[ 7 0 5 4 3 2 1 6 ]</pre>
 
=={{header|R}}==
Line 3,351 ⟶ 3,387:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
#lang racket
 
(define (sort-disjoint l is)
Line 3,363 ⟶ 3,398:
 
(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,392 ⟶ 3,426:
my @sortedValues = disjointSort( @values , @indices ) ;
say @sortedValues ;</syntaxhighlight>
{{out}}
Output:
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
Line 3,433 ⟶ 3,467:
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,495:
</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,509:
{ 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}}==
Line 3,726 ⟶ 3,754:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "./sort" for Sort
 
// sorts values in place, leaves indices unsorted
9,479

edits