all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
3
Task/Sorting-algorithms-Shell-sort/0DESCRIPTION
Normal file
3
Task/Sorting-algorithms-Shell-sort/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{{Sorting Algorithm}}
|
||||
In this task, the goal is to sort an array of elements using the [[wp:Shell sort|Shell sort]] algorithm, a diminishing increment sort. The Shell sort is named after its inventor, Donald Shell, who published the algorithm in 1959. Shellsort is a sequence of interleaved insertion sorts based on an increment sequence. The increment size is reduced after each pass until the increment size is 1. With an increment size of 1, the sort is a basic insertion sort, but by this time the data is guaranteed to be almost sorted, which is insertion sort's "best case". Any sequence will sort the data as long as it ends in 1, but some work better than others. Empirical studies have shown a geometric increment sequence with a ratio of about 2.2 work well in practice.
|
||||
[http://www.cs.princeton.edu/~rs/shell/] Other good sequences are found at the [https://oeis.org/search?q=shell+sort On-Line Encyclopedia of Integer Sequences].
|
||||
2
Task/Sorting-algorithms-Shell-sort/1META.yaml
Normal file
2
Task/Sorting-algorithms-Shell-sort/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Sorting Algorithms
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
MODE TYPE = CHAR;
|
||||
|
||||
PROC in place shell sort = (REF[]TYPE seq)REF[]TYPE:(
|
||||
INT inc := ( UPB seq + LWB seq + 1 ) OVER 2;
|
||||
WHILE inc NE 0 DO
|
||||
FOR index FROM LWB seq TO UPB seq DO
|
||||
INT i := index;
|
||||
TYPE el = seq[i];
|
||||
WHILE ( i - LWB seq >= inc | seq[i - inc] > el | FALSE ) DO
|
||||
seq[i] := seq[i - inc];
|
||||
i -:= inc
|
||||
OD;
|
||||
seq[i] := el
|
||||
OD;
|
||||
inc := IF inc = 2 THEN 1 ELSE ENTIER(inc * 5 / 11) FI
|
||||
OD;
|
||||
seq
|
||||
);
|
||||
|
||||
PROC shell sort = ([]TYPE seq)[]TYPE:
|
||||
in place shell sort(LOC[LWB seq: UPB seq]TYPE:=seq);
|
||||
|
||||
[]TYPE char array data = "big fjords vex quick waltz nymph";
|
||||
print((shell sort(char array data), new line))
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
line[NR] = $0
|
||||
}
|
||||
END { # sort it with shell sort
|
||||
increment = int(NR / 2)
|
||||
while ( increment > 0 ) {
|
||||
for(i=increment+1; i <= NR; i++) {
|
||||
j = i
|
||||
temp = line[i]
|
||||
while ( (j >= increment+1) && (line[j-increment] > temp) ) {
|
||||
line[j] = line[j-increment]
|
||||
j -= increment
|
||||
}
|
||||
line[j] = temp
|
||||
}
|
||||
if ( increment == 2 )
|
||||
increment = 1
|
||||
else
|
||||
increment = int(increment*5/11)
|
||||
}
|
||||
#print it
|
||||
for(i=1; i <= NR; i++) {
|
||||
print line[i]
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
function shellSort(data:Array):Array
|
||||
{
|
||||
var inc:uint = data.length/2;
|
||||
while(inc > 0)
|
||||
{
|
||||
for(var i:uint = inc; i< data.length; i++)
|
||||
{
|
||||
var tmp:Object = data[i];
|
||||
for(var j:uint = i; j >= inc && data[j-inc] > tmp; j -=inc)
|
||||
{
|
||||
data[j] = data[j-inc];
|
||||
}
|
||||
data[j] = tmp;
|
||||
}
|
||||
inc = Math.round(inc/2.2);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
generic
|
||||
type Element_Type is digits <>;
|
||||
type Index_Type is (<>);
|
||||
type Array_Type is array(Index_Type range <>) of Element_Type;
|
||||
package Shell_Sort is
|
||||
procedure Sort(Item : in out Array_Type);
|
||||
end Shell_Sort;
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package body Shell_Sort is
|
||||
|
||||
----------
|
||||
-- Sort --
|
||||
----------
|
||||
|
||||
procedure Sort (Item : in out Array_Type) is
|
||||
Increment : Natural := Index_Type'Pos(Item'Last) / 2;
|
||||
J : Index_Type;
|
||||
Temp : Element_Type;
|
||||
begin
|
||||
while Increment > 0 loop
|
||||
for I in Index_Type'Val(Increment) .. Item'Last loop
|
||||
J := I;
|
||||
Temp := Item(I);
|
||||
while J > Index_Type'val(Increment) and then Item (Index_Type'Val(Index_Type'Pos(J) - Increment)) > Temp loop
|
||||
Item(J) := Item (Index_Type'Val(Index_Type'Pos(J) - Increment));
|
||||
J := Index_Type'Val(Index_Type'Pos(J) - Increment);
|
||||
end loop;
|
||||
Item(J) := Temp;
|
||||
end loop;
|
||||
if Increment = 2 then
|
||||
Increment := 1;
|
||||
else
|
||||
Increment := Increment * 5 / 11;
|
||||
end if;
|
||||
end loop;
|
||||
end Sort;
|
||||
|
||||
end Shell_Sort;
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
MsgBox % ShellSort("")
|
||||
MsgBox % ShellSort("xxx")
|
||||
MsgBox % ShellSort("3,2,1")
|
||||
MsgBox % ShellSort("dog,000000,xx,cat,pile,abcde,1,cat,zz,xx,z")
|
||||
MsgBox % ShellSort("12,11,10,9,8,4,5,6,7,3,2,1,10,13,14,15,19,17,18,16,20,10")
|
||||
|
||||
ShellSort(var) { ; SORT COMMA SEPARATED LIST
|
||||
StringSplit a, var, `, ; make array (length = a0)
|
||||
inc := a0
|
||||
While inc:=round(inc/2.2) ; geometric gap sequence
|
||||
Loop % a0-inc { ; insertion sort:
|
||||
i := A_Index+inc, t := a%i%, j := i, k := j-inc
|
||||
While j > inc && a%k% > t
|
||||
a%j% := a%k%, j := k, k -= inc
|
||||
a%j% := t
|
||||
}
|
||||
Loop % a0 ; construct string from sorted array
|
||||
s .= "," . a%A_Index%
|
||||
Return SubStr(s,2) ; drop leading comma
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
DIM test(9)
|
||||
test() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
|
||||
PROCshellsort(test(), 10)
|
||||
FOR i% = 0 TO 9
|
||||
PRINT test(i%) ;
|
||||
NEXT
|
||||
PRINT
|
||||
END
|
||||
|
||||
DEF PROCshellsort(a(), n%)
|
||||
LOCAL h%, i%, j%, k
|
||||
h% = n%
|
||||
WHILE h%
|
||||
IF h% = 2 h% = 1 ELSE h% DIV= 2.2
|
||||
FOR i% = h% TO n% - 1
|
||||
k = a(i%)
|
||||
j% = i%
|
||||
WHILE j% >= h% AND k < a(ABS(j% - h%))
|
||||
a(j%) = a(j% - h%)
|
||||
j% -= h%
|
||||
ENDWHILE
|
||||
a(j%) = k
|
||||
NEXT
|
||||
ENDWHILE
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
GET "libhdr"
|
||||
|
||||
LET shellsort(v, upb) BE
|
||||
{ LET m = 1
|
||||
UNTIL m>upb DO m := m*3 + 1 // Find first suitable value in the
|
||||
// series: 1, 4, 13, 40, 121, 364, ...
|
||||
{ m := m/3
|
||||
FOR i = m+1 TO upb DO
|
||||
{ LET vi = v!i
|
||||
LET j = i
|
||||
{ LET k = j - m
|
||||
IF k<=0 | v!k < vi BREAK
|
||||
v!j := v!k
|
||||
j := k
|
||||
} REPEAT
|
||||
v!j := vi
|
||||
}
|
||||
} REPEATUNTIL m=1
|
||||
}
|
||||
|
||||
MANIFEST { upb = 10000 }
|
||||
|
||||
LET start() = VALOF
|
||||
{ LET v = getvec(upb)
|
||||
|
||||
try("shell", shellsort, v, upb)
|
||||
|
||||
writes("*nEnd of test*n")
|
||||
freevec(v)
|
||||
RESULTIS 0
|
||||
}
|
||||
|
||||
AND try(name, sortroutine, v, upb) BE
|
||||
{ // delay, referencing the first and last elements of v
|
||||
FOR i = 1 TO 50000 DO v!upb := v!1
|
||||
writef("*nSetting %n words of data for %s sort*n", upb, name)
|
||||
FOR i = 1 TO upb DO v!i := randno(10000)
|
||||
writef("Entering %s sort routine*n", name)
|
||||
sortroutine(v, upb)
|
||||
writes("Sorting complete*n")
|
||||
TEST sorted(v, upb)
|
||||
THEN writes("The data is now sorted*n")
|
||||
ELSE writef("### ERROR: %s sort does not work*n", name)
|
||||
}
|
||||
|
||||
AND sorted(v, n) = VALOF
|
||||
{ //FOR i = 1 TO n-1 UNLESS v!i<=v!(i+1) RESULTIS FALSE
|
||||
RESULTIS TRUE
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
void shell_sort (int *a, int n) {
|
||||
int h, i, j, k;
|
||||
for (h = n; h /= 2;) {
|
||||
for (i = h; i < n; i++) {
|
||||
k = a[i];
|
||||
for (j = i; j >= h && k < a[j - h]; j -= h) {
|
||||
a[j] = a[j - h];
|
||||
}
|
||||
a[j] = k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main (int ac, char **av) {
|
||||
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
|
||||
int n = sizeof a / sizeof a[0];
|
||||
shell_sort(a, n);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
C-PROCESS SECTION.
|
||||
C-000.
|
||||
DISPLAY "SORT STARTING".
|
||||
|
||||
DIVIDE WC-SIZE BY 2 GIVING WC-GAP.
|
||||
|
||||
PERFORM E-PROCESS-GAP UNTIL WC-GAP = 0.
|
||||
|
||||
DISPLAY "SORT FINISHED".
|
||||
|
||||
C-999.
|
||||
EXIT.
|
||||
|
||||
|
||||
E-PROCESS-GAP SECTION.
|
||||
E-000.
|
||||
PERFORM F-SELECTION VARYING WB-IX-1 FROM WC-GAP BY 1
|
||||
UNTIL WB-IX-1 > WC-SIZE.
|
||||
|
||||
DIVIDE WC-GAP BY 2.2 GIVING WC-GAP.
|
||||
|
||||
E-999.
|
||||
EXIT.
|
||||
|
||||
F-SELECTION SECTION.
|
||||
F-000.
|
||||
SET WB-IX-2 TO WB-IX-1.
|
||||
MOVE WB-ENTRY(WB-IX-1) TO WC-TEMP.
|
||||
|
||||
SET WB-IX-3 TO WB-IX-2.
|
||||
SET WB-IX-3 DOWN BY WC-GAP.
|
||||
PERFORM G-PASS UNTIL WB-IX-2 NOT > WC-GAP
|
||||
* The next line logically reads :
|
||||
* or wb-entry(wb-ix-2 - wc-gap) not > wc-temp.
|
||||
OR WB-ENTRY(WB-IX-3) NOT > WC-TEMP.
|
||||
|
||||
IF WB-IX-1 NOT = WB-IX-2
|
||||
MOVE WC-TEMP TO WB-ENTRY(WB-IX-2).
|
||||
|
||||
F-999.
|
||||
EXIT.
|
||||
|
||||
G-PASS SECTION.
|
||||
* Note that WB-IX-3 is WC-GAP less than WB-IX-2.
|
||||
* Logically this should be :
|
||||
* move wb-entry(wb-ix-2 - wc-gap) to wb-entry(wb-ix-2).
|
||||
* set wb-ix-2 down by wc-gap.
|
||||
* Unfortunately wb-entry(wb-ix-2 - wc-gap) is not legal in C2 cobol
|
||||
G-000.
|
||||
MOVE WB-ENTRY(WB-IX-3) TO WB-ENTRY(WB-IX-2).
|
||||
SET WB-IX-2 DOWN BY WC-GAP.
|
||||
SET WB-IX-3 DOWN BY WC-GAP.
|
||||
|
||||
G-999.
|
||||
EXIT.
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
(defun gap-insertion-sort (array predicate gap)
|
||||
(let ((length (length array)))
|
||||
(if (< length 2) array
|
||||
(do ((i 1 (1+ i))) ((eql i length) array)
|
||||
(do ((x (aref array i))
|
||||
(j i (- j gap)))
|
||||
((or (< (- j gap) 0)
|
||||
(not (funcall predicate x (aref array (1- j)))))
|
||||
(setf (aref array j) x))
|
||||
(setf (aref array j) (aref array (- j gap))))))))
|
||||
|
||||
(defconstant +gaps+
|
||||
'(1750 701 301 132 57 23 10 4 1)
|
||||
"The best sequence of gaps, according to Marcin Ciura.")
|
||||
|
||||
(defun shell-sort (array predicate &optional (gaps +gaps+))
|
||||
(assert (eql 1 (car (last gaps))) (gaps)
|
||||
"Last gap of ~w is not 1." gaps)
|
||||
(dolist (gap gaps array)
|
||||
(gap-insertion-sort array predicate gap)))
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
import std.stdio: writeln;
|
||||
|
||||
void shellSort(T)(T[] seq) pure nothrow {
|
||||
int inc = seq.length / 2;
|
||||
while (inc) {
|
||||
foreach (ref i, el; seq) {
|
||||
while (i >= inc && seq[i - inc] > el) {
|
||||
seq[i] = seq[i - inc];
|
||||
i -= inc;
|
||||
}
|
||||
seq[i] = el;
|
||||
}
|
||||
inc = (inc == 2) ? 1 : cast(int)(inc * 5.0 / 11);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto data = [22, 7, 2, -5, 8, 4];
|
||||
shellSort(data);
|
||||
writeln(data);
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
/** Shell sort (in-place) */
|
||||
def shellSort(array) {
|
||||
var inc := array.size() // 2
|
||||
while (inc.aboveZero()) {
|
||||
for var i => a in array {
|
||||
while (i >= inc && (def b := array[i - inc]) > a) {
|
||||
array[i] := b
|
||||
i -= inc
|
||||
}
|
||||
array[i] := a
|
||||
}
|
||||
inc := if (inc <=> 2) { 1 } else { (inc * 5.0 / 11).floor() }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
class
|
||||
MY_SORTED_SET [G -> COMPARABLE]
|
||||
inherit
|
||||
TWO_WAY_SORTED_SET [G]
|
||||
redefine
|
||||
sort
|
||||
end
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
sort
|
||||
-- Shell sort
|
||||
local
|
||||
inc: INTEGER
|
||||
j: INTEGER
|
||||
l_value: like item
|
||||
do
|
||||
from
|
||||
inc := (count.to_double / 2.0).rounded
|
||||
until
|
||||
inc <= 0
|
||||
loop
|
||||
across inc |..| (count - 1) as ii
|
||||
loop
|
||||
l_value := Current [ii.item + 1]
|
||||
from
|
||||
j := ii.item
|
||||
until
|
||||
j < inc or Current [j - inc + 1] <= l_value
|
||||
loop
|
||||
Current [j + 1] := Current [j - inc + 1]
|
||||
j := j - inc
|
||||
end
|
||||
Current [j + 1] := l_value
|
||||
end
|
||||
inc := (inc.to_double / 2.2).rounded
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
function shell_sort(sequence s)
|
||||
integer gap,j
|
||||
object temp
|
||||
gap = floor(length(s)/2)
|
||||
while gap > 0 do
|
||||
for i = gap to length(s) do
|
||||
temp = s[i]
|
||||
j=i-gap
|
||||
while j >= 1 and compare(temp, s[j]) <= 0 do
|
||||
s[j+gap]=s[j]
|
||||
j -= gap
|
||||
end while
|
||||
s[j+gap] = temp
|
||||
end for
|
||||
gap = floor(gap/2)
|
||||
end while
|
||||
return s
|
||||
end function
|
||||
|
||||
constant s = rand(repeat(1000,10))
|
||||
puts(1,"Before: ")
|
||||
? s
|
||||
puts(1,"After: ")
|
||||
? shell_sort(s)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
defer less? ' < is less?
|
||||
|
||||
: shell { array len -- }
|
||||
1 begin dup len u<= while 2* 1+ repeat { gap }
|
||||
begin gap 5 11 */ dup to gap while
|
||||
len gap do
|
||||
array i cells +
|
||||
dup @ swap ( temp last )
|
||||
begin gap cells -
|
||||
array over u<=
|
||||
while 2dup @ less?
|
||||
while dup gap cells + over @ swap !
|
||||
repeat then
|
||||
gap cells + !
|
||||
loop
|
||||
repeat ;
|
||||
|
||||
create array 8 , 1 , 4 , 2 , 10 , 3 , 7 , 9 , 6 , 5 ,
|
||||
|
||||
array 10 shell
|
||||
array 10 cells dump
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
defer precedes ' < is precedes
|
||||
|
||||
: (shell) ( a n h -- a n h)
|
||||
over >r tuck ( a h n h)
|
||||
?do ( a h)
|
||||
i swap >r ( a j R: h)
|
||||
2dup cells + @ -rot ( k a j R: h)
|
||||
begin ( k a j R: h)
|
||||
dup r@ - dup >r 0< 0= ( k a j f R: h j-h)
|
||||
while ( k a j R: h j-h)
|
||||
-rot over over r@ cells + @ ( j k a k v R: h j-h)
|
||||
precedes >r rot r> ( k a j f R: h j-h)
|
||||
while ( k a j R: h j-h)
|
||||
over r@ cells + @ >r ( k a j R: h j-h a[j-h])
|
||||
2dup cells + r> swap ! ( k a j R: h j-h)
|
||||
drop r> ( k a j' R: h)
|
||||
repeat then ( k a j R: h j-h)
|
||||
rot >r 2dup cells + ( a j a[j] R: h j-h k)
|
||||
r> swap ! r> drop drop r> ( a h)
|
||||
loop r> swap
|
||||
;
|
||||
( a n --)
|
||||
: shell dup begin 5 11 */ dup while (shell) repeat drop 2drop ;
|
||||
|
||||
create array 8 , 1 , 4 , 2 , 10 , 3 , 7 , 9 , 6 , 5 ,
|
||||
|
||||
array 10 shell
|
||||
array 10 cells dump
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
MODULE sort
|
||||
|
||||
CONTAINS
|
||||
|
||||
SUBROUTINE Shell_Sort(a)
|
||||
|
||||
IMPLICIT NONE
|
||||
INTEGER :: i, j, increment
|
||||
REAL :: temp
|
||||
REAL, INTENT(in out) :: a(:)
|
||||
|
||||
increment = SIZE(a) / 2
|
||||
DO WHILE (increment > 0)
|
||||
DO i = increment+1, SIZE(a)
|
||||
j = i
|
||||
temp = a(i)
|
||||
DO WHILE (j >= increment+1 .AND. a(j-increment) > temp)
|
||||
a(j) = a(j-increment)
|
||||
j = j - increment
|
||||
END DO
|
||||
a(j) = temp
|
||||
END DO
|
||||
IF (increment == 2) THEN
|
||||
increment = 1
|
||||
ELSE
|
||||
increment = increment * 5 / 11
|
||||
END IF
|
||||
END DO
|
||||
|
||||
END SUBROUTINE Shell_Sort
|
||||
|
||||
END MODULE sort
|
||||
|
||||
PROGRAM Shellsort
|
||||
|
||||
USE sort
|
||||
|
||||
IMPLICIT NONE
|
||||
REAL :: array(1000)
|
||||
|
||||
CALL RANDOM_SEED
|
||||
CALL RANDOM_NUMBER(array)
|
||||
|
||||
WRITE (*,*) "Unsorted array"
|
||||
WRITE (*,*) array
|
||||
WRITE (*,*)
|
||||
CALL Shell_Sort(array)
|
||||
WRITE (*,*) "Sorted array"
|
||||
WRITE (*,*) array
|
||||
|
||||
END PROGRAM Shellsort
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var a = []int{170, 45, 75, -90, -802, 24, 2, 66}
|
||||
|
||||
func main() {
|
||||
fmt.Println("before:", a)
|
||||
for inc := len(a) / 2; inc > 0; inc = (inc + 1) * 5 / 11 {
|
||||
for i := inc; i < len(a); i++ {
|
||||
j, temp := i, a[i]
|
||||
for ; j >= inc && a[j-inc] > temp; j -= inc {
|
||||
a[j] = a[j-inc]
|
||||
}
|
||||
a[j] = temp
|
||||
}
|
||||
}
|
||||
fmt.Println("after: ", a)
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import Data.List
|
||||
|
||||
shellSort xs = foldr (invColumnize (map (foldr insert []))) xs gaps
|
||||
where gaps = takeWhile (< length xs) $ iterate (succ.(3*)) 1
|
||||
invColumnize f k = concat. transpose. f. transpose
|
||||
. takeWhile (not.null). unfoldr (Just. splitAt k)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
procedure main() #: demonstrate various ways to sort a list and string
|
||||
demosort(shellsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
|
||||
end
|
||||
|
||||
procedure shellsort(X,op) #: return sorted X
|
||||
local i,j,inc,temp
|
||||
|
||||
op := sortop(op,X) # select how and what we sort
|
||||
|
||||
inc := *X/2
|
||||
while inc > 0 do {
|
||||
every i := inc to *X do {
|
||||
temp := X[j := i]
|
||||
while op(temp,X[j - (j >= inc)]) do
|
||||
X[j] := X[j -:= inc]
|
||||
X[j] := temp
|
||||
}
|
||||
inc := if inc = 2 then 1 else inc*5/11 # switch to insertion near the end
|
||||
}
|
||||
return X
|
||||
end
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
List do(
|
||||
shellSortInPlace := method(
|
||||
gap := (size / 2) round
|
||||
while(gap > 0,
|
||||
for(i, gap, size - 1,
|
||||
key := at(i)
|
||||
j := i
|
||||
|
||||
while(j >= gap and at(j - gap) > key,
|
||||
atPut(j, at(j - gap))
|
||||
j = j - gap
|
||||
)
|
||||
atPut(j, key)
|
||||
)
|
||||
gap = (gap / 2.2) round
|
||||
)
|
||||
self)
|
||||
)
|
||||
|
||||
l := list(2, 3, 4, 5, 1)
|
||||
l shellSortInPlace println # ==> list(1, 2, 3, 4, 5)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
gaps =: [: }: 1 (1+3*])^:(> {:)^:a:~ #
|
||||
insert =: (I.~ {. ]) , [ , ] }.~ I.~
|
||||
gapinss =: #@] {. ,@|:@(] insert//.~ #@] $ i.@[)
|
||||
shellSort =: [: ; gapinss &.>/@(< ,~ ]&.>@gaps)
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
public static void shell(int[] a) {
|
||||
int increment = a.length / 2;
|
||||
while (increment > 0) {
|
||||
for (int i = increment; i < a.length; i++) {
|
||||
int j = i;
|
||||
int temp = a[i];
|
||||
while (j >= increment && a[j - increment] > temp) {
|
||||
a[j] = a[j - increment];
|
||||
j = j - increment;
|
||||
}
|
||||
a[j] = temp;
|
||||
}
|
||||
if (increment == 2) {
|
||||
increment = 1;
|
||||
} else {
|
||||
increment *= (5.0 / 11);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
function shellSort (a) {
|
||||
for (var h = a.length; 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)
|
||||
a[j] = a[j - h];
|
||||
a[j] = k;
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
var a = [];
|
||||
var n = location.href.match(/\?(\d+)|$/)[1] || 10;
|
||||
for (var i = 0; i < n; i++)
|
||||
a.push(parseInt(Math.random() * 100));
|
||||
shellSort(a);
|
||||
document.write(a.join(" "));
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
Section Header
|
||||
|
||||
+ name := SHELL_SORT;
|
||||
|
||||
- external := `#include <time.h>`;
|
||||
|
||||
Section Public
|
||||
|
||||
- main <- (
|
||||
+ a : ARRAY[INTEGER];
|
||||
|
||||
a := ARRAY[INTEGER].create 0 to 100;
|
||||
`srand(time(NULL))`;
|
||||
0.to 100 do { i : INTEGER;
|
||||
a.put `rand()`:INTEGER to i;
|
||||
};
|
||||
|
||||
shell a;
|
||||
|
||||
a.foreach { item : INTEGER;
|
||||
item.print;
|
||||
'\n'.print;
|
||||
};
|
||||
);
|
||||
|
||||
- shell a : ARRAY[INTEGER] <- (
|
||||
+ lower, length, increment, temp : INTEGER;
|
||||
|
||||
lower := a.lower;
|
||||
length := a.upper - lower + 1;
|
||||
increment := length;
|
||||
{
|
||||
increment := increment / 2;
|
||||
increment > 0
|
||||
}.while_do {
|
||||
increment.to (length - 1) do { i : INTEGER; + j : INTEGER;
|
||||
temp := a.item(lower + i);
|
||||
j := i - increment;
|
||||
{ (j >= 0) && { a.item(lower + j) > temp } }.while_do {
|
||||
a.put (a.item(lower + j)) to (lower + j + increment);
|
||||
j := j - increment;
|
||||
};
|
||||
a.put temp to (lower + j + increment);
|
||||
};
|
||||
};
|
||||
);
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
function shellsort( a )
|
||||
local inc = math.ceil( #a / 2 )
|
||||
while inc > 0 do
|
||||
for i = inc, #a do
|
||||
local tmp = a[i]
|
||||
local j = i
|
||||
while j > inc and a[j-inc] > tmp do
|
||||
a[j] = a[j-inc]
|
||||
j = j - inc
|
||||
end
|
||||
a[j] = tmp
|
||||
end
|
||||
inc = math.floor( 0.5 + inc / 2.2 )
|
||||
end
|
||||
|
||||
return a
|
||||
end
|
||||
|
||||
a = { -12, 3, 0, 4, 7, 4, 8, -5, 9 }
|
||||
a = shellsort( a )
|
||||
|
||||
for _, i in pairs(a) do
|
||||
print(i)
|
||||
end
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
function list = shellSort(list)
|
||||
|
||||
N = numel(list);
|
||||
increment = round(N/2);
|
||||
|
||||
while increment > 0
|
||||
|
||||
for i = (increment+1:N)
|
||||
temp = list(i);
|
||||
j = i;
|
||||
while (j >= increment+1) && (list(j-increment) > temp)
|
||||
list(j) = list(j-increment);
|
||||
j = j - increment;
|
||||
end
|
||||
|
||||
list(j) = temp;
|
||||
|
||||
end %for
|
||||
|
||||
if increment == 2 %This case causes shell sort to become insertion sort
|
||||
increment = 1;
|
||||
else
|
||||
increment = round(increment/2.2);
|
||||
end
|
||||
end %while
|
||||
end %shellSort
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>> shellSort([4 3 1 5 6 2])
|
||||
|
||||
ans =
|
||||
|
||||
1 2 3 4 5 6
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
shellSort[ lst_ ] := Module[ {list = lst, incr, temp, i, j},
|
||||
incr = Round[Length[list]/2];
|
||||
While[incr > 0,
|
||||
|
||||
For[i = incr + 1, i < Length[list], i++,
|
||||
|
||||
temp = list[[i]]; j = i;
|
||||
|
||||
While[(j >= (incr + 1)) && (list[[j - incr]] > temp) ,
|
||||
list[[j]] = list[[j - incr]]; j = j-incr;
|
||||
];
|
||||
|
||||
list[[j]] = temp;];
|
||||
If[incr == 2, incr = 1, incr = Round[incr/2.2]]
|
||||
]; list
|
||||
]
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols binary
|
||||
|
||||
placesList = [String -
|
||||
"UK London", "US New York", "US Boston", "US Washington" -
|
||||
, "UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" -
|
||||
]
|
||||
sortedList = shellSort(String[] Arrays.copyOf(placesList, placesList.length))
|
||||
|
||||
lists = [placesList, sortedList]
|
||||
loop ln = 0 to lists.length - 1
|
||||
cl = lists[ln]
|
||||
loop ct = 0 to cl.length - 1
|
||||
say cl[ct]
|
||||
end ct
|
||||
say
|
||||
end ln
|
||||
|
||||
return
|
||||
|
||||
method shellSort(a = String[]) public constant binary returns String[]
|
||||
|
||||
n = a.length
|
||||
inc = int Math.round(double n / 2.0)
|
||||
loop label inc while inc > 0
|
||||
loop i_ = inc to n - 1
|
||||
temp = a[i_]
|
||||
j_ = i_
|
||||
loop label j_ while j_ >= inc
|
||||
if \(a[j_ - inc].compareTo(temp) > 0) then leave j_
|
||||
a[j_] = a[j_ - inc]
|
||||
j_ = j_ - inc
|
||||
end j_
|
||||
a[j_] = temp
|
||||
end i_
|
||||
inc = int Math.round(double inc / 2.2)
|
||||
end inc
|
||||
|
||||
return a
|
||||
|
||||
method isTrue public constant binary returns boolean
|
||||
return 1 == 1
|
||||
|
||||
method isFalse public constant binary returns boolean
|
||||
return \isTrue
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
let shellsort a =
|
||||
let len = Array.length a in
|
||||
let incSequence = [| 412771; 165103; 66041; 26417; 10567;
|
||||
4231; 1693; 673; 269; 107; 43; 17; 7; 3; 1 |] in
|
||||
|
||||
Array.iter (fun increment ->
|
||||
if (increment * 2) <= len then
|
||||
for i = increment to pred len do
|
||||
let temp = a.(i) in
|
||||
let rec loop j =
|
||||
if j < 0 || a.(j) <= temp then (j)
|
||||
else begin
|
||||
a.(j + increment) <- a.(j);
|
||||
loop (j - increment)
|
||||
end
|
||||
in
|
||||
let j = loop (i - increment) in
|
||||
a.(j + increment) <- temp;
|
||||
done;
|
||||
) incSequence;
|
||||
;;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
let () =
|
||||
let arraysize = 1000 in (* or whatever *)
|
||||
Random.self_init();
|
||||
let intArray =
|
||||
Array.init arraysize (fun _ -> Random.int 4000)
|
||||
in
|
||||
shellsort intArray;
|
||||
Array.iter (Printf.printf " %d") intArray;
|
||||
print_newline();
|
||||
;;
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
bundle Default {
|
||||
class ShellSort {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
a := [1, 3, 7, 21, 48, 112,336, 861, 1968, 4592, 13776,33936, 86961, 198768, 463792, 1391376,3402672, 8382192, 21479367, 49095696, 114556624,343669872, 52913488, 2085837936];
|
||||
Shell(a);
|
||||
each(i : a) {
|
||||
IO.Console->Print(a[i])->Print(", ");
|
||||
};
|
||||
IO.Console->PrintLine();
|
||||
}
|
||||
|
||||
function : native : Shell(a : Int[]) ~ Nil {
|
||||
increment := a->Size() / 2;
|
||||
while(increment > 0) {
|
||||
for(i := increment; i < a->Size(); i += 1;) {
|
||||
j := i;
|
||||
temp := a[i];
|
||||
while(j >= increment & a[j - increment] > temp) {
|
||||
a[j] := a[j - increment];
|
||||
j -= increment;
|
||||
};
|
||||
a[j] := temp;
|
||||
};
|
||||
|
||||
if(increment = 2) {
|
||||
increment := 1;
|
||||
}
|
||||
else {
|
||||
increment *= (5.0 / 11);
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
shellSort(v)={
|
||||
my(inc=#v\2);
|
||||
while(inc,
|
||||
for(i=inc+1,#v,
|
||||
my(t=v[i],j=i);
|
||||
while(j>inc && v[j-inc]>t,
|
||||
v[j]=v[j-=inc]
|
||||
);
|
||||
v[j]=t
|
||||
);
|
||||
inc \= 2.2
|
||||
);
|
||||
v
|
||||
};
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
function shellSort($arr)
|
||||
{
|
||||
$inc = round(count($arr)/2);
|
||||
while($inc > 0)
|
||||
{
|
||||
for($i = $inc; $i < count($arr);$i++){
|
||||
$temp = $arr[$i];
|
||||
$j = $i;
|
||||
while($j >= $inc && $arr[$j-$inc] > $temp)
|
||||
{
|
||||
$arr[$j] = $arr[$j - $inc];
|
||||
$j -= $inc;
|
||||
}
|
||||
$arr[$j] = $temp;
|
||||
}
|
||||
$inc = round($inc/2.2);
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
/* Based on Rosetta Fortran */
|
||||
Shell_Sort: PROCEDURE (A);
|
||||
DECLARE A(*) FIXED;
|
||||
DECLARE ( i, j, increment) FIXED BINARY (31);
|
||||
DECLARE temp FIXED;
|
||||
|
||||
increment = DIMENSION(a) / 2;
|
||||
DO WHILE (increment > 0);
|
||||
DO i = lbound(A,1)+increment TO hbound(a,1);
|
||||
j = i;
|
||||
temp = a(i);
|
||||
DO WHILE (j >= increment+1 & a(j-increment) > temp);
|
||||
a(j) = a(j-increment);
|
||||
j = j - increment;
|
||||
END;
|
||||
a(j) = temp;
|
||||
END;
|
||||
IF increment = 2 THEN
|
||||
increment = 1;
|
||||
ELSE
|
||||
increment = increment * 5 / 11;
|
||||
END;
|
||||
END SHELL_SORT;
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
Const
|
||||
MaxN = 100; { number of elements (my example is 100) }
|
||||
Type
|
||||
TArray = Array [0..MaxN] of Integer;
|
||||
|
||||
Procedure ShellSort ( var A : TArray; N : Integer );
|
||||
Var
|
||||
i, j, step, tmp : Integer;
|
||||
Begin
|
||||
step:=N div 2; // step:=step shr 1
|
||||
While step>0 Do Begin
|
||||
For i:=step to N Do Begin
|
||||
tmp:=A[i];
|
||||
j:=i;
|
||||
While (j>=step) and (A[j-step]>tmp) Do Begin
|
||||
A[j]:=A[j-step];
|
||||
dec(j,step);
|
||||
End;
|
||||
A[j]:=tmp;
|
||||
End;
|
||||
step:=step div 2; // step:=step shr 1
|
||||
End;
|
||||
End;
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
sub shell_sort ( @a is copy ) {
|
||||
loop ( my $gap = (@a/2).round; $gap > 0; $gap = ( $gap * 5 / 11 ).round ) {
|
||||
for $gap .. @a.end -> $i {
|
||||
my $temp = @a[$i];
|
||||
|
||||
my $j;
|
||||
loop ( $j = $i; $j >= $gap; $j -= $gap ) {
|
||||
my $v = @a[$j - $gap];
|
||||
last if $v <= $temp;
|
||||
@a[$j] = $v;
|
||||
}
|
||||
|
||||
@a[$j] = $temp;
|
||||
}
|
||||
}
|
||||
return @a;
|
||||
}
|
||||
my @data = 22, 7, 2, -5, 8, 4;
|
||||
say 'input = ' ~ @data;
|
||||
say 'output = ' ~ @data.&shell_sort;
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
sub shell_sort {
|
||||
my (@a, $h, $i, $j, $k) = @_;
|
||||
for ($h = @a; $h = int $h / 2;) {
|
||||
for $i ($h .. $#a) {
|
||||
$k = $a[$i];
|
||||
for ($j = $i; $j >= $h && $k < $a[$j - $h]; $j -= $h) {
|
||||
$a[$j] = $a[$j - $h];
|
||||
}
|
||||
$a[$j] = $k;
|
||||
}
|
||||
}
|
||||
@a;
|
||||
}
|
||||
|
||||
my @a = map int rand 100, 1 .. $ARGV[0] || 10;
|
||||
say "@a";
|
||||
@a = shell_sort @a;
|
||||
say "@a";
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
(de shellSort (A)
|
||||
(for (Inc (*/ (length A) 2) (gt0 Inc) (*/ Inc 10 22))
|
||||
(for (I Inc (get A I) (inc I))
|
||||
(let (Tmp @ J I)
|
||||
(while (and (>= J Inc) (> (get A (- J Inc)) Tmp))
|
||||
(set (nth A J) (get A (- J Inc)))
|
||||
(dec 'J Inc) )
|
||||
(set (nth A J) Tmp) ) ) )
|
||||
A )
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
Function ShellSort( [Array] $data )
|
||||
{
|
||||
# http://oeis.org/A108870
|
||||
$A108870 = [Int64[]] ( 1, 4, 9, 20, 46, 103, 233, 525, 1182, 2660, 5985, 13467, 30301, 68178, 153401, 345152, 776591, 1747331, 3931496, 8845866, 19903198, 44782196, 100759940, 226709866, 510097200, 1147718700, 2582367076, 5810325920, 13073233321, 29414774973 )
|
||||
$datal = $data.length - 1
|
||||
$inci = [Array]::BinarySearch( $A108870, [Int64] ( [Math]::Floor( $datal / 2 ) ) )
|
||||
if( $inci -lt 0 )
|
||||
{
|
||||
$inci = ( $inci -bxor -1 ) - 1
|
||||
}
|
||||
$A108870[ $inci..0 ] | ForEach-Object {
|
||||
$inc = $_
|
||||
$_..$datal | ForEach-Object {
|
||||
$temp = $data[ $_ ]
|
||||
$j = $_
|
||||
for( ; ( $j -ge $inc ) -and ( $data[ $j - $inc ] -gt $temp ); $j -= $inc )
|
||||
{
|
||||
$data[ $j ] = $data[ $j - $inc ]
|
||||
}
|
||||
$data[ $j ] = $temp
|
||||
}
|
||||
}
|
||||
$data
|
||||
}
|
||||
|
||||
$l = 10000; ShellSort( ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( 0, $l - 1 ) } ) )
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#STEP=2.2
|
||||
|
||||
Procedure Shell_sort(Array A(1))
|
||||
Protected l=ArraySize(A()), increment=Int(l/#STEP)
|
||||
Protected i, j, temp
|
||||
While increment
|
||||
For i= increment To l
|
||||
j=i
|
||||
temp=A(i)
|
||||
While j>=increment And A(j-increment)>temp
|
||||
A(j)=A(j-increment)
|
||||
j-increment
|
||||
Wend
|
||||
A(j)=temp
|
||||
Next i
|
||||
If increment=2
|
||||
increment=1
|
||||
Else
|
||||
increment*(5.0/11)
|
||||
EndIf
|
||||
Wend
|
||||
EndProcedure
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
def shell(seq):
|
||||
inc = len(seq) // 2
|
||||
while inc:
|
||||
for i, el in enumerate(seq):
|
||||
while i >= inc and seq[i - inc] > el:
|
||||
seq[i] = seq[i - inc]
|
||||
i -= inc
|
||||
seq[i] = el
|
||||
inc = 1 if inc == 2 else int(inc * 5.0 / 11)
|
||||
|
||||
data = [22, 7, 2, -5, 8, 4]
|
||||
shell(data)
|
||||
print data # [-5, 2, 4, 7, 8, 22]
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
/*REXX program sorts an (stemmed) array using the shellsort method. */
|
||||
call gen@ /*generate the array elements. */
|
||||
call show@ 'before sort' /*show the before array elements.*/
|
||||
call shellSort highItem /*invoke the shell sort. */
|
||||
call show@ ' after sort' /*show the after array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
|
||||
gen@: @.= /*assign default value to stem. */
|
||||
@.1='3 character abbreviations for states of the USA' /*predates ZIP.*/
|
||||
@.2='==============================================='
|
||||
@.3='RHO Rhode Island and Providence Plantations' ; @.36='NMX New Mexico'
|
||||
@.4='CAL California' ; @.20='NEV Nevada' ; @.37='IND Indiana'
|
||||
@.5='KAN Kansas' ; @.21='TEX Texas' ; @.38='MOE Missouri'
|
||||
@.6='MAS Massachusetts' ; @.22='VGI Virginia' ; @.39='COL Colorado'
|
||||
@.7='WAS Washington' ; @.23='OHI Ohio' ; @.40='CON Connecticut'
|
||||
@.8='HAW Hawaii' ; @.24='NHM New Hampshire'; @.41='MON Montana'
|
||||
@.9='NCR North Carolina'; @.25='MAE Maine' ; @.42='LOU Louisiana'
|
||||
@.10='SCR South Carolina'; @.26='MIC Michigan' ; @.43='IOW Iowa'
|
||||
@.11='IDA Idaho' ; @.27='MIN Minnesota' ; @.44='ORE Oregon'
|
||||
@.12='NDK North Dakota' ; @.28='MIS Mississippi' ; @.45='ARK Arkansas'
|
||||
@.13='SDK South Dakota' ; @.29='WIS Wisconsin' ; @.46='ARZ Arizona'
|
||||
@.14='NEB Nebraska' ; @.30='OKA Oklahoma' ; @.47='UTH Utah'
|
||||
@.15='DEL Delaware' ; @.31='ALA Alabama' ; @.48='KTY Kentucky'
|
||||
@.16='PEN Pennsylvania' ; @.32='FLA Florida' ; @.49='WVG West Virginia'
|
||||
@.17='TEN Tennessee' ; @.33='MLD Maryland' ; @.50='NWJ New Jersey'
|
||||
@.18='GEO Georgia' ; @.34='ALK Alaska' ; @.51='NYK New York'
|
||||
@.19='VER Vermont' ; @.35='ILL Illinois' ; @.52='WYO Wyoming'
|
||||
|
||||
do highItem=1 while @.highItem\=='' /*find how many entries in array.*/
|
||||
end
|
||||
|
||||
highItem=highItem-1 /*adjust the highItem slightly.*/
|
||||
return
|
||||
/*──────────────────────────────────SHELLSORT subroutine───────---──────*/
|
||||
shellSort: procedure expose @.; parse arg highItem
|
||||
i=highItem%2
|
||||
do while i\==0
|
||||
do j=i+1 to highItem; k=j; kmi=k-i
|
||||
_=@.j
|
||||
do while k>=i+1 & @.kmi>_; @.k=@.kmi
|
||||
k=k-i; kmi=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@ subroutine────────────────────*/
|
||||
show@: widthH=length(highItem) /*maximum width of any line. */
|
||||
do j=1 for highItem
|
||||
say 'element' right(j,widthH) arg(1)': ' @.j
|
||||
end /*j*/
|
||||
say copies('─',79) /*show a separator line (a fence)*/
|
||||
return
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
class Array
|
||||
def shellsort!
|
||||
inc = length / 2
|
||||
while inc != 0
|
||||
inc.step(length-1) do |i|
|
||||
el = self[i]
|
||||
while i >= inc and self[i - inc] > el
|
||||
self[i] = self[i - inc]
|
||||
i -= inc
|
||||
end
|
||||
self[i] = el
|
||||
end
|
||||
inc = (inc == 2 ? 1 : (inc * 5.0 / 11).to_i)
|
||||
end
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
data = [22, 7, 2, -5, 8, 4]
|
||||
data.shellsort!
|
||||
p data # [-5, 2, 4, 7, 8, 22]
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
siz = 100
|
||||
dim a(siz)
|
||||
for i = 1 to siz
|
||||
a(i) = rnd(1) * 1000
|
||||
next i
|
||||
|
||||
' -------------------------------
|
||||
' Shell Sort
|
||||
' -------------------------------
|
||||
incr = int(siz / 2)
|
||||
WHILE incr > 0
|
||||
for i = 1 to siz
|
||||
j = i
|
||||
temp = a(i)
|
||||
WHILE (j >= incr and a(abs(j-incr)) > temp)
|
||||
a(j) = a(j-incr)
|
||||
j = j - incr
|
||||
WEND
|
||||
a(j) = temp
|
||||
next i
|
||||
incr = int(incr / 2.2)
|
||||
WEND
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
object ShellSort {
|
||||
def incSeq(len:Int)=new Iterator[Int]{
|
||||
private[this] var x:Int=len/2
|
||||
def hasNext=x>0
|
||||
def next()={x=if (x==2) 1 else x*5/11; x}
|
||||
}
|
||||
|
||||
def InsertionSort(a:Array[Int], inc:Int)={
|
||||
for (i <- inc until a.length; temp=a(i)){
|
||||
var j=i;
|
||||
while (j>=inc && a(j-inc)>temp){
|
||||
a(j)=a(j-inc)
|
||||
j=j-inc
|
||||
}
|
||||
a(j)=temp
|
||||
}
|
||||
}
|
||||
|
||||
def shellSort(a:Array[Int])=for(inc<-incSeq(a.length)) InsertionSort(a, inc)
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
var a=Array(2, 5, 3, 4, 3, 9, 3, 2, 5, 4, 1, 3, 22, 7, 2, -5, 8, 4)
|
||||
println(a.mkString(","))
|
||||
shellSort(a)
|
||||
println(a.mkString(","))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
const proc: shellSort (inout array elemType: arr) is func
|
||||
local
|
||||
var integer: i is 0;
|
||||
var integer: j is 0;
|
||||
var integer: increment is 0;
|
||||
var elemType: help is elemType.value;
|
||||
begin
|
||||
increment := length(arr) div 2;
|
||||
while increment > 0 do
|
||||
for i range 1 to length(arr) do
|
||||
j := i;
|
||||
help := arr[i];
|
||||
while j > increment and arr[j - increment] > help do
|
||||
arr[j] := arr[j - increment];
|
||||
j -:= increment;
|
||||
end while;
|
||||
arr[j] := help;
|
||||
end for;
|
||||
increment := increment div 2;
|
||||
end while;
|
||||
end func;
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package require Tcl 8.5
|
||||
|
||||
proc shellsort {m} {
|
||||
set len [llength $m]
|
||||
set inc [expr {$len / 2}]
|
||||
while {$inc > 0} {
|
||||
for {set i $inc} {$i < $len} {incr i} {
|
||||
set j $i
|
||||
set temp [lindex $m $i]
|
||||
while {$j >= $inc && [set val [lindex $m [expr {$j - $inc}]]] > $temp} {
|
||||
lset m $j $val
|
||||
incr j -$inc
|
||||
}
|
||||
lset m $j $temp
|
||||
}
|
||||
set inc [expr {$inc == 2 ? 1 : $inc * 5 / 11}]
|
||||
}
|
||||
return $m
|
||||
}
|
||||
|
||||
puts [shellsort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
include c:\cxpl\codes; \intrinsic 'code' declarations
|
||||
string 0; \use zero-terminated strings
|
||||
|
||||
|
||||
proc SSort(A, N); \Shell sort array in ascending order
|
||||
char A; \address of array
|
||||
int N; \number of elements in array (size)
|
||||
int I, J, Gap, JG, T;
|
||||
[Gap:= N>>1;
|
||||
while Gap > 0 do
|
||||
[for I:= Gap to N-1 do
|
||||
[J:= I - Gap;
|
||||
loop [JG:= J + Gap;
|
||||
if A(J) <= A(JG) then quit;
|
||||
T:= A(J); A(J):= A(JG); A(JG):= T; \swap elements
|
||||
J:= J - Gap;
|
||||
if J < 0 then quit;
|
||||
];
|
||||
];
|
||||
Gap:= Gap>>1;
|
||||
];
|
||||
]; \SSort
|
||||
|
||||
|
||||
func StrLen(Str); \Return number of characters in an ASCIIZ string
|
||||
char Str;
|
||||
int I;
|
||||
for I:= 0 to -1>>1-1 do
|
||||
if Str(I) = 0 then return I;
|
||||
|
||||
|
||||
char Str;
|
||||
[Str:= "Pack my box with five dozen liquor jugs.";
|
||||
SSort(Str, StrLen(Str));
|
||||
Text(0, Str); CrLf(0);
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue