Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,8 +1,8 @@
import std.stdio, std.algorithm;
void pancakeSort(bool tutor=false, T)(T[] data) {
foreach_reverse (i; 2 .. data.length + 1) {
immutable maxIndex = i - data[0 .. i].minPos!q{a > b}().length;
foreach_reverse (immutable i; 2 .. data.length + 1) {
immutable maxIndex = i - data[0 .. i].minPos!q{a > b}.length;
if (maxIndex + 1 != i) {
if (maxIndex != 0) {
static if (tutor)
@ -18,7 +18,7 @@ void pancakeSort(bool tutor=false, T)(T[] data) {
}
void main() {
char[] data = "769248135".dup;
pancakeSort!true(data);
writeln(data);
auto data = "769248135".dup;
data.pancakeSort!true;
data.writeln;
}

View file

@ -0,0 +1,87 @@
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
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)
from
i:= 1
j:=upper
until
i>j
loop
new_array[i]:=ar[j]
new_array[j]:=ar[i]
i:=i+1
j:=j-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
end
Result := my_array
ensure
same_length: ar.count= Result.count
end
my_array:ARRAY[INTEGER]
feature
make(ar:ARRAY[INTEGER])
do
create my_array.make_from_array(ar)
end
pancake_sort:ARRAY[INTEGER]
do
Result:= sort(my_array)
end
end

View file

@ -0,0 +1,21 @@
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
end

View file

@ -0,0 +1,29 @@
fn flipArr arr index =
(
local new = #()
for i = index to 1 by -1 do
(
append new arr[i]
)
join new (for i in (index+1) to arr.count collect arr[i])
return new
)
fn pancakeSort arr =
(
if arr.count < 2 then return arr
else
(
for i = arr.count to 1 by -1 do
(
local newArr = for n in 1 to i collect arr[n]
local oldArr = for o in (i+1) to arr.count collect arr[o]
local maxIndices = for m in 1 to (newArr.count) where (newArr[m] == amax newArr) collect m
local lastMaxIndex = maxIndices[maxIndices.count]
newArr = flipArr newArr lastMaxIndex
newArr = flipArr newArr newArr.count
arr = join newArr oldArr
)
return arr
)
)

View file

@ -0,0 +1,4 @@
a = for i in 1 to 15 collect random 0 20
#(8, 13, 2, 0, 10, 8, 1, 15, 4, 7, 6, 9, 11, 3, 5)
pancakeSort a
#(0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 13, 15)

View file

@ -1,59 +1,38 @@
/*REXX program sorts/shows an array using the pancake sort algorithm.*/
/*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 #items /*invoke the pancake sort. Yummy.*/
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.*/
/*──────────────────────────────────PANCAKESORT subroutine──────────────*/
pancakeSort: procedure expose @.; parse arg n
do n-1 /*perform this loop N-1 times.*/
!=@.1; ?=1
do j=2 to n
if @.j<=! then iterate
!=@.j; ?=j
end /*j*/
call flip ?; call flip n
n=n-1
end /*n-1*/
return
/*──────────────────────────────────FLIP subroutine─────────────────────*/
flip: procedure expose @.; parse arg w
do i=1 for (w+1)%2 /*In REXX, % is integer divide.*/
kmip1=w-i+1; _=@.i; @.i=@.kmip1; @.kmip1=_
end /*i*/
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@: @.='' /*assign a default value. */
/*Generate some 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 & meat primes.*/
/*──────────────────────────────────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. */
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.*/
return
/*──────────────────────────────────PANCAKESORT subroutine──────────────*/
pancakeSort: procedure expose @.; parse arg N
@.1= 2 ; @.17= 113 ; @.33= -55
@.2= 17 ; @.18= 461 ; @.34= -21
@.3= 5 ; @.19= 137 ; @.35= -1
@.4= 29 ; @.20= 557 ; @.36= -8
@.5= 7 ; @.21= 167 ; @.37= -8
@.6= 37 ; @.22= 677 ; @.38= -21
@.7= 13 ; @.23= 173 ; @.39= -55
@.8= 61 ; @.24= 701 ; @.40= 0
@.9= 43 ; @.25= 797 ; @.41= 0
@.10= 181 ; @.26= 1117
@.11= 47 ; @.27= 307 /*The non-positive numbers*/
@.12= 197 ; @.28= 1237 /*above are some negative */
@.13= 67 ; @.29= 1597 /*Fibonacci numbers, some */
@.14= 277 ; @.30= 463 /*of which where specified*/
@.15= 97 ; @.31= 1861 /*twice just 'cause I felt*/
@.16= 397 ; @.32= 467 /*like it. Also, added */
/*two zeroes, just 'cause.*/
do #items=1 while @.#items\==''; end /*find how many entries in array.*/
#items=#items-1 /*adjust the #items slightly. */
do N=N by -1 for N-1
!=@.1; ?=1; do j=2 to N; if @.j<=! then iterate
!=@.j; ?=j
end /*j*/
call flip ?; call flip N
end /*N*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: widthH=length(#items) /*the maximum width of any line. */
do k=1 for #items
say 'element' right(k,widthH) arg(1)':' @.k
end /*k*/
say copies('',79) /*show an eyeball separator line.*/
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.*/
return