Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,41 @@
|
|||
PROC flip = ([]INT s, INT n) []INT:
|
||||
BEGIN
|
||||
[UPB s]INT ss := s;
|
||||
INT temp;
|
||||
FOR i TO n OVER 2 DO
|
||||
temp := ss[i];
|
||||
ss[i] := ss[n-i+1];
|
||||
ss[n-i+1] := temp
|
||||
OD;
|
||||
ss
|
||||
END;
|
||||
|
||||
PROC pancake sort = ([]INT s) []INT:
|
||||
BEGIN
|
||||
INT m;
|
||||
[UPB s]INT ss := s;
|
||||
FOR i FROM UPB s DOWNTO 2 DO
|
||||
m := 1;
|
||||
FOR j FROM 2 TO i DO
|
||||
IF ss[j] > ss[m] THEN
|
||||
m := j
|
||||
FI
|
||||
OD;
|
||||
|
||||
IF m < i THEN
|
||||
IF m > 1 THEN
|
||||
ss := flip (ss,m)
|
||||
FI;
|
||||
ss := flip (ss,i)
|
||||
FI
|
||||
OD;
|
||||
ss
|
||||
END;
|
||||
|
||||
[10]INT s;
|
||||
FOR i TO UPB s DO
|
||||
s[i] := ENTIER (next random * 100-50)
|
||||
OD;
|
||||
printf (($"Pancake sort demonstration"l$));
|
||||
printf (($"unsorted: "10(g(4) )l$, s));
|
||||
printf (($"sorted: "10(g(4) )l$, pancake sort(s)))
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
::Pancake Sort
|
||||
::Batch File Implementation
|
||||
::
|
||||
::Using the "Classic XOR trick" to swap integer values of two variables...
|
||||
::...IF the variable names are different...
|
||||
|
||||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
::Initial Values and Variables
|
||||
set "range=0"
|
||||
set "output="
|
||||
set "list=-2 0 -1 5 2 7 4 3 6 -1 7 2 1 8"
|
||||
::Put the sequence of integers (ONLY) on the list variable.
|
||||
::Please do not "play" with the list variable.
|
||||
::or else the code will not work or crash.
|
||||
|
||||
for %%l in (!list!) do (
|
||||
set num!range!=%%l
|
||||
set /a range+=1
|
||||
)
|
||||
set /a range-=1
|
||||
|
||||
::Scramble
|
||||
for /l %%l in (%range%,-1,1) do (
|
||||
set /a n=^(%random% %% %%l^)
|
||||
set /a num%%l^^^^=num!n!
|
||||
set /a num!n!^^^^=num%%l
|
||||
set /a num%%l^^^^=num!n!
|
||||
)
|
||||
::/Scramble (Remove this if you do not want to scramble the integers)
|
||||
|
||||
::Display initial condition
|
||||
for /l %%l in (0,1,%range%) do set "output=!output! !num%%l!"
|
||||
echo.Initial Sequence:
|
||||
echo.
|
||||
echo. ^>^> !output!
|
||||
echo.
|
||||
echo Sorting:
|
||||
echo.
|
||||
|
||||
::Sort begins!
|
||||
for /l %%m in (%range%,-1,1) do (
|
||||
set n=0
|
||||
for /l %%l in (1,1,%%m) do (
|
||||
set /a tmp_var1=num!n!
|
||||
if !tmp_var1! lss !num%%l! set n=%%l
|
||||
)
|
||||
if !n! lss %%m (
|
||||
if !n! gtr 0 (
|
||||
set /a tmp_var1=!n!/2
|
||||
for /l %%l in (0,1,!tmp_var1!) do (
|
||||
set /a tmp_var2=!n!-%%l
|
||||
if !tmp_var2! neq %%l (
|
||||
set /a num!tmp_var2!^^^^=num%%l
|
||||
set /a num%%l^^^^=num!tmp_var2!
|
||||
set /a num!tmp_var2!^^^^=num%%l
|
||||
)
|
||||
)
|
||||
set output=
|
||||
for /l %%x in (0,1,%range%) do set "output=!output! !num%%x!"
|
||||
echo. ^>^> !output!
|
||||
)
|
||||
set /a tmp_var3=%%m/2
|
||||
for /l %%l in (0,1,!tmp_var3!) do (
|
||||
set /a tmp_var4=%%m-%%l
|
||||
if !tmp_var4! neq %%l (
|
||||
set /a num!tmp_var4!^^^^=num%%l
|
||||
set /a num%%l^^^^=num!tmp_var4!
|
||||
set /a num!tmp_var4!^^^^=num%%l
|
||||
)
|
||||
)
|
||||
set output=
|
||||
for /l %%x in (0,1,%range%) do set "output=!output! !num%%x!"
|
||||
echo. ^>^> !output!
|
||||
)
|
||||
)
|
||||
|
||||
)
|
||||
::We are done.
|
||||
echo DONE^^!
|
||||
exit /b 0
|
||||
|
|
@ -1,87 +1,105 @@
|
|||
class
|
||||
PANCAKE_SORT
|
||||
create
|
||||
make
|
||||
feature{NONE}
|
||||
arraymax(array: ARRAY [INTEGER]; upper: INTEGER):INTEGER
|
||||
require
|
||||
upper_index_positive: upper >=0
|
||||
array_exists: array/= void
|
||||
local
|
||||
i, cur_max, index: INTEGER
|
||||
do
|
||||
from
|
||||
i:=1
|
||||
cur_max := array.item (i)
|
||||
index := i
|
||||
until
|
||||
i+1 > upper
|
||||
loop
|
||||
if array.item(i+1) > cur_max then
|
||||
cur_max := array.item(i+1)
|
||||
index := i+1
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
Result:=index
|
||||
ensure
|
||||
Index_positive: Result > 0
|
||||
end
|
||||
PANCAKE_SORT [G -> COMPARABLE]
|
||||
|
||||
reverse_array(ar:ARRAY[INTEGER]; upper:INTEGER):ARRAY[INTEGER]
|
||||
require
|
||||
upper_positive: upper >0
|
||||
ar_not_void: ar /= void
|
||||
local
|
||||
i,j:INTEGER
|
||||
new_array: ARRAY[INTEGER]
|
||||
do
|
||||
create new_array.make_empty
|
||||
new_array.copy (ar)
|
||||
feature {NONE}
|
||||
|
||||
arraymax (array: ARRAY [G]; upper: INTEGER): INTEGER
|
||||
--- Max item of 'array' between index 1 and 'upper'.
|
||||
require
|
||||
upper_index_positive: upper >= 0
|
||||
array_not_void: array /= Void
|
||||
local
|
||||
i: INTEGER
|
||||
cur_max: G
|
||||
do
|
||||
from
|
||||
i:= 1
|
||||
j:=upper
|
||||
i := 1
|
||||
cur_max := array.item (i)
|
||||
Result := i
|
||||
until
|
||||
i>j
|
||||
i + 1 > upper
|
||||
loop
|
||||
new_array[i]:=ar[j]
|
||||
new_array[j]:=ar[i]
|
||||
i:=i+1
|
||||
j:=j-1
|
||||
if array.item (i + 1) > cur_max then
|
||||
cur_max := array.item (i + 1)
|
||||
Result := i + 1
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
Result:= new_array
|
||||
ensure
|
||||
same_length: ar.count = Result.count
|
||||
end
|
||||
|
||||
sort(ar:ARRAY[INTEGER]):ARRAY[INTEGER]
|
||||
local
|
||||
i:INTEGER
|
||||
do
|
||||
my_array:=ar
|
||||
from
|
||||
i:=ar.count
|
||||
until
|
||||
i=1
|
||||
loop
|
||||
my_array:=reverse_array(reverse_array(my_array, arraymax(my_array,i)),i)
|
||||
i:=i-1
|
||||
ensure
|
||||
Index_positive: Result > 0
|
||||
end
|
||||
Result := my_array
|
||||
ensure
|
||||
same_length: ar.count= Result.count
|
||||
end
|
||||
|
||||
my_array:ARRAY[INTEGER]
|
||||
reverse_array (ar: ARRAY [G]; upper: INTEGER): ARRAY [G]
|
||||
-- Array reversed from index one to upper.
|
||||
require
|
||||
upper_positive: upper > 0
|
||||
ar_not_void: ar /= Void
|
||||
local
|
||||
i, j: INTEGER
|
||||
new_array: ARRAY [G]
|
||||
do
|
||||
create Result.make_empty
|
||||
Result.deep_copy (ar)
|
||||
from
|
||||
i := 1
|
||||
j := upper
|
||||
until
|
||||
i > j
|
||||
loop
|
||||
Result [i] := ar [j]
|
||||
Result [j] := ar [i]
|
||||
i := i + 1
|
||||
j := j - 1
|
||||
end
|
||||
ensure
|
||||
same_length: ar.count = Result.count
|
||||
end
|
||||
|
||||
sort (ar: ARRAY [G]): ARRAY [G]
|
||||
-- Sorted array in ascending order.
|
||||
local
|
||||
i: INTEGER
|
||||
do
|
||||
create Result.make_empty
|
||||
Result.deep_copy (ar)
|
||||
from
|
||||
i := ar.count
|
||||
until
|
||||
i = 1
|
||||
loop
|
||||
Result := reverse_array (reverse_array (Result, arraymax (Result, i)), i)
|
||||
i := i - 1
|
||||
end
|
||||
ensure
|
||||
same_length: ar.count = Result.count
|
||||
Result_sorted: is_sorted (Result)
|
||||
end
|
||||
|
||||
is_sorted (ar: ARRAY [G]): BOOLEAN
|
||||
--- Is 'ar' sorted in ascending order?
|
||||
require
|
||||
ar_not_empty: ar.is_empty = False
|
||||
local
|
||||
i: INTEGER
|
||||
do
|
||||
Result := True
|
||||
from
|
||||
i := ar.lower
|
||||
until
|
||||
i = ar.upper
|
||||
loop
|
||||
if ar [i] > ar [i + 1] then
|
||||
Result := False
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
end
|
||||
|
||||
feature
|
||||
make(ar:ARRAY[INTEGER])
|
||||
do
|
||||
create my_array.make_from_array(ar)
|
||||
end
|
||||
|
||||
pancake_sort:ARRAY[INTEGER]
|
||||
do
|
||||
Result:= sort(my_array)
|
||||
end
|
||||
pancake_sort (ar: ARRAY [G]): ARRAY [G]
|
||||
do
|
||||
Result := sort (ar)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,21 +1,32 @@
|
|||
class
|
||||
APPLICATION
|
||||
inherit
|
||||
ARGUMENTS
|
||||
create
|
||||
make
|
||||
feature
|
||||
make
|
||||
do
|
||||
test := <<1, 27, 32, 99, 1, -7, 3, 5>>
|
||||
create sorter.make(test)
|
||||
io.put_string ("Unsorted: ")
|
||||
across test as ar loop io.put_string (ar.item.out + " ") end
|
||||
io.put_string ("%NSorted: ")
|
||||
test:= sorter.pancake_sort
|
||||
across test as ar loop io.put_string (ar.item.out + " ") end
|
||||
end
|
||||
|
||||
test: ARRAY[INTEGER]
|
||||
sorter: PANCAKE_SORT
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
make
|
||||
do
|
||||
test := <<1, 27, 32, 99, 1, -7, 3, 5>>
|
||||
create sorter
|
||||
io.put_string ("Unsorted: ")
|
||||
across
|
||||
test as ar
|
||||
loop
|
||||
io.put_string (ar.item.out + " ")
|
||||
end
|
||||
io.put_string ("%NSorted: ")
|
||||
test := sorter.pancake_sort(test)
|
||||
across
|
||||
test as ar
|
||||
loop
|
||||
io.put_string (ar.item.out + " ")
|
||||
end
|
||||
end
|
||||
|
||||
test: ARRAY [INTEGER]
|
||||
|
||||
sorter: PANCAKE_SORT[INTEGER]
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
defmodule Sort do
|
||||
def pancake_sort(list) when is_list(list), do: pancake_sort(list, length(list))
|
||||
|
||||
defp pancake_sort(list, 0), do: list
|
||||
defp pancake_sort(list, limit) do
|
||||
index = search_max(list, limit)
|
||||
flip(list, index) |> flip(limit) |> pancake_sort(limit-1)
|
||||
end
|
||||
|
||||
defp search_max([h | t], limit), do: search_max(t, limit, 2, h, 1)
|
||||
|
||||
defp search_max(_, limit, index, _, max_index) when limit<index, do: max_index
|
||||
defp search_max([h | t], limit, index, max, max_index) do
|
||||
if h > max, do: search_max(t, limit, index+1, h, index),
|
||||
else: search_max(t, limit, index+1, max, max_index)
|
||||
end
|
||||
|
||||
defp flip(list, n), do: flip(list, n, [])
|
||||
|
||||
defp flip(list, 0, reverse), do: reverse ++ list
|
||||
defp flip([h | t], n, reverse) do
|
||||
flip(t, n-1, [h | reverse])
|
||||
end
|
||||
end
|
||||
|
||||
IO.inspect list = Enum.shuffle(1..9)
|
||||
IO.inspect Sort.pancake_sort(list)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
function pancakesort!{T<:Real}(a::Array{T,1})
|
||||
len = length(a)
|
||||
if len < 2
|
||||
return a
|
||||
end
|
||||
for i in len:-1:2
|
||||
j = indmax(a[1:i])
|
||||
i != j || continue
|
||||
a[1:j] = reverse(a[1:j])
|
||||
a[1:i] = reverse(a[1:i])
|
||||
end
|
||||
return a
|
||||
end
|
||||
|
||||
pancakesort{T<:Real}(a::Array{T,1}) = pancakesort!(copy(a))
|
||||
|
||||
println("Testing a pancake sort.")
|
||||
a = [rand(-100:100) for i in 1:20]
|
||||
println("Pre: ", a)
|
||||
b = pancakesort(a)
|
||||
println("Post: ", b)
|
||||
|
|
@ -1,38 +1,35 @@
|
|||
/*REXX program sorts & shows an array using the pancake sort algorithm.*/
|
||||
call gen@ /*generate elements in the array.*/
|
||||
call show@ 'before sort' /*show the BEFORE array elements.*/
|
||||
call pancakeSort # /*invoke the pancake sort. Yummy.*/
|
||||
call show@ ' after sort' /*show the AFTER array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────FLIP subroutine─────────────────────*/
|
||||
flip: procedure expose @.; parse arg y
|
||||
do i=1 for (y+1)%2
|
||||
ymp=y-i+1; _=@.i; @.i=@.ymp; @.ymp=_
|
||||
end /*i*/
|
||||
/*REXX program sorts and displays an array using the pancake sort algorithm.*/
|
||||
call gen /*generate elements in the @. array.*/
|
||||
call show 'before sort' /*display the BEFORE array elements.*/
|
||||
say copies('▒',40) /*display a separator line for eyeballs*/
|
||||
call pancakeSort # /*invoke the pancake sort. Yummy. */
|
||||
call show ' after sort' /*display the AFTER array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
flip: procedure expose @.; parse arg y
|
||||
do i=1 for (y+1)%2; ymp=y-i+1; _=@.i; @.i=@.ymp; @.ymp=_
|
||||
end /*i*/
|
||||
return
|
||||
/*──────────────────────────────────GEN@ subroutine──────────────────────────────────────────────────────────────────*/
|
||||
gen@: /*a few sorted bread primes which are primes of the form: (p-3)÷2 and 2∙p+3 */
|
||||
/*where p is a prime. Bread primes are related to sandwich and meat primes. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
gen: fibs= '-55 -21 -1 -8 -8 -21 -55 0 0' /*some non─positive Fibonacci numbers, most of which are repeated. */
|
||||
/* ┌───◄ a few sorted bread primes which are primes of the form: (p-3)÷2 and 2∙p+3 */
|
||||
/* ↓ where p is a prime. Bread primes are related to sandwich and meat primes.*/
|
||||
bp=2 17 5 29 7 37 13 61 43 181 47 197 67 277 97 397 113 461 137 557 167 677 173 701 797 1117 307 1237 1597 463 1861 467
|
||||
fb='-55 -21 -1 -8 -8 -21 -55 0 0' /*some non-positive Fibonacci #s,*/
|
||||
$=bp fb /* most of which are repeated.*/
|
||||
#=words($) /*get number of items in $ list. */
|
||||
/* [↓] populate @ array with #s.*/
|
||||
do j=1 for #; @.j=word($,j); end /*obtain a number of the $ list.*/
|
||||
$=bp fibs; #=words($) /*combine the two lists; get # of items*/
|
||||
/* [↓] populate the @. array with #s*/
|
||||
do j=1 for #; @.j=word($,j); end /*◄─── obtain a number from the $ list.*/
|
||||
return
|
||||
/*──────────────────────────────────PANCAKESORT subroutine──────────────*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
pancakeSort: procedure expose @.; parse arg N
|
||||
|
||||
do N=N by -1 for N-1
|
||||
!=@.1; ?=1; do j=2 to N; if @.j<=! then iterate
|
||||
!=@.j; ?=j
|
||||
!=@.1; ?=1; do j=2 to N; if @.j<=! then iterate
|
||||
!=@.j; ?=j
|
||||
end /*j*/
|
||||
call flip ?; call flip N
|
||||
call flip ?; call flip N
|
||||
end /*N*/
|
||||
return
|
||||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
|
||||
show@: w=length(#); do k=1 for #
|
||||
say ' element' right(k,w) arg(1)':' right(@.k,9)
|
||||
end /*k*/
|
||||
say copies('█',40) /*show an eyeball separator line.*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
show: w=length(#) /* [↓] display elements of @. array.*/
|
||||
do k=1 for #; say ' element' right(k,w) arg(1)':' right(@.k,9); end
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue