Skip to content

Commit 10c7a5f

Browse files
authored
Merge pull request kodecocodes#350 from taiheng/feature/swift3-combsort
Migrate Combsort to Swift 3 syntax
2 parents d3b2496 + f0e83bd commit 10c7a5f

File tree

9 files changed

+476
-26
lines changed

9 files changed

+476
-26
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ script:
1919
# - xcodebuild test -project ./Breadth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
2020
- xcodebuild test -project ./Bucket\ Sort/Tests/Tests.xcodeproj -scheme Tests
2121
- xcodebuild test -project ./B-Tree/Tests/Tests.xcodeproj -scheme Tests
22+
- xcodebuild test -project ./Comb\ Sort/Tests/Tests.xcodeproj -scheme Tests
2223
# - xcodebuild test -project ./Counting\ Sort/Tests/Tests.xcodeproj -scheme Tests
2324
# - xcodebuild test -project ./Depth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
2425
# - xcodebuild test -project ./Graph/Graph.xcodeproj -scheme GraphTests

Comb Sort/Comb Sort.playground/Contents.swift

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,12 @@
44

55
import Cocoa
66

7-
func combSort (input: [Int]) -> [Int] {
8-
var copy = input
9-
var gap = copy.count
10-
let shrink = 1.3
11-
12-
while gap > 1 {
13-
gap = Int(Double(gap) / shrink)
14-
if gap < 1 {
15-
gap = 1
16-
}
17-
18-
var index = 0
19-
while index + gap < copy.count {
20-
if copy[index] > copy[index + gap] {
21-
swap(&copy[index], &copy[index + gap])
22-
}
23-
index += 1
24-
}
25-
}
26-
return copy
27-
}
28-
297
// Test Comb Sort with small array of ten values
308
let array = [2, 32, 9, -1, 89, 101, 55, -10, -12, 67]
319
combSort(array)
3210

3311
// Test Comb Sort with large array of 1000 random values
34-
var bigArray = [Int](count: 1000, repeatedValue: 0)
12+
var bigArray = [Int](repeating: 0, count: 1000)
3513
var i = 0
3614
while i < 1000 {
3715
bigArray[i] = Int(arc4random_uniform(1000) + 1)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Comb Sort.swift
2+
// Comb Sort
3+
//
4+
// Created by Stephen.Rutstein on 7/16/16.
5+
// Copyright © 2016 Stephen.Rutstein. All rights reserved.
6+
//
7+
8+
import Foundation
9+
10+
public func combSort<T: Comparable>(_ input: [T]) -> [T] {
11+
var copy: [T] = input
12+
var gap = copy.count
13+
let shrink = 1.3
14+
15+
while gap > 1 {
16+
gap = (Int)(Double(gap) / shrink)
17+
if gap < 1 {
18+
gap = 1
19+
}
20+
21+
var index = 0
22+
while !(index + gap >= copy.count) {
23+
if copy[index] > copy[index + gap] {
24+
swap(&copy[index], &copy[index + gap])
25+
}
26+
index += 1
27+
}
28+
}
29+
return copy
30+
}
31+
32+
fileprivate func swap<T: Comparable>(a: inout T, b: inout T) {
33+
let temp = a
34+
a = b
35+
b = temp
36+
}

Comb Sort/Comb Sort.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import Foundation
99

10-
func combSort (input: [Int]) -> [Int] {
11-
var copy: [Int] = input
10+
public func combSort<T: Comparable>(_ input: [T]) -> [T] {
11+
var copy: [T] = input
1212
var gap = copy.count
1313
let shrink = 1.3
1414

@@ -29,7 +29,7 @@ func combSort (input: [Int]) -> [Int] {
2929
return copy
3030
}
3131

32-
func swap (inout a: Int, inout b: Int) {
32+
fileprivate func swap<T: Comparable>(a: inout T, b: inout T) {
3333
let temp = a
3434
a = b
3535
b = temp

Comb Sort/Tests/CombSortTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// CombSortTests.swift
3+
// Tests
4+
//
5+
// Created by theng on 2017-01-09.
6+
// Copyright © 2017 Swift Algorithm Club. All rights reserved.
7+
//
8+
9+
import XCTest
10+
11+
class CombSortTests: XCTestCase {
12+
var sequence: [Int]!
13+
let expectedSequence: [Int] = [-12, -10, -1, 2, 9, 32, 55, 67, 89, 101]
14+
15+
override func setUp() {
16+
super.setUp()
17+
sequence = [2, 32, 9, -1, 89, 101, 55, -10, -12, 67]
18+
}
19+
20+
override func tearDown() {
21+
super.tearDown()
22+
}
23+
24+
func testCombSort() {
25+
let sortedSequence = combSort(sequence)
26+
XCTAssertEqual(sortedSequence, expectedSequence)
27+
}
28+
}

Comb Sort/Tests/Info.plist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>

0 commit comments

Comments
 (0)