Sorting algorithms/Shell sort: Difference between revisions

m
m (→‎{{header|REXX}}: changed a variable name, changed DO-end comments.)
m (→‎{{header|Wren}}: Minor tidy)
 
(17 intermediate revisions by 10 users not shown)
Line 24:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F shell_sort(&seq)
V inc = seq.len I/ 2
L inc != 0
Line 37:
V data = [22, 7, 2, -5, 8, 4]
shell_sort(&data)
print(data)</langsyntaxhighlight>
 
{{out}}
Line 47:
{{trans|PL/I}}
The program uses ASM structured macros and two ASSIST macros to keep the code as short as possible.
<langsyntaxhighlight lang="360asm">* Shell sort 24/06/2016
SHELLSRT CSECT
USING SHELLSRT,R13 base register
Line 122:
RK EQU 8 incr
RT EQU 9 temp
END SHELLSRT</langsyntaxhighlight>
{{out}}
<pre>
Line 130:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program shellSort64.s */
Line 303:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC PrintArray(INT ARRAY a INT size)
INT i
 
Put('[)
FOR i=0 TO size-1
DO
IF i>0 THEN Put(' ) FI
PrintI(a(i))
OD
Put(']) PutE()
RETURN
 
PROC ShellSort(INT ARRAY a INT size)
INT stp,i,j,tmp,v
 
stp=size/2
WHILE stp>0
DO
FOR i=stp TO size-1
DO
tmp=a(i)
j=i
 
WHILE j>=stp
DO
v=a(j-stp)
IF v<=tmp THEN EXIT FI
 
a(j-stp)=a(j)
a(j)=v
j==-stp
OD
 
a(j)=tmp
OD
 
stp=stp/2
OD
RETURN
 
PROC Test(INT ARRAY a INT size)
PrintE("Array before sort:")
PrintArray(a,size)
ShellSort(a,size)
PrintE("Array after sort:")
PrintArray(a,size)
PutE()
RETURN
 
PROC Main()
INT ARRAY
a(10)=[1 4 65535 0 3 7 4 8 20 65530],
b(21)=[10 9 8 7 6 5 4 3 2 1 0
65535 65534 65533 65532 65531
65530 65529 65528 65527 65526],
c(8)=[101 102 103 104 105 106 107 108],
d(12)=[1 65535 1 65535 1 65535 1
65535 1 65535 1 65535]
Test(a,10)
Test(b,21)
Test(c,8)
Test(d,12)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Shell_sort.png Screenshot from Atari 8-bit computer]
<pre>
Array before sort:
[1 4 -1 0 3 7 4 8 20 -6]
Array after sort:
[-6 -1 0 1 3 4 4 7 8 20]
 
Array before sort:
[10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5-6 -7 -8 -9 -10]
Array after sort:
[-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10]
 
Array before sort:
[101 102 103 104 105 106 107 108]
Array after sort:
[101 102 103 104 105 106 107 108]
 
Array before sort:
[1 -1 1 -1 1 -1 1 -1 1 -1 1 -1]
Array after sort:
[-1 -1 -1 -1 -1 -1 1 1 1 1 1 1]
</pre>
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">function shellSort(data:Array):Array
{
var inc:uint = data.length/2;
Line 323 ⟶ 412:
return data;
}
</syntaxhighlight>
</lang>
 
=={{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.
<langsyntaxhighlight lang="ada">generic
type Element_Type is digits <>;
type Index_Type is (<>);
Line 333 ⟶ 422:
package Shell_Sort is
procedure Sort(Item : in out Array_Type);
end Shell_Sort;</langsyntaxhighlight>
<langsyntaxhighlight lang="ada">package body Shell_Sort is
----------
Line 363 ⟶ 452:
end Sort;
 
end Shell_Sort;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 370 ⟶ 459:
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.8 algol68g-2.8].}}
'''File: prelude/sort/shell.a68'''<langsyntaxhighlight lang="algol68"># -*- coding: utf-8 -*- #
 
COMMENT
Line 430 ⟶ 519:
shell sort in place(LOC[LWB seq: UPB seq]SORTELEMENT:=seq, sort cmp rev);
 
SKIP</langsyntaxhighlight>'''File: test/sort/shell.a68'''<langsyntaxhighlight lang="algol68">#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
 
Line 437 ⟶ 526:
 
[]SORTELEMENT char array data = "big fjords vex quick waltz nymph";
print((shell sort(char array data), new line))</langsyntaxhighlight>
{{out}}
<pre>
Line 445 ⟶ 534:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">(*-- In-place Shell sort.
-- Algorithm: Donald Shell, 1959.
on ShellSort(theList, l, r) -- Sort items l thru r of theList.
*)
set listLength to (count theList)
 
if (listLength < 2) then return
-- Sort range l thru r of a list, in place.
-- Convert negative and/or transposed range indices.
on ShellSort(theList, l, r)
if (l < 0) then set l to listLength + l + 1
if (r < 0) then set r to listLength + r + 1
if (l > r) then set {l, r} to {r, l}
-- The list as a script property to allow faster references to its items.
script o
property lst : theList
end script
set listLengthstepSize to (countr theList- l + 1) div 2
ifrepeat while (listLengthstepSize > 10) then
repeat with i from (l + stepSize) to r
-- Convert negative and/or transposed range indices.
if (l < 0) then set lcurrentValue to listLength +o's llst's +item 1i
if (r < 0) thenrepeat setwith rj tofrom listLength(i +- stepSize) to rl +by 1-stepSize
if (l > r) then set {l, r} set thisValue to {r,o's lst's item l}j
if (thisValue > currentValue) then
set o's lst's item (j + stepSize) to thisValue
-- Do the sort.
set stepSize to (r - l + 1) div 2else
set j to j + stepSize
repeat while (stepSize > 0)
repeat with i from (l + stepSize) to rexit repeat
setend currentValue to item i of o's lstif
repeat with j from (i - stepSize) to l by -stepSize
set thisValue to item j of o's lst
if (currentValue < thisValue) then
set item (j + stepSize) of o's lst to thisValue
else
set j to j + stepSize
exit repeat
end if
end repeat
if (j < i) then set item j of o's lst to currentValue
end repeat
set stepSize toif (stepSizej /< 2.2i) asthen set o's lst's item j to integercurrentValue
end repeat
set stepSize to (stepSize / 2.2) as integer
end if
end repeat
return -- nothing. The input list has been sorted in place.
end ShellSort
property sort : ShellSort
 
-- Test codeDemo:
local aList
set aList to {60, 73, 11, 66, 6, 77, 41, 97, 59, 45, 64, 15, 91, 100, 22, 89, 77, 59, 54, 61}
set aList to {56, 44, 72, 4, 93, 26, 61, 72, 52, 9, 87, 26, 73, 75, 94, 91, 30, 18, 63, 16}
sort(aList, 1, -1) -- Sort the entire list.
sort(aList, 1, -1) -- Sort items 1 thru -1 of aList.
return aList</lang>
return aList</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">{64, 119, 1516, 2218, 4126, 4526, 5430, 5944, 5952, 6056, 61, 6463, 6672, 7372, 7773, 7775, 8987, 91, 9793, 10094}</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program shellSort.s */
Line 728 ⟶ 814:
bx lr @ leave function
iMagicNumber: .int 0xCCCCCCCD
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">shellSort: function [items][
a: new items
h: size a
Line 752 ⟶ 838:
]
 
print shellSort [3 1 2 8 5 7 9 4 6]</langsyntaxhighlight>
 
{{out}}
 
<pre>1 2 3 4 5 6 7 8 9</pre>
 
=={{header|ATS}}==
 
===For arrays whose elements may be of linear type===
 
<syntaxhighlight lang="ats">(* Shell sort with both the gap sequence and the order predicate
selected by templates. *)
 
#include "share/atspre_staload.hats"
 
(*------------------------------------------------------------------*)
(* Interface *)
 
extern fn {a : vt@ype} (* The "less than" template. *)
shell_sort$lt : (&a, &a) -<> bool
 
extern fn {} (* Maps array size to a gap sequence. *)
shell_sort$gaps : {n : int} size_t n -<> List1 ([i : pos] size_t i)
 
extern fn {a : vt@ype}
shell_sort {n : int}
(arr : &array (a, n) >> _,
n : size_t n)
:<!wrt> void
 
(*------------------------------------------------------------------*)
(* Implementation *)
 
extern praxi
array_v_takeout2 (* Get views for two distinct array elements.*)
{a : vt@ype}
{p : addr}
{n : int}
{i, j : nat | i < n; j < n; i != j}
(pfarr : array_v (a, p, n))
:<prf> @(a @ p + (i * sizeof a),
a @ p + (j * sizeof a),
(a @ p + (i * sizeof a),
a @ p + (j * sizeof a)) -<prf,lin> array_v (a, p, n))
 
implement {a}
shell_sort {n} (arr, n) =
let
fun
gapped_sort {gap : pos | gap < n}
{i : int | gap <= i; i <= n}
{p_arr : addr}
.<n - i>.
(pf_arr : !array_v (a, p_arr, n) >> _ |
p_arr : ptr p_arr,
gap : size_t gap,
i : size_t i)
:<!wrt> void =
if i <> n then
let
fun
move_elems {j : nat | j <= i}
.<j>.
(pf_arr : !array_v (a, p_arr, n) >> _ |
j : size_t j)
:<!wrt> void =
(* For simplicity in the safe use of an array, use
interchanges of array elements, instead of a temporary
variable and moves. *)
if gap <= j then
let
stadef k = j - gap
prval () = prop_verify {0 <= k} ()
prval () = prop_verify {k < j} ()
val k : size_t k = j - gap
 
val pk = ptr_add<a> (p_arr, k)
and pj = ptr_add<a> (p_arr, j)
 
prval @(pfk, pfj, fpf) =
array_v_takeout2 {a} {p_arr} {n} {k, j} pf_arr
val is_less = shell_sort$lt<a> (!pj, !pk)
prval () = pf_arr := fpf (pfk, pfj)
in
if is_less then
begin
array_interchange (!p_arr, k, j);
move_elems (pf_arr | k)
end
end
in
move_elems (pf_arr | i);
gapped_sort (pf_arr | p_arr, gap, succ i)
end
 
fun
go_through_gaps
{num_gaps : nat}
.<num_gaps>.
(arr : &array (a, n) >> _,
gaps : list ([i : pos] size_t i, num_gaps))
:<!wrt> void =
case+ gaps of
| list_nil () => ()
| list_cons (gap, more_gaps) =>
if n <= gap then
go_through_gaps (arr, more_gaps)
else
begin
gapped_sort (view@ arr | addr@ arr, gap, gap);
go_through_gaps (arr, more_gaps)
end
in
go_through_gaps (arr, shell_sort$gaps<> n)
end
 
(*------------------------------------------------------------------*)
 
implement
shell_sort$lt<int> (x, y) =
x < y
 
implement
main0 () =
let
(* Gaps by Marcin Ciura. https://oeis.org/A102549 *)
val ciura_gaps =
$list{[i : pos] size_t i}
(i2sz 1750,
i2sz 701, i2sz 301,
i2sz 132, i2sz 57,
i2sz 23, i2sz 10,
i2sz 4, i2sz 1)
 
implement
shell_sort$gaps<> n =
(* Use Ciura's gaps, regardless of array size. *)
ciura_gaps
 
#define SIZE 30
var i : [i : nat] int i
var arr : array (int, SIZE)
in
array_initize_elt<int> (arr, i2sz SIZE, 0);
for (i := 0; i < SIZE; i := succ i)
arr[i] := $extfcall (int, "rand") % 10;
 
for (i := 0; i < SIZE; i := succ i)
print! (" ", arr[i]);
println! ();
 
shell_sort<int> (arr, i2sz SIZE);
 
for (i := 0; i < SIZE; i := succ i)
print! (" ", arr[i]);
println! ()
end</syntaxhighlight>
 
{{out}}
<pre>$ patscc -DATS_MEMALLOC_GCBDW -O3 shell_sort_task.dats -lgc && ./a.out
3 6 7 5 3 5 6 2 9 1 2 7 0 9 3 6 0 6 2 6 1 8 7 9 2 0 2 3 7 5
0 0 0 1 1 2 2 2 2 2 3 3 3 3 5 5 5 6 6 6 6 6 7 7 7 7 8 9 9 9</pre>
 
Comments:
 
Because the value of a[i] ends up in a particular spot earlier in the array, it is common to store that value in a temporary variable, rather than use interchanges (swaps) to move the value through the array. Writing "safe" code to do it with a temporary variable, however, would have been tedious, so I used interchanges. One can always write the implementation "unsafely", however. This will still leave you with about as much "safety" as one would expect from most other languages.
 
Furthermore, there is no difficulty in using the temporary-variable approach, if the elements of the array are assumed to be non-linear ('''t@ype''' instead of '''vt@ype'''). For example, the element type used in the demonstration ('''int''') is not linear. And so the next version of the sort ...
 
===For arrays whose elements must be of non-linear type===
 
The differences from the previous implementation are mainly in the '''gapped_sort''' function. Note that the order predicate now gets its arguments by value instead of by reference.
 
<syntaxhighlight lang="ats">(* Shell sort with both the gap sequence and the order predicate
selected by templates. *)
(* This version is only for arrays of non-linear elements (whose
values may freely be copied). Thus the code looks more like what
one would write in most other programming languages. *)
 
#include "share/atspre_staload.hats"
 
(*------------------------------------------------------------------*)
(* Interface *)
 
extern fn {a : t@ype} (* The "less than" template. *)
shell_sort2$lt : (a, a) -<> bool
 
extern fn {} (* Maps array size to a gap sequence. *)
shell_sort2$gaps : {n : int} size_t n -<> List1 ([i : pos] size_t i)
 
extern fn {a : t@ype}
shell_sort2 {n : int}
(arr : &array (a, n) >> _,
n : size_t n)
:<!wrt> void
 
(*------------------------------------------------------------------*)
(* Implementation *)
 
implement {a}
shell_sort2 {n} (arr, n) =
let
macdef lt = shell_sort2$lt<a>
 
fun
gapped_sort {gap : pos | gap < n}
{i : int | gap <= i; i <= n}
.<n - i>.
(arr : &array (a, n) >> _,
gap : size_t gap,
i : size_t i)
:<!wrt> void =
if i <> n then
let
fun
move_elems {j : nat | j <= i}
.<j>.
(arr : &array (a, n) >> _,
temp : a,
j : size_t j)
:<!wrt> void =
if j < gap then
arr[j] := temp
else if ~(temp \lt arr[j - gap]) then
arr[j] := temp
else
begin
arr[j] := arr[j - gap];
move_elems (arr, temp, j - gap)
end
in
move_elems (arr, arr[i], i);
gapped_sort (arr, gap, succ i)
end
 
fun
go_through_gaps
{num_gaps : nat}
.<num_gaps>.
(arr : &array (a, n) >> _,
gaps : list ([i : pos] size_t i, num_gaps))
:<!wrt> void =
case+ gaps of
| list_nil () => ()
| list_cons (gap, more_gaps) =>
if n <= gap then
go_through_gaps (arr, more_gaps)
else
begin
gapped_sort (arr, gap, gap);
go_through_gaps (arr, more_gaps)
end
in
go_through_gaps (arr, shell_sort2$gaps<> n)
end
 
(*------------------------------------------------------------------*)
 
implement
shell_sort2$lt<int> (x, y) =
x < y
 
implement
main0 () =
let
(* Gaps by Marcin Ciura. https://oeis.org/A102549 *)
val ciura_gaps =
$list{[i : pos] size_t i}
(i2sz 1750,
i2sz 701, i2sz 301,
i2sz 132, i2sz 57,
i2sz 23, i2sz 10,
i2sz 4, i2sz 1)
 
implement
shell_sort2$gaps<> n =
(* Use Ciura's gaps, regardless of array size. *)
ciura_gaps
 
#define SIZE 30
var i : [i : nat] int i
var arr : array (int, SIZE)
in
array_initize_elt<int> (arr, i2sz SIZE, 0);
for (i := 0; i < SIZE; i := succ i)
arr[i] := $extfcall (int, "rand") % 10;
 
for (i := 0; i < SIZE; i := succ i)
print! (" ", arr[i]);
println! ();
 
shell_sort2<int> (arr, i2sz SIZE);
 
for (i := 0; i < SIZE; i := succ i)
print! (" ", arr[i]);
println! ()
end</syntaxhighlight>
 
{{out}}
<pre>$ patscc -DATS_MEMALLOC_GCBDW -O3 shell_sort_task_nonlinear.dats -lgc && ./a.out
3 6 7 5 3 5 6 2 9 1 2 7 0 9 3 6 0 6 2 6 1 8 7 9 2 0 2 3 7 5
0 0 0 1 1 2 2 2 2 2 3 3 3 3 5 5 5 6 6 6 6 6 7 7 7 7 8 9 9 9</pre>
 
=={{header|AutoHotkey}}==
ahk forum: [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=131 discussion]
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % ShellSort("")
MsgBox % ShellSort("xxx")
MsgBox % ShellSort("3,2,1")
Line 779 ⟶ 1,163:
s .= "," . a%A_Index%
Return SubStr(s,2) ; drop leading comma
}</langsyntaxhighlight>
 
=={{header|AWK}}==
{{trans|Fortran}}
<langsyntaxhighlight lang="awk">{
line[NR] = $0
}
Line 807 ⟶ 1,191:
print line[i]
}
}</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
Note that the array index is assumed to start at zero.
<langsyntaxhighlight lang="bbcbasic"> DIM test(9)
test() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
PROCshellsort(test(), 10)
Line 835 ⟶ 1,219:
NEXT
ENDWHILE
ENDPROC</langsyntaxhighlight>
{{out}}
<pre>
Line 842 ⟶ 1,226:
 
=={{header|BCPL}}==
<langsyntaxhighlight BCPLlang="bcpl">GET "libhdr"
 
LET shellsort(v, upb) BE
Line 890 ⟶ 1,274:
{ //FOR i = 1 TO n-1 UNLESS v!i<=v!(i+1) RESULTIS FALSE
RESULTIS TRUE
}</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
void shell_sort (int *a, int n) {
Line 919 ⟶ 1,303:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 927 ⟶ 1,311:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight Clang="c sharp|Cc#">
public static class ShellSorter
{
Line 957 ⟶ 1,341:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 982 ⟶ 1,366:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <time.h>
#include <iostream>
Line 1,056 ⟶ 1,440:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,086 ⟶ 1,470:
Program will sort any array using standard EBCDIC sequence (won't work properly with signed packed variables). In addition to the "usual" array and array lenght parameters, you need to supply an area (initialized to low-values) to detail row-length and up to 10 sort keys defined as follows: start position (1 based), length and sequence (Ascending/Descending).
 
<langsyntaxhighlight lang="cobol"> *******************************************************
IDENTIFICATION DIVISION.
*******************************************************
Line 1,185 ⟶ 1,569:
IF KEY-RESULT = ' '
MOVE '=' TO KEY-RESULT
END-IF.</langsyntaxhighlight>
 
===Sorting Process===
This excerpt contains just enough of the procedure division to show the workings. See the example for the bubble sort for a more complete program.
<langsyntaxhighlight COBOLlang="cobol"> C-PROCESS SECTION.
C-000.
DISPLAY "SORT STARTING".
Line 1,243 ⟶ 1,627:
 
G-999.
EXIT.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
 
<langsyntaxhighlight lang="lisp">(defun gap-insertion-sort (array predicate gap)
(let ((length (length array)))
(if (< length 2) array
Line 1,266 ⟶ 1,650:
"Last gap of ~w is not 1." gaps)
(dolist (gap gaps array)
(gap-insertion-sort array predicate gap)))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio: writeln;
 
void shellSort(T)(T[] seq) pure nothrow {
Line 1,289 ⟶ 1,673:
shellSort(data);
writeln(data);
}</langsyntaxhighlight>
{{out}}
<pre>[-5, 2, 4, 7, 8, 22]</pre>
 
=={{header|Dart}}==
 
<syntaxhighlight lang="dart">
void main() {
List<int> a = shellSort([1100, 2, 56, 200, -52, 3, 99, 33, 177, -199]);
print('$a');
}
 
shellSort(List<int> array) {
int n = array.length;
// Start with a big gap, then reduce the gap
for (int gap = n~/2; gap > 0; gap ~/= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already
// in gapped order keep adding one more element
// until the entire array is gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have been gap
// sorted save a[i] in temp and make a hole at
// position i
int temp = array[i];
// shift earlier gap-sorted elements up until
// the correct location for a[i] is found
int j;
for (j = i; j >= gap && array[j - gap] > temp; j -= gap)
array[j] = array[j - gap];
// put temp (the original a[i]) in its correct
// location
array[j] = temp;
}
}
return array;
}
</syntaxhighlight>
 
{{out}}
<pre>[-199, -52, 2, 3, 33, 56, 99, 177, 200, 1100]</pre>
 
 
=={{header|Delphi}}==
<langsyntaxhighlight lang="delphi">Procedure ShellSort(var buf:Array of Integer);
const
gaps:array[0..7] of Integer = (701, 301, 132, 57, 23, 10, 4, 1);
Line 1,316 ⟶ 1,744:
end;
end;
end;</langsyntaxhighlight>
 
=={{header|E}}==
Line 1,322 ⟶ 1,750:
{{trans|Python}}
 
<langsyntaxhighlight lang="e">/** Shell sort (in-place) */
def shellSort(array) {
var inc := array.size() // 2
Line 1,335 ⟶ 1,763:
inc := if (inc <=> 2) { 1 } else { (inc * 5.0 / 11).floor() }
}
}</langsyntaxhighlight>
 
=={{header|Eiffel}}==
Line 1,346 ⟶ 1,774:
For a more complete explanation of the Eiffel sort examples, see [[Sorting algorithms/Bubble sort#Eiffel|Bubble sort]].
 
<langsyntaxhighlight lang="eiffel">class
MY_SORTED_SET [G -> COMPARABLE]
inherit
Line 1,387 ⟶ 1,815:
end
 
end</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Sort do
def shell_sort(list) when length(list)<=1, do: list
def shell_sort(list), do: shell_sort(list, div(length(list),2))
Line 1,426 ⟶ 1,854:
 
list = [0, 14, 11, 8, 13, 15, 5, 7, 16, 17, 1, 6, 12, 2, 10, 4, 19, 9, 18, 3]
IO.inspect Sort.shell_sort(list)</langsyntaxhighlight>
 
{{out}}
Line 1,434 ⟶ 1,862:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function shell_sort(sequence s)
integer gap,j
object temp
Line 1,457 ⟶ 1,885:
? s
puts(1,"After: ")
? shell_sort(s)</langsyntaxhighlight>
 
{{out}}
Line 1,466 ⟶ 1,894:
=={{header|Forth}}==
{{works with|GNU Forth}}
<langsyntaxhighlight lang="forth">defer less? ' < is less?
 
: shell { array len -- }
Line 1,486 ⟶ 1,914:
 
array 10 shell
array 10 cells dump</langsyntaxhighlight>
 
A version without local variables:
<langsyntaxhighlight lang="forth">defer precedes ' < is precedes
 
: (shell) ( a n h -- a n h)
Line 1,516 ⟶ 1,944:
array 10 shell
array 10 cells dump</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">MODULE sort
 
CONTAINS
Line 1,570 ⟶ 1,998:
WRITE (*,*) array
END PROGRAM Shellsort</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
modified bubble sort code
<langsyntaxhighlight lang="freebasic">' version 21-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
Line 1,626 ⟶ 2,054:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>unsorted 1 -4 -1 7 -6 3 6 -7 -5 2 -2 0 5 4 -3
Line 1,633 ⟶ 2,061:
=={{header|Go}}==
Following WP pseudocode:
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,651 ⟶ 2,079:
}
fmt.Println("after: ", a)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,661 ⟶ 2,089:
Adapted version from [http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Shell_sort#Haskell]
 
<langsyntaxhighlight lang="haskell">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)</langsyntaxhighlight>
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">class ShellSort {
@:generic
public static function sort<T>(arr:Array<T>) {
Line 1,706 ⟶ 2,134:
Sys.println('Sorted Strings: ' + stringArray);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,719 ⟶ 2,147:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main() #: demonstrate various ways to sort a list and string
demosort(shellsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
Line 1,739 ⟶ 2,167:
}
return X
end</langsyntaxhighlight>
 
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'sortop', and 'demosort' in Bubble Sort]]. The full demosort exercises the named sort of a list with op = "numeric", "string", ">>" (lexically gt, descending),">" (numerically gt, descending), a custom comparator, and also a string.
Line 1,753 ⟶ 2,181:
=={{header|Io}}==
Translated from pseudocode at [[wp:Shell_sort#Shell_sort_algorithm_in_pseudocode|Wikipedia]]
<langsyntaxhighlight lang="io">List do(
shellSortInPlace := method(
gap := (size / 2) round
Line 1,773 ⟶ 2,201:
 
l := list(2, 3, 4, 5, 1)
l shellSortInPlace println # ==> list(1, 2, 3, 4, 5)</langsyntaxhighlight>
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "ShellSrt.bas"
110 RANDOMIZE
120 LET N=20 ! Number of elements
Line 1,811 ⟶ 2,239:
430 LET D=INT(D/2)
440 LOOP WHILE D>0
450 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
Line 1,818 ⟶ 2,246:
 
'''Solution'''
<langsyntaxhighlight lang="j">gaps =: [: }: 1 (1+3*])^:(> {:)^:a:~ #
insert =: (I.~ {. ]) , [ , ] }.~ I.~
gapinss =: #@] {. ,@|:@(] insert//.~ #@] $ i.@[)
shellSort =: [: ; gapinss &.>/@(< ,~ ]&.>@gaps)</langsyntaxhighlight>
 
Example:
 
<langsyntaxhighlight Jlang="j"> shellSort 8 6 4 2 1 3 5 7 9
1 2 3 4 5 6 7 8 9</langsyntaxhighlight>
 
=={{header|Java}}==
Line 1,832 ⟶ 2,260:
 
This method will sort in place. If you want to preserve your unsorted array, use a copy of the array as an argument to this method.
<langsyntaxhighlight lang="java">public static void shell(int[] a) {
int increment = a.length / 2;
while (increment > 0) {
Line 1,850 ⟶ 2,278:
}
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight Javascriptlang="javascript">function shellSort (a) {
for (var h = a.length; h > 0; h = parseInt(h / 2)) {
for (var i = h; i < a.length; i++) {
Line 1,870 ⟶ 2,298:
a.push(parseInt(Math.random() * 100));
shellSort(a);
document.write(a.join(" "));</langsyntaxhighlight>
 
=={{header|jq}}==
Line 1,877 ⟶ 2,305:
 
shellSort as defined here can be used to sort an array of arbitrary JSON entities.
<langsyntaxhighlight lang="jq"># The "while" loops are implemented using the following jq function:
 
# As soon as "condition" is true, then emit . and stop:
Line 1,900 ⟶ 2,328:
)
| [(($h+1)*5/11 | floor), .] )
| .[1] ;</langsyntaxhighlight>
'''Example''':
<langsyntaxhighlight lang="jq">([],
[5,null,3,1,2,0,4.4,5]
) | shellSort</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang="sh">$ jq -M -c -n -f Shell_sort.jq
[]
[null,0,1,2,3,4.4,5,5]</langsyntaxhighlight>
 
=={{header|Julia}}==
{{trans|Java}}
<langsyntaxhighlight lang="julia"># v0.6
 
function shellsort!(a::Array{Int})::Array{Int}
Line 1,937 ⟶ 2,365:
x = rand(1:10, 10)
@show x shellsort!(x)
@assert issorted(x)</langsyntaxhighlight>
 
{{out}}
Line 1,944 ⟶ 2,372:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.0
 
val gaps = listOf(701, 301, 132, 57, 23, 10, 4, 1) // Marcin Ciura's gap sequence
Line 1,972 ⟶ 2,400:
println(a.joinToString(", "))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,982 ⟶ 2,410:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
siz = 100
dim a(siz)
Line 2,013 ⟶ 2,441:
print a(i)
next
</syntaxhighlight>
</lang>
 
=={{header|Lisaac}}==
<langsyntaxhighlight Lisaaclang="lisaac">Section Header
 
+ name := SHELL_SORT;
Line 2,061 ⟶ 2,489:
};
};
);</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function shellsort( a )
local inc = math.ceil( #a / 2 )
while inc > 0 do
Line 2,087 ⟶ 2,515:
for _, i in pairs(a) do
print(i)
end</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Line 2,097 ⟶ 2,525:
A For statement can be as in this example or the faster For { } without Next
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module ShellSortExample {
Module shellsort(&a()) {
Line 2,123 ⟶ 2,551:
}
ShellSortExample
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<syntaxhighlight lang="text">shellsort := proc(arr)
local n, gap, i, val, j;
n := numelems(arr):
Line 2,145 ⟶ 2,573:
arr := Array([17,3,72,0,36,2,3,8,40,0]);
shellsort(arr);
arr;</langsyntaxhighlight>
{{Out|Output}}
<pre>[0,0,2,3,3,8,17,36,40,72]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">shellSort[ lst_ ] := Module[ {list = lst, incr, temp, i, j},
incr = Round[Length[list]/2];
While[incr > 0,
Line 2,165 ⟶ 2,593:
If[incr == 2, incr = 1, incr = Round[incr/2.2]]
]; list
]</langsyntaxhighlight>
 
<pre>shellSort[{2,1,4,6,8}]
Line 2,172 ⟶ 2,600:
=={{header|MATLAB}} / {{header|Octave}}==
This is a translation of the FORTRAN solution into MATLAB.
<langsyntaxhighlight MATLABlang="matlab">function list = shellSort(list)
 
N = numel(list);
Line 2,197 ⟶ 2,625:
end
end %while
end %shellSort</langsyntaxhighlight>
 
Sample Usage:
<langsyntaxhighlight MATLABlang="matlab">>> shellSort([4 3 1 5 6 2])
 
ans =
 
1 2 3 4 5 6</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
Line 2,252 ⟶ 2,680:
method isFalse public constant binary returns boolean
return \isTrue
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,275 ⟶ 2,703:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">proc shellSort[T](a: var openarray[T]) =
var h = a.len
while h > 0:
Line 2,289 ⟶ 2,717:
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
shellSort a
echo a</langsyntaxhighlight>
 
=={{header|Objeck}}==
{{trans|C sharp}}
<langsyntaxhighlight lang="objeck">
bundle Default {
class ShellSort {
Line 2,328 ⟶ 2,756:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
{{trans|C}}
<langsyntaxhighlight lang="ocaml">let shellsort a =
let len = Array.length a in
let incSequence = [| 412771; 165103; 66041; 26417; 10567;
Line 2,352 ⟶ 2,780:
done;
) incSequence;
;;</langsyntaxhighlight>
and the main:
<langsyntaxhighlight lang="ocaml">let () =
let arraysize = 1000 in (* or whatever *)
Random.self_init();
Line 2,363 ⟶ 2,791:
Array.iter (Printf.printf " %d") intArray;
print_newline();
;;</langsyntaxhighlight>
 
=={{header|ooRexx}}==
{{Trans|NetRexx}}
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
-- --- Main --------------------------------------------------------------------
call demo
Line 2,444 ⟶ 2,872:
self~put(item, ix)
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,467 ⟶ 2,895:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">shellSort(v)={
my(inc=#v\2);
while(inc,
Line 2,480 ⟶ 2,908:
);
v
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
 
<langsyntaxhighlight lang="pascal">Const
MaxN = 100; { number of elements (my example is 100) }
Type
Line 2,507 ⟶ 2,935:
End;
End;
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
 
<langsyntaxhighlight lang="perl">sub shell_sort {
my (@a, $h, $i, $j, $k) = @_;
for ($h = @a; $h = int $h / 2;) {
Line 2,529 ⟶ 2,957:
@a = shell_sort @a;
say "@a";
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 2,553 ⟶ 2,981:
<span style="color: #0000FF;">?</span><span style="color: #000000;">shell_sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,560 ⟶ 2,988:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
function shellSort($arr)
{
Line 2,580 ⟶ 3,008:
return $arr;
}
</syntaxhighlight>
</lang>
 
=={{header|Picat}}==
Using the algorithm from the Wikipedia page, inline sort.
<syntaxhighlight lang="picat">go =>
A = [23, 76, 99, 58, 97, 57, 35, 89, 51, 38, 95, 92, 24, 46, 31, 24, 14, 12, 57, 78],
println(A),
shell_sort(A),
println(A),
nl.
 
% Inline sort
shell_sort(A) =>
Inc = round(A.length/2),
while (Inc > 0)
foreach(I in Inc+1..A.length)
Temp = A[I],
J := I,
while (J > Inc, A[J-Inc] > Temp)
A[J] := A[J-Inc],
J := J - Inc
end,
A[J] := Temp
end,
Inc := round(Inc/2.2)
end.</syntaxhighlight>
 
{{out}}
<pre>[23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78]
[12,14,23,24,24,31,35,38,46,51,57,57,58,76,78,89,92,95,97,99]</pre>
 
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de shellSort (A)
(for (Inc (*/ (length A) 2) (gt0 Inc) (*/ Inc 10 22))
(for (I Inc (get A I) (inc I))
Line 2,591 ⟶ 3,049:
(dec 'J Inc) )
(set (nth A J) Tmp) ) ) )
A )</langsyntaxhighlight>
{{out}}
<pre>: (shellSort (make (do 9 (link (rand 1 999)))))
Line 2,603 ⟶ 3,061:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* Based on Rosetta Fortran */
Shell_Sort: PROCEDURE (A);
Line 2,627 ⟶ 3,085:
END;
END SHELL_SORT;
</syntaxhighlight>
</lang>
 
=={{header|PowerShell}}==
<langsyntaxhighlight PowerShelllang="powershell">Function ShellSort( [Array] $data )
{
# http://oeis.org/A108870
Line 2,655 ⟶ 3,113:
}
 
$l = 10000; ShellSort( ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( 0, $l - 1 ) } ) )</langsyntaxhighlight>
 
=={{header|PureBasic}}==
{{trans|Fortran}}
<langsyntaxhighlight PureBasiclang="purebasic">#STEP=2.2
 
Procedure Shell_sort(Array A(1))
Line 2,680 ⟶ 3,138:
EndIf
Wend
EndProcedure</langsyntaxhighlight>
 
=={{header|Python}}==
Line 2,687 ⟶ 3,145:
This method sorts in place.
If you want to preserve your unsorted list, copy it first.
<langsyntaxhighlight lang="python">def shell(seq):
inc = len(seq) // 2
while inc:
Line 2,695 ⟶ 3,153:
i -= inc
seq[i] = el
inc = 1 if inc == 2 else inc * 5 // 11</langsyntaxhighlight>
 
{{output}}
Line 2,706 ⟶ 3,164:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define (shell-sort! xs)
Line 2,721 ⟶ 3,179:
(loop (new Δ))))
xs)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub shell_sort ( @a is copy ) {
loop ( my $gap = (@a/2).round; $gap > 0; $gap = ( $gap * 5 / 11 ).round ) {
for $gap .. @a.end -> $i {
Line 2,745 ⟶ 3,203:
say 'input = ' ~ @data;
say 'output = ' ~ @data.&shell_sort;
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,758 ⟶ 3,216:
 
'''ZIP''' = '''Z'''one '''I'''mprovement '''P'''lan. &nbsp; &nbsp; Now-a-days, the USA uses two-character abbreviations.
<langsyntaxhighlight lang="rexx">/*REXX program sorts a stemmed array using the shell sort (shellsort) algorithm. */
call gen /*generate the array elements. */
call show 'before sort' /*display the before array elements. */
Line 2,803 ⟶ 3,261:
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say 'element' right(j, length(#) ) arg(1)": " @.j; end; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the (internal) inputs:}}
<pre style="height:85ex">
Line 2,915 ⟶ 3,373:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
aList = [-12, 3, 0, 4, 7, 4, 8, -5, 9]
shellSort(aList)
Line 2,937 ⟶ 3,395:
end
return a
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
Line 2,943 ⟶ 3,401:
 
This method sorts in place. If you want to preserve your unsorted list, copy it first.
<langsyntaxhighlight lang="ruby">class Array
def shellsort!
inc = length / 2
Line 2,963 ⟶ 3,421:
data = [22, 7, 2, -5, 8, 4]
data.shellsort!
p data # [-5, 2, 4, 7, 8, 22]</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
{{works with|QBasic}}
<langsyntaxhighlight lang="runbasic">siz = 100
dim a(siz)
for i = 1 to siz
Line 2,988 ⟶ 3,446:
next i
incr = int(incr / 2.2)
WEND</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
fn shell_sort<T: Ord + Copy>(v: &mut [T]) {
let mut gap = v.len() / 2;
Line 3,016 ⟶ 3,474:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,024 ⟶ 3,482:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object ShellSort {
def incSeq(len:Int)=new Iterator[Int]{
private[this] var x:Int=len/2
Line 3,050 ⟶ 3,508:
println(a.mkString(","))
}
}</langsyntaxhighlight>
{{out}}
<pre>2,5,3,4,3,9,3,2,5,4,1,3,22,7,2,-5,8,4
Line 3,056 ⟶ 3,514:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">const proc: shellSort (inout array elemType: arr) is func
local
var integer: i is 0;
Line 3,076 ⟶ 3,534:
increment := increment div 2;
end while;
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/sorting.htm#shellSort]
Line 3,082 ⟶ 3,540:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func shell_sort(a) {
var h = a.len;
while (h >>= 1) {
Line 3,099 ⟶ 3,557:
say a;
shell_sort(a);
say a;</langsyntaxhighlight>
{{out}}
<pre>[54, 67, 65, 8, 56, 83, 64, 42, 20, 17]
Line 3,106 ⟶ 3,564:
=={{header|Swift}}==
{{works with|Swift|2.1}}
<langsyntaxhighlight lang="swift">func shellsort<T where T : Comparable>(inout seq: [T]) {
var inc = seq.count / 2
while inc > 0 {
Line 3,122 ⟶ 3,580:
}
}
}</langsyntaxhighlight>
 
{{in}}
Line 3,131 ⟶ 3,589:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
proc shellsort {m} {
Line 3,151 ⟶ 3,609:
}
 
puts [shellsort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9</langsyntaxhighlight>
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">PRINT "Shell sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
Line 3,206 ⟶ 3,664:
PRINT
RETURN</langsyntaxhighlight>
 
=={{header|Visual BASICBasic}}==
<syntaxhighlight lang Visual BASIC="vb">Sub arrShellSort(ByVal arrData As Variant)
Dim lngHold, lngGap As Long
Dim lngCount, lngMin, lngMax As Long
Line 3,234 ⟶ 3,692:
Loop
arrShellSort = arrData
End Sub'</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn shell(arr mut arr []int, n int) {
mut j := 0
for h := n; h /= 2; {
Line 3,256 ⟶ 3,714:
shell(mut arr, n)
println('Output: ' + arr.str())
}</langsyntaxhighlight>
{{out}}
<pre>Input: [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]
Line 3,267 ⟶ 3,725:
=={{header|Wren}}==
Based on the Wikipedia article pseudo-code.
<langsyntaxhighlight ecmascriptlang="wren">var shellSort = Fn.new { |a|
var n = a.count
var gaps = [701, 301, 132, 57, 23, 10, 4, 1]
Line 3,285 ⟶ 3,743:
}
 
var asarray = [ [4, 65, 2, -31, 0, 99, 2, 83, 782, 1], [7, 5, 2, 6, 1, 4, 2, 6, 3] ]
for (a in asarray) {
System.print("Before: %(a)")
shellSort.call(a)
System.print("After : %(a)")
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 3,304 ⟶ 3,762:
Alternatively we can just call a library method.
{{libheader|Wren-sort}}
<langsyntaxhighlight ecmascriptlang="wren">import "./sort" for Sort
 
var asarray = [ [4, 65, 2, -31, 0, 99, 2, 83, 782, 1], [7, 5, 2, 6, 1, 4, 2, 6, 3] ]
for (a in asarray) {
System.print("Before: %(a)")
Sort.shell(a)
System.print("After : %(a)")
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 3,320 ⟶ 3,778:
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">
<lang XPL0>
include c:\cxpl\codes; \intrinsic 'code' declarations
string 0; \use zero-terminated strings
Line 3,356 ⟶ 3,814:
SSort(Str, StrLen(Str));
Text(0, Str); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 3,364 ⟶ 3,822:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">export sub shell_sort(x())
// Shell sort based on insertion sort
 
Line 3,419 ⟶ 3,877:
next n
 
end if</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl"> // Shell sort a sequence of objects in place
// Requires mutiable list
fcn shellSort(sequence){
Line 3,440 ⟶ 3,898:
}
return(sequence);
}</langsyntaxhighlight>
 
{{omit from|GUISS}}
9,476

edits