Sorting algorithms/Shell sort: Difference between revisions

Content added Content deleted
(link Online Encyclopedia of Integer Sequences for other good sequences of increments)
No edit summary
Line 5: Line 5:
=={{header|Ada}}==
=={{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.
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.
<ada>
<lang ada>
generic
generic
type Element_Type is digits <>;
type Element_Type is digits <>;
Line 13: Line 13:
procedure Sort(Item : in out Array_Type);
procedure Sort(Item : in out Array_Type);
end Shell_Sort;
end Shell_Sort;
</ada>
</lang>
<ada>package body Shell_Sort is
<lang ada>package body Shell_Sort is
----------
----------
Line 44: Line 44:


end Shell_Sort;
end Shell_Sort;
</ada>
</lang>


=={{header|C}}==
=={{header|C}}==
This implementation uses an array of pre-defined increments
This implementation uses an array of pre-defined increments
<c>#include <ansi_c.h>
<lang c>#include <ansi_c.h>


#define ARRAYSIZE 100000 /* or whatever */
#define ARRAYSIZE 100000 /* or whatever */
Line 83: Line 83:
}
}
}
}
}</c>
}</lang>


=={{header|D}}==
=={{header|D}}==
From the Python version, uses Phobos of D 1.
From the Python version, uses Phobos of D 1.
<d>
<lang d>
import std.stdio: writefln;
import std.stdio: writefln;


Line 109: Line 109:
writefln(data); // [-5, 2, 4, 7, 8, 22]
writefln(data); // [-5, 2, 4, 7, 8, 22]
}
}
</d>
</lang>


=={{header|Forth}}==
=={{header|Forth}}==
Line 193: Line 193:


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.
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.
<java>public static void shell(int[] a) {
<lang java>public static void shell(int[] a) {
int increment = a.length / 2;
int increment = a.length / 2;
while (increment > 0) {
while (increment > 0) {
Line 211: Line 211:
}
}
}
}
}</java>
}</lang>


=={{header|Python}}==
=={{header|Python}}==
Line 217: Line 217:


This method sorts in place. If you want to preserve your unsorted list, copy it first.
This method sorts in place. If you want to preserve your unsorted list, copy it first.
<python>
<lang python>
def shell(seq):
def shell(seq):
inc = len(seq) // 2
inc = len(seq) // 2
Line 231: Line 231:
shell(data)
shell(data)
print data # [-5, 2, 4, 7, 8, 22]
print data # [-5, 2, 4, 7, 8, 22]
</python>
</lang>