Sorting algorithms/Shell sort: Difference between revisions

Content added Content deleted
(Added in-place solution in Swift 2.1, based on Python in-place example.)
Line 2,157: Line 2,157:
<pre>[54, 67, 65, 8, 56, 83, 64, 42, 20, 17]
<pre>[54, 67, 65, 8, 56, 83, 64, 42, 20, 17]
[8, 17, 20, 42, 54, 56, 64, 65, 67, 83]</pre>
[8, 17, 20, 42, 54, 56, 64, 65, 67, 83]</pre>

=={{header|Swift}}==
{{works with|Swift|2.1}}
<lang swift>func shellsort<T where T : Comparable>(inout seq: [T]) {
var inc = seq.count / 2
while inc > 0 {
for (var i, el) in EnumerateSequence(seq) {
while i >= inc && seq[i - inc] > el {
seq[i] = seq[i - inc]
i -= inc
}
seq[i] = el
}
if inc == 2 {
inc = 1
} else {
inc = inc * 5 / 11
}
}
}</lang>

{{in}}
<pre>var data = [22, 7, 2, -5, 8, 4]</pre>

{{out}}
<pre>shellsort(&data) // [-5, 2, 4, 7, 8, 22]</pre>


=={{header|Tcl}}==
=={{header|Tcl}}==