Skip to content

Commit bf46bb5

Browse files
committed
Converted playground and combSort code to Swift 3 syntax.
Changed combSort to use generic type.
1 parent a0794ba commit bf46bb5

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

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

0 commit comments

Comments
 (0)