Skip to content

Commit 3b8331d

Browse files
committed
Merge pull request kodecocodes#37 from mitghi/b1
Add shell sort
2 parents cee2120 + b9f5f62 commit 3b8331d

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

Shell Sort/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Shell Sort
2+
3+
Shell sort is based on [Insertion Sort](https://github.com/hollance/swift-algorithm-club/tree/master/Insertion%20Sort) as a general way to improve its performance by breaking the original list into smaller sublists which can be then indivually sorted using Insertion Sort.
4+
5+
[There is a nice video created at Sapientia University which shows the process as Hungarian fol dance.](https://www.youtube.com/watch?v=CmPA7zE8mx0)
6+
7+
8+
# Process
9+
10+
Assume we want to sort [64,23,72,33,4] using Shellsort.
11+
12+
We start by creating a sublist by diving length of list by 2.
13+
14+
n = floor(5/2) = 2
15+
16+
then we iterate 0 to n times and create n sublists which its items are far apart by n.
17+
18+
Sublists are created by `insertionSort()` function. It uses start position of an item and
19+
the gap between items to create a sublist.
20+
21+
n = floor(5/2) = 2
22+
23+
i = 0 64 23 72 33 4
24+
`` `` ``
25+
64 23 4 33 72
26+
4 23 64 33 72
27+
28+
i = 1 4 23 64 33 72
29+
`` ``
30+
31+
n = floor(2/2) = 1
32+
33+
i = 0 4 23 64 33 72
34+
`` `` `` `` ``
35+
( normal insertionSort operations )
36+
4 23 33 64 72
37+
38+
Each item in sublist is compared against each other, if the condition is met, the value is swapped and that value travels all the way down and compared against previous items until we reach our start point.
39+
40+
41+
# See also
42+
43+
[Shellsort on Wikipedia](https://en.wikipedia.org/wiki/Shellsort)
44+
45+
*Written for Swift Algorithm Club by [Mike Taghavi](https://github.com/mitghi)*

Shell Sort/shellsort.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// The MIT License (MIT)
2+
3+
// Copyright (c) 2016 Mike Taghavi (mitghi[at]me.com)
4+
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
func insertionSort(inout list: [Int],start: Int, gap: Int) {
24+
for i in (start + gap).stride(to: list.count, by: gap){
25+
let currentValue = list[i]
26+
var pos = i
27+
while pos >= gap && list[pos - gap] > currentValue {
28+
list[pos] = list[pos - gap]
29+
pos = pos - gap
30+
}
31+
list[pos] = currentValue
32+
}
33+
}
34+
35+
func shellSort(inout list: [Int]) {
36+
var sublistcount = list.count / 2
37+
38+
while sublistcount > 0 {
39+
for pos in 0..<sublistcount { insertionSort(&list,start:pos, gap:sublistcount) }
40+
sublistcount = sublistcount / 2
41+
}
42+
}

0 commit comments

Comments
 (0)