September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,5 +1,5 @@
function shellSort (a) {
for (var h = a.length; h = parseInt(h / 2);) {
for (var h = a.length; h > 0; h = parseInt(h / 2)) {
for (var i = h; i < a.length; i++) {
var k = a[i];
for (var j = i; j >= h && k < a[j - h]; j -= h)

View file

@ -0,0 +1,26 @@
# v0.6
function shellsort!(a::Array{Int})::Array{Int}
incr = div(length(a), 2)
while incr > 0
for i in incr+1:length(a)
j = i
tmp = a[i]
while j > incr && a[j - incr] > tmp
a[j] = a[j-incr]
j -= incr
end
a[j] = tmp
end
if incr == 2
incr = 1
else
incr = floor(Int, incr * 5.0 / 11)
end
end
return a
end
x = rand(1:10, 10)
@show x shellsort!(x)
@assert issorted(x)

View file

@ -0,0 +1,29 @@
// version 1.1.0
val gaps = listOf(701, 301, 132, 57, 23, 10, 4, 1) // Marcin Ciura's gap sequence
fun shellSort(a: IntArray) {
for (gap in gaps) {
for (i in gap until a.size) {
val temp = a[i]
var j = i
while (j >= gap && a[j - gap] > temp) {
a[j] = a[j - gap]
j -= gap
}
a[j] = temp
}
}
}
fun main(args: Array<String>) {
val aa = arrayOf(
intArrayOf(100, 2, 56, 200, -52, 3, 99, 33, 177, -199),
intArrayOf(4, 65, 2, -31, 0, 99, 2, 83, 782, 1),
intArrayOf(62, 83, 18, 53, 7, 17, 95, 86, 47, 69, 25, 28)
)
for (a in aa) {
shellSort(a)
println(a.joinToString(", "))
}
}

View file

@ -0,0 +1,77 @@
/* Rexx */
-- --- Main --------------------------------------------------------------------
call demo
return
exit
-- -----------------------------------------------------------------------------
-- Shell sort implementation
-- -----------------------------------------------------------------------------
::routine shellSort
use arg ra
n = ra~items()
inc = format(n / 2.0,, 0) -- rounding
loop label inc while inc > 0
loop i_ = inc to n - 1
temp = ra~get(i_)
j_ = i_
loop label j_ while j_ >= inc
if \(ra~get(j_ - inc) > temp) then leave j_
ra~set(j_, ra~get(j_ - inc))
j_ = j_ - inc
end j_
ra~set(j_, temp)
end i_
inc = format(inc / 2.2,, 0) -- rounding
end inc
return ra
-- -----------------------------------------------------------------------------
-- Demonstrate the implementation
-- -----------------------------------------------------------------------------
::routine demo
placesList = .nlist~of( -
"UK London", "US New York", "US Boston", "US Washington" -
, "UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" -
)
lists = .array~of( -
placesList -
, shellSort(placesList~copy()) -
)
loop ln = 1 to lists~items()
cl = lists[ln]
loop ct = 0 to cl~items() - 1
say right(ct + 1, 4)':' cl[ct]
end ct
say
end ln
return
-- -----------------------------------------------------------------------------
::routine isTrue
return 1 == 1
-- -----------------------------------------------------------------------------
::routine isFalse
return \isTrue()
-- -----------------------------------------------------------------------------
-- Helper class. Map get and set methods for easier conversion from java.util.List
-- -----------------------------------------------------------------------------
::class NList mixinclass List public
-- Map get() to at()
::method get
use arg ix
return self~at(ix)
-- Map set() to put()
::method set
use arg ix, item
self~put(item, ix)
return

View file

@ -31,18 +31,16 @@ gen: @.= /*assign a default value to ste
/*──────────────────────────────────────────────────────────────────────────────────────*/
shellSort: procedure expose @.; parse arg N /*obtain the N from the argument list*/
i=N%2 /*% is integer division in REXX. */
do while i\==0
do j=i+1 to N; k=j; p=k-i /*P: previous item*/
_=@.j
do while k>=i+1 & @.p>_; @.k=@.p
k=k-i; p=k-i
end /*while k≥i+1*/
@.k=_
end /*j*/
if i==2 then i=1
else i=i*5 % 11
end /*while i¬==0*/
do while i\==0
do j=i+1 to N; k=j; p=k-i /*P: previous item*/
_=@.j
do while k>=i+1 & @.p>_; @.k=@.p; k=k-i; p=k-i
end /*while k≥i+1*/
@.k=_
end /*j*/
if i==2 then i=1
else i=i * 5 % 11
end /*while i¬==0*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say 'element' right(j,length(#)) arg(1)": " @.j; end; return

View file

@ -0,0 +1,19 @@
// Shell sort a sequence of objects in place
// Requires mutiable list
fcn shellSort(sequence){
n := sequence.len();
gap := n / 2;
while (0 < gap){
i := gap;
while (i < n){
j := i - gap;
while ((j >= 0) and (sequence[j] > sequence[j + gap])){
sequence.swap(j,j + gap);
j -= gap;
}
i += 1;
}
gap /= 2;
}
return(sequence);
}