Sort disjoint sublist: Difference between revisions

m
(add task to arm assembly raspberry pi)
m (→‎{{header|Wren}}: Minor tidy)
 
(13 intermediate revisions by 6 users not shown)
Line 42:
[7, 0, 5, 4, 3, 2, 1, 6]
</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program sublistSort64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessStart: .asciz "Program 64 bits start.\n"
sMessResult: .ascii "Value : "
sMessValeur: .fill 11, 1, ' ' // size => 11
szCarriageReturn: .asciz "\n"
.align 4
ArrayNumber: .quad 7, 6, 5, 4, 3, 2, 1, 0
.equ NBELEMENTS, (. - ArrayNumber) / 8
ArrayIndex: .quad 6,1,7
.equ NBELEMINDEX, (. - ArrayIndex) / 8
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
ArrayExtract: .skip 8 * NBELEMINDEX
/*********************************/
/* code section */
/*********************************/
.text
.global main
main:
ldr x0,qAdrszMessStart
bl affichageMess
ldr x4,qAdrArrayNumber // number array address
ldr x5,qAdrArrayIndex // index array address
ldr x6,qAdrArrayExtract // extract array address
mov x3,#0 // index
1:
ldr x0,[x5,x3,lsl #3] // load index
ldr x1,[x4,x0,lsl #3] // load value of index
str x1,[x6,x3,lsl #3] // store value in array extract
add x3,x3,#1 // increment index
cmp x3,#NBELEMINDEX // end array index ?
blt 1b // no -> loop
 
mov x0,x5 // index array address
mov x1,#0 // first element
mov x2,#NBELEMINDEX // array size
bl insertionSort
mov x0,x6 // extract array address
mov x1,#0 // first element
mov x2,#NBELEMINDEX // array size
bl insertionSort
 
mov x3,#0 // init index
2:
ldr x0,[x6,x3,lsl #3] // load value of array extract
ldr x1,[x5,x3,lsl #3] // load index
str x0,[x4,x1,lsl #3] // store value in number array in index place
add x3,x3,#1 // increment indice
cmp x3,#NBELEMINDEX
blt 2b
 
mov x0,x4 // number array address
bl displayArray
100: // standard end of the program
mov x0, #0 // return code
mov x8, #EXIT // request to exit program
svc #0 // perform the system call
qAdrsMessValeur: .quad sMessValeur
qAdrszMessStart: .quad szMessStart
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResult: .quad sMessResult
qAdrArrayNumber: .quad ArrayNumber
qAdrArrayIndex: .quad ArrayIndex
qAdrArrayExtract: .quad ArrayExtract
/******************************************************************/
/* insertion sort */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the first element */
/* x2 contains the number of element */
insertionSort:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]!
stp x4,x5,[sp,-16]!
stp x6,x7,[sp,-16]!
add x3,x1,#1 // start index i
1: // start loop
ldr x4,[x0,x3,lsl #3] // load value A[i]
sub x5,x3,#1 // index j
2:
ldr x6,[x0,x5,lsl #3] // load value A[j]
cmp x6,x4 // compare value
ble 3f
add x5,x5,#1 // increment index j
str x6,[x0,x5,lsl #3] // store value A[j+1]
subs x5,x5,#2 // j = j - 1
bge 2b // loop if j >= 0
3:
add x5,x5,#1 // increment index j
str x4,[x0,x5,lsl #3] // store value A[i] in A[j+1]
add x3,x3,#1 // increment index i
cmp x3,x2 // end ?
blt 1b // no -> loop
 
100:
ldp x6,x7,[sp],16
ldp x4,x5,[sp],16
ldp x2,x3,[sp],16
ldp x1,lr,[sp],16
ret
 
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* x0 contains the address of array */
displayArray:
stp x1,lr,[sp,-16]!
stp x2,x3,[sp,-16]!
mov x2,x0 // table address
mov x3,#0
1: // loop display table
ldr x0,[x2,x3,lsl #3]
ldr x1,qAdrsMessValeur // display value
bl conversion10 // call function
ldr x0,qAdrsMessResult
bl affichageMess // display message
ldr x0,qAdrszCarriageReturn
bl affichageMess
add x3,x3,#1
cmp x3,#NBELEMENTS
blt 1b
ldr x0,qAdrszCarriageReturn
bl affichageMess
100:
ldp x2,x3,[sp],16
ldp x1,lr,[sp],16
ret
 
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
Value : 7
Value : 0
Value : 5
Value : 4
Value : 3
Value : 2
Value : 1
Value : 6
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC PrintArray(INT ARRAY a INT size)
Line 897 ⟶ 1,062:
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
=={{header|BBC BASIC}}==
==={{header|Applesoft BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB"
Line 931 ⟶ 1,100:
NEXT
= LEFT$(LEFT$(o$)) + "]"</syntaxhighlight>
{{out}}
'''Output:'''
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
<pre>
 
[7, 0, 5, 4, 3, 2, 1, 6]
==={{header|Chipmunk Basic}}===
</pre>
{{works with|Chipmunk Basic|3.6.4}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
<syntaxhighlight lang="qbasic">100 cls
110 dim values(7)
120 data 7,6,5,4,3,2,1,0
130 for i = 0 to 7
140 read values(i)
150 next
160 dim indices(2)
170 data 6,1,7
180 for i = 0 to 2
190 read indices(i)
200 next
210 print "Before sort:"
220 for i = 0 to ubound(values)
230 print values(i);
240 next i
250 print
260 print
270 print "After sort:"
280 for i = 0 to 1
290 if values(indices(i)) > values(indices(i+1)) then
300 temp = values(indices(i)) : values(indices(i)) = values(indices(i+1)) : values(indices(i+1)) = temp
310 endif
320 next i
330 for i = 0 to ubound(values)
340 print values(i);
350 next i
360 print
370 end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight 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
 
for i = 0 to 1
if value(index(i))>value(index(i+1)) then
swap value(index(i)), value(index(i+1))
end if
next i
 
for i = 0 to 7
print value(i);
next i : print</syntaxhighlight>
{{out}}<pre> 7 0 5 4 3 2 1 6</pre>
 
==={{header|GW-BASIC}}===
{{works with|Applesoft BASIC}}
{{works with|Chipmunk Basic}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
{{works with|Quite BASIC}}
<syntaxhighlight lang="qbasic">100 CLS : rem 100 HOME for Applesoft BASIC
110 DIM V(7)
120 DATA 7,6,5,4,3,2,1,0
130 FOR C = 0 TO 7
140 READ V(C)
150 NEXT C
160 DIM I(2)
170 DATA 6,1,7
180 FOR C = 0 TO 2
190 READ I(C)
200 NEXT C
210 PRINT "Before sort:"
220 FOR C = 0 TO 7
230 PRINT V(C); " ";
240 NEXT C
250 PRINT
260 PRINT
270 PRINT "After sort:"
280 FOR C = 0 TO 1
290 IF V(I(C)) > V(I(C+1)) THEN LET T = V(I(C)) : LET V(I(C)) = V(I(C+1)) : LET V(I(C+1)) = T
300 NEXT C
310 FOR C = 0 TO 7
320 PRINT V(C); " ";
330 NEXT C
340 END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Minimal BASIC}}===
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC|any}}
<syntaxhighlight lang="qbasic">110 DIM V(7)
120 DATA 7,6,5,4,3,2,1,0
130 FOR C = 0 TO 7
140 READ V(C)
150 NEXT C
160 DIM I(2)
170 DATA 6,1,7
180 FOR C = 0 TO 2
190 READ I(C)
200 NEXT C
210 PRINT "BEFORE SORT:"
220 FOR C = 0 TO 7
230 PRINT V(C);" ";
240 NEXT C
250 PRINT
260 PRINT
270 PRINT "AFTER SORT:"
280 FOR C = 0 TO 1
290 IF V(I(C)) > V(I(C+1)) THEN 310
300 GOTO 340
310 LET T = V(I(C))
320 LET V(I(C)) = V(I(C+1))
330 LET V(I(C+1)) = T
340 NEXT C
350 FOR C = 0 TO 7
360 PRINT V(C);" ";
370 NEXT C
380 END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|MSX Basic}}===
{{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}}===
Normally we sort with SQLite in memory. Faster and less code
<syntaxhighlight lang="runbasic">sortData$ = "7, 6, 5, 4, 3, 2, 1, 0"
sortIdx$ = "7, 2, 8"
 
numSort = 8
dim sortData(numSort)
for i = 1 to numSort
sortData(i) = val(word$(sortData$,i,","))
next i
 
while word$(sortIdx$,s + 1) <> ""
s = s + 1
idx = val(word$(sortIdx$,s))
gosub [bubbleSort]
wend
end
 
[bubbleSort]
sortSw = 1
while sortSw = 1
sortSw = 0
for i = idx to numSort - 1 ' start sorting at idx
if sortData(i) > sortData(i+1) then
sortSw = 1
sortHold = sortData(i)
sortData(i) = sortData(i+1)
sortData(i+1) = sortHold
end if
next i
wend
RETURN</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">OPTION BASE 0
 
SUB SWAP(vb1, vb2)
LET temp = vb1
LET vb1 = vb2
LET vb2 = temp
END SUB
 
DIM values(7)
DATA 7, 6, 5, 4, 3, 2, 1, 0
FOR i = 0 TO 7
READ values(i)
NEXT i
DIM indices(2)
DATA 6, 1, 7
FOR i = 0 TO 2
READ indices(i)
NEXT i
PRINT "Before sort:"
FOR i = 0 TO 7 !UBOUND(values)
PRINT values(i);
NEXT i
PRINT
PRINT
PRINT "After sort:"
FOR i = 0 TO 1
IF values(indices(i)) > values(indices(i+1)) THEN CALL SWAP (values(indices(i)), values(indices(i+1)))
NEXT i
FOR i = 0 TO 7 !UBOUND(values)
PRINT values(i);
NEXT i
 
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>
 
=={{header|Bracmat}}==
Line 953 ⟶ 1,392:
)
& out$!values;</syntaxhighlight>
{{out}}
Output:
<pre>7 0 5 4 3 2 1 6</pre>
 
Line 1,233 ⟶ 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,290 ⟶ 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,323 ⟶ 1,774:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,332 ⟶ 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,354 ⟶ 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,378 ⟶ 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,409 ⟶ 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,464 ⟶ 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,498 ⟶ 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,563 ⟶ 2,003:
end subroutine Isort
end program Example</syntaxhighlight>
{{out}}
Output
<pre> 7 0 5 4 3 2 1 6</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight 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
 
for i = 0 to 1
if value(index(i))>value(index(i+1)) then
swap value(index(i)), value(index(i+1))
end if
next i
 
for i = 0 to 7
print value(i);
next i : print</syntaxhighlight>
{{out}}<pre> 7 0 5 4 3 2 1 6</pre>
 
=={{header|Go}}==
Line 1,619 ⟶ 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 1,785 ⟶ 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 1,813 ⟶ 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 1,919 ⟶ 2,335:
===ES5===
====Iterative====
 
Does not check for duplicate indices.
<syntaxhighlight lang="javascript">function sort_disjoint(values, indices) {
Line 1,939 ⟶ 2,354:
 
====Functional====
 
<syntaxhighlight lang="javascript">(function () {
'use strict';
Line 1,967 ⟶ 2,381:
 
})();</syntaxhighlight>
 
{{Out}}
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
===ES6===
 
<syntaxhighlight lang="javascript">(() => {
'use strict';
Line 2,075 ⟶ 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,093 ⟶ 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,109 ⟶ 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,122 ⟶ 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,148 ⟶ 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,245 ⟶ 2,648:
io.write( values[i], " " )
end</syntaxhighlight>
{{out}}
<pre>7 0 5 4 3 2 1 6</pre>
 
Line 2,265 ⟶ 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,313 ⟶ 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,346 ⟶ 2,771:
sortDisjoinSublist(d, @[6, 1, 7])
echo d</syntaxhighlight>
{{out}}
Output:
<pre>@[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
Line 2,416 ⟶ 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,432 ⟶ 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,587 ⟶ 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,610 ⟶ 3,029:
disjointSort( \@values , @indices ) ;
print "[@values]\n" ;</syntaxhighlight>
{{out}}
Output:
<pre>[7 0 5 4 3 2 1 6]</pre>
 
Line 2,633 ⟶ 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,647 ⟶ 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 2,657 ⟶ 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 2,761 ⟶ 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 2,797 ⟶ 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 2,847 ⟶ 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 2,991 ⟶ 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,003 ⟶ 3,387:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
#lang racket
 
(define (sort-disjoint l is)
Line 3,015 ⟶ 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,044 ⟶ 3,426:
my @sortedValues = disjointSort( @values , @indices ) ;
say @sortedValues ;</syntaxhighlight>
{{out}}
Output:
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
Line 3,085 ⟶ 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,115 ⟶ 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,131 ⟶ 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,146 ⟶ 3,522:
indices = [6, 1, 7]
p sort_disjoint_sublist!(values, indices)</syntaxhighlight>
{{out}}
Output
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
=={{header|Run BASIC}}==
Normally we sort with SQLite in memory. Faster and less code
<syntaxhighlight lang="runbasic">sortData$ = "7, 6, 5, 4, 3, 2, 1, 0"
sortIdx$ = "7, 2, 8"
 
numSort = 8
dim sortData(numSort)
for i = 1 to numSort
sortData(i) = val(word$(sortData$,i,","))
next i
 
while word$(sortIdx$,s + 1) <> ""
s = s + 1
idx = val(word$(sortIdx$,s))
gosub [bubbleSort]
wend
end
 
[bubbleSort]
sortSw = 1
while sortSw = 1
sortSw = 0
for i = idx to numSort - 1 ' start sorting at idx
if sortData(i) > sortData(i+1) then
sortSw = 1
sortHold = sortData(i)
sortData(i) = sortData(i+1)
sortData(i+1) = sortHold
end if
next i
wend
RETURN</syntaxhighlight>
 
=={{header|Rust}}==
 
 
<syntaxhighlight lang="rust">use std::collections::BTreeSet;
 
Line 3,204 ⟶ 3,545:
disjoint_sort(&mut array, &indices);
println!("{:?}", array);
}</syntaxhighlight>
}
</syntaxhighlight>
 
{{out}}
 
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
Line 3,249 ⟶ 3,587:
car))</syntaxhighlight>
{{output}}
<pre>(7 0 5 4 3 2 1 6)</pre>
<pre>
(7 0 5 4 3 2 1 6)
</pre>
 
=={{header|Sidef}}==
Line 3,386 ⟶ 3,722:
set indices {6 1 7}
puts \[[join [disjointSort $values $indices] ", "]\]</syntaxhighlight>
{{out}}
Output:
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
=={{header|TUSCRIPT}}==
TUSCRIPT indexing is one based
<syntaxhighlight lang="tuscript">$$ MODE TUSCRIPT
$$ MODE TUSCRIPT
values="7'6'5'4'3'2'1'0"
indices="7'2'8"
Line 3,401 ⟶ 3,736:
values=REPLACE (values,#i,v)
ENDLOOP
PRINT values</syntaxhighlight>
{{out}}
</syntaxhighlight>
<pre>7'0'5'4'3'2'1'6 </pre>
Output:
<pre>
7'0'5'4'3'2'1'6
</pre>
 
=={{header|Ursala}}==
Line 3,417 ⟶ 3,749:
 
t = disjoint_sort({6,1,7},<7,6,5,4,3,2,1,0>)</syntaxhighlight>
{{out}}
output:
<pre><7,0,5,4,3,2,1,6></pre>
 
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "./sort" for Sort
 
// sorts values in place, leaves indices unsorted
Line 3,442 ⟶ 3,774:
sortDisjoint.call(values, indices)
System.print("Sorted : %(values)")</syntaxhighlight>
 
{{out}}
<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>
 
=={{header|XPL0}}==
Line 3,465 ⟶ 3,794:
[IntOut(0, Values(I)); ChOut(0, ^ )];
]</syntaxhighlight>
 
{{out}}
<pre>7 0 5 4 3 2 1 6 </pre>
<pre>
7 0 5 4 3 2 1 6
</pre>
 
=={{header|zkl}}==
Line 3,487 ⟶ 3,813:
 
values.println(); // modified list</syntaxhighlight>
{{out}}
{{out}}<pre>L(7,0,5,4,3,2,1,6)</pre>
<pre>L(7,0,5,4,3,2,1,6)</pre>
9,479

edits