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,22 +1,22 @@
import std.stdio, std.algorithm;
void combSort(T)(T[] input) {
void combSort(T)(T[] input) pure nothrow @safe @nogc {
int gap = input.length;
bool swaps = true;
while (gap > 1 || swaps) {
gap = max(1, cast(int)(gap / 1.2473));
swaps = false;
foreach (i; 0 .. input.length - gap)
foreach (immutable i; 0 .. input.length - gap)
if (input[i] > input[i + gap]) {
swap(input[i], input[i + gap]);
input[i].swap(input[i + gap]);
swaps = true;
}
}
}
void main() {
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
combSort(array);
writeln(array);
auto data = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
data.combSort;
data.writeln;
}

View file

@ -0,0 +1,44 @@
class
COMB_SORT[G -> COMPARABLE]
feature
combsort (ar: ARRAY[G]): ARRAY[G]
require
array_not_empty: ar.count >0
local
gap, i: INTEGER
swap: G
swapped: BOOLEAN
shrink: REAL_64
do
gap:= ar.count
from
until
gap= 1 and swapped = false
loop
from
i:= 1
swapped:= false
until
i+gap > ar.count
loop
if ar[i]> ar[i+gap] then
swap:= ar[i]
ar[i]:= ar[i+gap]
ar[i+gap]:= swap
swapped:= TRUE
end
i:= i+1
end
shrink:= gap/1.3
gap:= shrink.floor
if gap <1 then
gap:= 1
end
end
RESULT:= ar
ensure
RESULT_is_set: Result /= VOID
end
end

View file

@ -0,0 +1,24 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
test:= <<1,5,99,2,95, 7,-7>>
io.put_string ("unsorted"+"%N")
across test as ar loop io.put_string(ar.item.out + "%T") end
io.put_string ("%N"+"sorted:"+"%N")
create combsort
test:=combsort.combsort(test)
across test as ar loop io.put_string (ar.item.out + "%T") end
end
combsort: COMB_SORT[INTEGER]
test: ARRAY[INTEGER]
end

View file

@ -0,0 +1,28 @@
fn combSort arr =
(
local gap = arr.count
local swaps = 1
while not (gap == 1 and swaps == 0) do
(
gap = (gap / 1.25) as integer
if gap < 1 do
(
gap = 1
)
local i = 1
swaps = 0
while not (i + gap > arr.count) do
(
if arr[i] > arr[i+gap] do
(
swap arr[i] arr[i+gap]
swaps = 1
)
i += 1
)
)
return arr
)

View file

@ -0,0 +1,4 @@
a = for i in 1 to 10 collect random 1 10
#(2, 6, 5, 9, 10, 7, 2, 6, 1, 4)
combsort a
#(1, 2, 2, 4, 5, 6, 6, 7, 9, 10)

View file

@ -1,44 +1,36 @@
/*REXX program sorts an array using the comb-sort method. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call combSort highItem /*invoke the comb sort. */
call show@ ' after sort' /*show the after array elements.*/
/*REXX program sorts a stemmed array using the comb-sort algorithm. */
call gen@; w=length(#) /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call combSort # /*invoke the comb sort. */
call show@ ' after sort' /*show the after array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────COMBSORT subroutine─────────────────*/
combSort: procedure expose @.; parse arg n
s=n-1 /*S = spread between COMBs. */
do until s<=1 & done
s=trunc(s*.8) /* ÷ is slow, * is better. */
done=1
do j=1 until j+s>=n
jps=j+s
if @.j>@.jps then do; _=@.j; @.j=@.jps; @.jps=_; done=0; end
end /*j*/
end /*until*/
combSort: procedure expose @.; parse arg N /*N: is number of elements.*/
s=N-1 /*S: is the spread between COMBs.*/
do until s<=1 & done; done=1 /*assume sort is done (so far). */
s=trunc(s*.8) /* ÷ is slow, * is better.*/
do j=1 until js>=N; js=j+s
if @.j>@.js then do; _=@.j; @.j=@.js; @.js=_; done=0; end
end /*j*/
end /*until*/
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.= /*assign the default value. */
@.1 ='--- polygon sides'
@.2 ='============== ====='
@.3 ='triangle 3'
@.4 ='quadrilateral 4'
@.5 ='pentagon 5'
@.6 ='hexagon 6'
@.7 ='heptagon 7'
@.8 ='octagon 8'
@.9 ='nonagon 9'
@.10='decagon 10'
@.11='dodecagon 12'
do highItem=1 while @.highItem\=='' /*find how many entries in array.*/
end
highItem=highItem-1 /*adjust highItem slightly. */
gen@: @. = ; @.12 = 'dodecagon 12'
@.1 = '----polygon--- sides' ; @.13 = 'tridecagon 13'
@.2 = '============== =======' ; @.14 = 'tetradecagon 14'
@.3 = 'triangle 3' ; @.15 = 'pentadecagon 15'
@.4 = 'quadrilateral 4' ; @.16 = 'hexadecagon 16'
@.5 = 'pentagon 5' ; @.17 = 'heptadecagon 17'
@.6 = 'hexagon 6' ; @.18 = 'octadecagon 18'
@.7 = 'heptagon 7' ; @.19 = 'enneadecagon 19'
@.8 = 'octagon 8' ; @.20 = 'icosagon 20'
@.9 = 'nonagon 9' ; @.21 = 'hectogon 100'
@.10 = 'decagon 10' ; @.22 = 'chiliagon 1000'
@.11 = 'hendecagon 11' ; @.23 = 'myriagon 10000'
do #=1 while @.#\==''; end; #=#-1 /*find how many entries in array.*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: widthH=length(highItem) /*the maximum width of any line. */
do j=1 for highItem
say 'element' right(j,widthH) arg(1)":" @.j
end
say copies('',79) /*show a nice separator line. */
show@: say copies('',60); do j=1 for # /*display array elements.*/
say ' element' right(j,w) arg(1)":" @.j
end /*j*/
return