forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComb Sort.swift
More file actions
36 lines (31 loc) · 764 Bytes
/
Comb Sort.swift
File metadata and controls
36 lines (31 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Comb Sort.swift
// Comb Sort
//
// Created by Stephen.Rutstein on 7/16/16.
// Copyright © 2016 Stephen.Rutstein. All rights reserved.
//
import Foundation
public func combSort<T: Comparable>(_ input: [T]) -> [T] {
var copy: [T] = input
var gap = copy.count
let shrink = 1.3
while gap > 1 {
gap = (Int)(Double(gap) / shrink)
if gap < 1 {
gap = 1
}
var index = 0
while !(index + gap >= copy.count) {
if copy[index] > copy[index + gap] {
copy.swapAt(index, index + gap)
}
index += 1
}
}
return copy
}
fileprivate func swap<T: Comparable>(a: inout T, b: inout T) {
let temp = a
a = b
b = temp
}