Sort disjoint sublist: Difference between revisions

m
(Added Yabasic)
m (→‎{{header|Wren}}: Minor tidy)
(8 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,681 ⟶ 1,730:
[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>
 
=={{header|EchoLisp}}==
Line 1,703 ⟶ 1,774:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,712 ⟶ 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 2,600 ⟶ 2,671:
{{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,970 ⟶ 3,065:
<!--</syntaxhighlight>-->
{{out}}
<pre>same output.</pre>
 
=={{header|PicoLisp}}==
Line 3,168 ⟶ 3,263:
{{out}}
<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>
{{out}}
<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,309 ⟶ 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,360 ⟶ 3,426:
my @sortedValues = disjointSort( @values , @indices ) ;
say @sortedValues ;</syntaxhighlight>
{{out}}
Output:
<pre>[7, 0, 5, 4, 3, 2, 1, 6]</pre>
 
Line 3,688 ⟶ 3,754:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "./sort" for Sort
 
// sorts values in place, leaves indices unsorted
9,488

edits