Jump to content

Sorting algorithms/Shell sort: Difference between revisions

m
Fixed lang tags.
m (Fixed lang tags.)
Line 5:
=={{header|Ada}}==
This is a generic implementation of the shell sort. Ada allows arrays to be indexed by integer or enumeration types starting at any value. This version deals with any kind or value of valid index type.
<lang ada>generic
generic
type Element_Type is digits <>;
type Index_Type is (<>);
Line 12 ⟶ 11:
package Shell_Sort is
procedure Sort(Item : in out Array_Type);
end Shell_Sort;</lang>
</lang>
<lang ada>package body Shell_Sort is
Line 43 ⟶ 41:
end Sort;
 
end Shell_Sort;</lang>
</lang>
 
=={{header|ALGOL 68}}==
Line 52 ⟶ 49:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algolalgol68>MODE TYPE = CHAR;
 
PROC in place shell sort = (REF[]TYPE seq)REF[]TYPE:(
Line 196 ⟶ 193:
=={{header|D}}==
From the Python version, uses Phobos of D 1.
<lang d>import std.stdio: writefln;
import std.stdio: writefln;
 
void shell(T)(T[] seq) {
Line 217 ⟶ 213:
shell(data);
writefln(data); // [-5, 2, 4, 7, 8, 22]
}</lang>
}
</lang>
 
=={{header|E}}==
Line 241 ⟶ 236:
=={{header|Forth}}==
{{works with|GNU Forth}}
<lang forth>defer less? ' < is less?
: shell { array len -- }
1 begin dup len u<= while 2* 1+ repeat { gap }
begin gap 2/ 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 ;
 
: shell { array len -- }
create array 8 , 1 , 4 , 2 , 10 , 3 , 7 , 9 , 6 , 5 ,
1 begin dup len u<= while 2* 1+ repeat { gap }
begin gap 2/ dup to gap while
array 10 shell
len gap do
array 10 cells dump
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</lang>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<lang fortran> 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
CONTAINS
 
END MODULE sort
 
PROGRAM Shellsort
 
USE sort
 
IMPLICIT NONE
REAL :: array(1000)
CALL RANDOM_SEED
CALL RANDOM_NUMBER(array)
WRITE (*,*) "Unsorted array"
SUBROUTINE Shell_Sort(a)
WRITE (*,*) array
WRITE (*,*)
IMPLICIT NONE
CALL Shell_Sort(array)
INTEGER :: i, j, increment
WRITE (*,*) "Sorted array"
REAL :: temp
WRITE (*,*) array
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 PROGRAM Shellsort</lang>
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</lang>
 
=={{header|Haskell}}==
Line 352 ⟶ 347:
 
=={{header|Lisaac}}==
<lang Lisaac>Section Header
Section Header
 
+ name := SHELL_SORT;
Line 398 ⟶ 392:
};
};
);</lang>
</lang>
 
=={{header|OCaml}}==
Line 471 ⟶ 464:
 
This method sorts in place. If you want to preserve your unsorted list, copy it first.
<lang python>def shell(seq):
def shell(seq):
inc = len(seq) // 2
while inc:
Line 484 ⟶ 476:
data = [22, 7, 2, -5, 8, 4]
shell(data)
print data # [-5, 2, 4, 7, 8, 22]</lang>
</lang>
 
=={{header|Ruby}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.