Skip to content

Commit a965553

Browse files
authored
Update ShellSortExample.swift
Fix bug. Origin code probally get wrong answer.My fault.
1 parent 345f844 commit a965553

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Shell Sort/ShellSortExample.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,25 @@
88

99
import Foundation
1010

11-
public func shellSort(_ list: inout [Int]) {
12-
11+
public func shellSort(_ list : inout [Int])
12+
{
1313
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 }
14+
15+
while sublistCount > 0
16+
{
17+
for var index in 0..<arr.count{
18+
19+
guard index + sublistCount < arr.count else { break }
2020

21-
if list[index] > list[index + sublistCount] {
22-
swap(&list[index], &list[index + sublistCount])
21+
if arr[index] > arr[index + sublistCount]{
22+
swap(&arr[index], &arr[index + sublistCount])
2323
}
2424

2525
guard sublistCount == 1 && index > 0 else { continue }
26-
27-
if list[index - 1] > list[index] {
28-
swap(&list[index - 1], &list[index])
26+
27+
while arr[index - 1] > arr[index] && index - 1 > 0 {
28+
swap(&arr[index - 1], &arr[index])
29+
index -= 1
2930
}
3031
}
3132
sublistCount = sublistCount / 2

0 commit comments

Comments
 (0)