10000 add file ShellSortExample.swift · trongdth/swift-algorithm-club@71bfd71 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71bfd71

Browse files
authored
add file ShellSortExample.swift
update code
1 parent ae77f56 commit 71bfd71

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..<arr.count{
18+
19+
guard index + sublistCount < arr.count else { break }
20+
21+
if arr[index] > arr[index + sublistCount]{
22+
swap(&arr[index], &arr[index + sublistCount])
23+
}
24+
25+
guard sublistCount == 1 && index > 0 else { continue }
26+
27+
if arr[index - 1] > arr[index]{
28+
swap(&arr[index - 1], &arr[index])
29+
}
30+
}
31+
sublistCount = sublistCount / 2
32+
}
33+
}

0 commit comments

Comments
 (0)
0