all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,23 @@
|
|||
declare
|
||||
proc {BubbleSort Arr}
|
||||
proc {Swap I J}
|
||||
Arr.J := (Arr.I := Arr.J) %% assignment returns the old value
|
||||
end
|
||||
IsSorted = {NewCell false}
|
||||
MaxItem = {NewCell {Array.high Arr}-1}
|
||||
in
|
||||
for until:@IsSorted do
|
||||
IsSorted := true
|
||||
for I in {Array.low Arr}..@MaxItem do
|
||||
if Arr.I > Arr.(I+1) then
|
||||
IsSorted := false
|
||||
{Swap I I+1}
|
||||
end
|
||||
end
|
||||
MaxItem := @MaxItem - 1
|
||||
end
|
||||
end
|
||||
Arr = {Tuple.toArray unit(10 9 8 7 6 5 4 3 2 1)}
|
||||
in
|
||||
{BubbleSort Arr}
|
||||
{Inspect Arr}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
declare
|
||||
local
|
||||
fun {Loop Xs Changed ?IsSorted}
|
||||
case Xs
|
||||
of X1|X2|Xr andthen X1 > X2 then
|
||||
X2|{Loop X1|Xr true IsSorted}
|
||||
[] X|Xr then
|
||||
X|{Loop Xr Changed IsSorted}
|
||||
[] nil then
|
||||
IsSorted = {Not Changed}
|
||||
nil
|
||||
end
|
||||
end
|
||||
in
|
||||
fun {BubbleSort Xs}
|
||||
IsSorted
|
||||
Result = {Loop Xs false ?IsSorted}
|
||||
in
|
||||
if IsSorted then Result
|
||||
else {BubbleSort Result}
|
||||
end
|
||||
end
|
||||
end
|
||||
in
|
||||
{Show {BubbleSort [3 1 4 1 5 9 2 6 5]}}
|
||||
Loading…
Add table
Add a link
Reference in a new issue