File tree Expand file tree Collapse file tree 1 file changed +6
-7
lines changed Expand file tree Collapse file tree 1 file changed +6
-7
lines changed Original file line number Diff line number Diff line change 8
8
9
9
import Foundation
10
10
11
- public func shellSort( _ list: inout [ Int ] ) {
12
-
11
+ public func shellSort( _ list : inout [ Int ] ) {
13
12
var sublistCount = list. count / 2
14
-
13
+
15
14
while sublistCount > 0 {
16
-
17
- for index in 0 ..< list. count {
18
-
15
+ for var index in 0 ..< list. count {
16
+
19
17
guard index + sublistCount < list. count else { break }
20
18
21
19
if list [ index] > list [ index + sublistCount] {
@@ -24,8 +22,9 @@ public func shellSort(_ list: inout [Int]) {
24
22
25
23
guard sublistCount == 1 && index > 0 else { continue }
26
24
27
- if list [ index - 1 ] > list [ index] {
25
+ while list [ index - 1 ] > list [ index] && index - 1 > 0 {
28
26
swap ( & list[ index - 1 ] , & list[ index] )
27
+ index -= 1
29
28
}
30
29
}
31
30
sublistCount = sublistCount / 2
You can’t perform that action at this time.
0 commit comments