Skip to content

Commit aec126c

Browse files
authored
Merge pull request kodecocodes#392 from Moon1102/master
add file ShellSortExample.swift
2 parents 8e955cd + 69cb42e commit aec126c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Shell Sort/ShellSortExample.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// ShellSortExample.swift
3+
//
4+
//
5+
// Created by Cheer on 2017/2/26.
6+
//
7+
//
8+
9+
import Foundation
10+
11+
public func shellSort(_ list: inout [Int]) {
12+
13+
var sublistCount = list.count / 2
14+
15+
while sublistCount > 0 {
16+
17+
for index in 0..<list.count {
18+
19+
guard index + sublistCount < list.count else { break }
20+
21+
if list[index] > list[index + sublistCount] {
22+
swap(&list[index], &list[index + sublistCount])
23+
}
24+
25+
guard sublistCount == 1 && index > 0 else { continue }
26+
27+
if list[index - 1] > list[index] {
28+
swap(&list[index - 1], &list[index])
29+
}
30+
}
31+
sublistCount = sublistCount / 2
32+
}
33+
}

0 commit comments

Comments
 (0)