1 C[ | ]

C
Copy
#include <stdio.h>
void shell_sort(int a[], int size) {
int i, j, temp;
int gap = size / 2;
while( gap > 0 ) {
for( i=gap; i<size; i++ ) {
temp = a[i];
j = i;
while( j>=gap && a[j-gap]>temp ) {
a[j] = a[j-gap];
j -= gap;
}
a[j] = temp;
}
gap /= 2;
}
}
int main() {
int arr[] = {9,1,22,4,0,-1,1,22,100,10};
int size = sizeof(arr)/sizeof(int);
shell_sort(arr, size);
for(int i=0; i<size; i++) printf("%d ", arr[i]);
// -1 0 1 1 4 9 10 22 22 100
}
2 C++[ | ]

C++
Copy
#include <iostream>
void shell_sort(int a[], int size) {
int i, j, temp;
int gap = size / 2;
while( gap > 0 ) {
for( i=gap; i<size; i++ ) {
temp = a[i];
j = i;
while( j>=gap && a[j-gap]>temp ) {
a[j] = a[j-gap];
j -= gap;
}
a[j] = temp;
}
gap /= 2;
}
}
int main() {
int arr[] = {9,1,22,4,0,-1,1,22,100,10};
int size = sizeof(arr)/sizeof(int);
shell_sort(arr, size);
for(int x: arr) std::cout << x << " ";
// -1 0 1 1 4 9 10 22 22 100
}
3 C#[ | ]

C#
Copy
using System;
class Program {
static void shellSort(int[] a) {
int i, j, temp, size=a.Length;
int gap = size / 2;
while( gap > 0 ) {
for( i=gap; i<size; i++ ) {
temp = a[i];
j = i;
while( j>=gap && a[j-gap]>temp ) {
a[j] = a[j-gap];
j -= gap;
}
a[j] = temp;
}
gap /= 2;
}
}
static void Main() {
int[] arr = {9,1,22,4,0,-1,1,22,100,10};
shellSort(arr);
Console.Write(string.Join(",",arr));
// -1,0,1,1,4,9,10,22,22,100
}
}
4 Java[ | ]

Java
Copy
public class MyClass {
static void shellSort(int[] a) {
int i, j, temp, size = a.length;
int gap = size / 2;
while( gap > 0 ) {
for( i=gap; i<size; i++ ) {
temp = a[i];
j = i;
while( j>=gap && a[j-gap]>temp ) {
a[j] = a[j-gap];
j -= gap;
}
a[j] = temp;
}
gap /= 2;
}
}
public static void main(String args[]) {
int[] arr = {9,1,22,4,0,-1,1,22,100,10};
shellSort(arr);
for(int x: arr) System.out.format( "%d ", x );
// -1 0 1 1 4 9 10 22 22 100
}
}
5 Perl[ | ]

Perl
Copy
sub shell_sort {
my $a=shift;
$size=@$a;
$gap=int($size/2);
while ($gap>0) {
for $i ($gap..$size-1) {
$temp=@$a[$i];
$j=$i;
while ($j >= $gap && @$a[$j-$gap] > $temp) {
@$a[$j]=@$a[$j-$gap];
$j-=$gap;
}
@$a[$j]=$temp;
}
$gap /= 2;
}
}
@arr = (9,1,22,4,0,-1,1,22,100,10);
shell_sort(\@arr);
print join(',',@arr);
# -1,0,1,1,4,9,10,22,22,100
6 PHP[ | ]

PHP
Copy
<?php
function shell_sort(&$a) {
$size = count($a);
$gap = intdiv($size,2);
while( $gap > 0 ) {
for($i=$gap; $i<$size; $i++) {
$temp = $a[$i];
$j = $i;
while( $j>=$gap && $a[$j-$gap]>$temp ) {
$a[$j] = $a[$j-$gap];
$j -= $gap;
}
$a[$j] = $temp;
}
$gap = intdiv($gap,2);
}
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
shell_sort( $arr );
echo implode(',',$arr);
# -1,0,1,1,4,9,10,22,22,100
7 Python[ | ]

Python
Copy
def shell_sort(a):
size = len(a)
gap = size//2
while gap>0:
for i in range(gap,size):
temp=a[i]
j=i
while j>=gap and a[j-gap]>temp:
a[j]=a[j-gap]
j-=gap
a[j]=temp
gap//=2
arr = [9,1,22,4,0,-1,1,22,100,10]
shell_sort(arr)
print(arr)
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
8 Ruby[ | ]

Ruby
Copy
def shell_sort(a)
size = a.size
gap = size/2
while gap>0
for i in (gap...size)
temp=a[i]
j=i
while j>=gap and a[j-gap]>temp
a[j]=a[j-gap]
j-=gap
end
a[j]=temp
end
gap /= 2
end
end
arr = [9,1,22,4,0,-1,1,22,100,10]
shell_sort(arr)
print(arr)
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]