Skip to content

Commit 506d1fd

Browse files
authored
Merge pull request kodecocodes#416 from Moon1102/master
update ShellSortExample.swift
2 parents 7ffa266 + cf1561f commit 506d1fd

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Shell Sort/ShellSortExample.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
import Foundation
1010

11-
public func shellSort(_ list: inout [Int]) {
12-
11+
public func shellSort(_ list : inout [Int]) {
1312
var sublistCount = list.count / 2
14-
13+
1514
while sublistCount > 0 {
16-
17-
for index in 0..<list.count {
18-
15+
for var index in 0..<list.count {
16+
1917
guard index + sublistCount < list.count else { break }
2018

2119
if list[index] > list[index + sublistCount] {
@@ -24,8 +22,9 @@ public func shellSort(_ list: inout [Int]) {
2422

2523
guard sublistCount == 1 && index > 0 else { continue }
2624

27-
if list[index - 1] > list[index] {
25+
while list[index - 1] > list[index] && index - 1 > 0 {
2826
swap(&list[index - 1], &list[index])
27+
index -= 1
2928
}
3029
}
3130
sublistCount = sublistCount / 2

0 commit comments

Comments
 (0)