Skip to content

Commit 4ede1c3

Browse files
authored
Merge pull request kodecocodes#390 from Moon1102/Moon1102-patch-1
Update README.markdown
2 parents aec126c + a69e3d8 commit 4ede1c3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Shell Sort/README.markdown

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,38 @@ This is an old Commodore 64 BASIC version of shell sort that Matthijs used a lon
111111
61300 GOTO 61220
112112
61310 RETURN
113113

114+
## The Code:
115+
Here is an implementation of Shell Sort in Swift:
116+
```
117+
var arr = [64, 20, 50, 33, 72, 10, 23, -1, 4, 5]
118+
119+
public func shellSort(_ list: inout [Int]) {
120+
121+
var sublistCount = list.count / 2
122+
123+
while sublistCount > 0 {
124+
125+
for index in 0..<list.count {
126+
127+
guard index + sublistCount < list.count else { break }
128+
129+
if list[index] > list[index + sublistCount] {
130+
swap(&list[index], &list[index + sublistCount])
131+
}
132+
133+
guard sublistCount == 1 && index > 0 else { continue }
134+
135+
if list[index - 1] > list[index] {
136+
swap(&list[index - 1], &list[index])
137+
}
138+
}
139+
sublistCount = sublistCount / 2
140+
}
141+
}
142+
143+
shellSort(&arr)
144+
```
145+
114146
## See also
115147

116148
[Shellsort on Wikipedia](https://en.wikipedia.org/wiki/Shellsort)

0 commit comments

Comments
 (0)