Skip to content

Commit 4d56039

Browse files
committed
Minor styling fixes to comb sort
1 parent 04ce858 commit 4d56039

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

Comb Sort/Comb Sort.playground/Contents.swift

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,37 @@
55
import Cocoa
66

77
func combSort (input: [Int]) -> [Int] {
8-
var copy: [Int] = 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-
}
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
2516
}
26-
return copy
27-
}
2817

29-
// A function to swap two integer values
30-
// Used for swapping within the array of values.
31-
func swap (inout a: Int, inout b: Int) {
32-
let temp = a
33-
a = b
34-
b = temp
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
3527
}
3628

3729
// Test Comb Sort with small array of ten values
38-
let array: [Int] = [2, 32, 9, -1, 89, 101, 55, -10, -12, 67]
30+
let array = [2, 32, 9, -1, 89, 101, 55, -10, -12, 67]
3931
combSort(array)
4032

4133
// Test Comb Sort with large array of 1000 random values
4234
var bigArray = [Int](count: 1000, repeatedValue: 0)
4335
var i = 0
4436
while i < 1000 {
45-
bigArray[i] = Int(arc4random_uniform(1000) + 1)
46-
i += 1
37+
bigArray[i] = Int(arc4random_uniform(1000) + 1)
38+
i += 1
4739
}
4840
combSort(bigArray)
4941

0 commit comments

Comments
 (0)