forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1eb05ba
commit 4c796cc
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//: Playground - noun: a place where people can play | ||
|
||
import UIKit | ||
|
||
// last checked with Xcode 9.0b4 | ||
#if swift(>=4.0) | ||
print("Hello, Swift 4!") | ||
#endif | ||
|
||
public func insertionSort(_ list: inout [Int], start: Int, gap: Int) { | ||
for i in stride(from: (start + gap), to: list.count, by: gap) { | ||
let currentValue = list[i] | ||
var pos = i | ||
while pos >= gap && list[pos - gap] > currentValue { | ||
list[pos] = list[pos - gap] | ||
pos -= gap | ||
} | ||
list[pos] = currentValue | ||
} | ||
} | ||
|
||
public func shellSort(_ list: inout [Int]) { | ||
var sublistCount = list.count / 2 | ||
while sublistCount > 0 { | ||
for pos in 0..<sublistCount { | ||
insertionSort(&list, start: pos, gap: sublistCount) | ||
} | ||
sublistCount = sublistCount / 2 | ||
} | ||
} | ||
|
||
var arr = [64, 20, 50, 33, 72, 10, 23, -1, 4, 5] | ||
|
||
shellSort(&arr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |