diff --git a/Comb Sort/Comb Sort.playground/Sources/Comb Sort.swift b/Comb Sort/Comb Sort.playground/Sources/Comb Sort.swift index 559fe47e8..72db871db 100644 --- a/Comb Sort/Comb Sort.playground/Sources/Comb Sort.swift +++ b/Comb Sort/Comb Sort.playground/Sources/Comb Sort.swift @@ -7,25 +7,22 @@ import Foundation -public func combSort(_ input: [T]) -> [T] { - var copy: [T] = input +public func combSort(_ input: [T]) -> [T] { + var copy = input var gap = copy.count - let shrink = 1.3 + var done = false - while gap > 1 { - gap = (Int)(Double(gap) / shrink) - if gap < 1 { - gap = 1 - } - - var index = 0 - while !(index + gap >= copy.count) { + while gap > 1 || !done { + gap = max(gap * 10 / 13, 1) + done = true + for index in 0 ..< copy.count - gap { if copy[index] > copy[index + gap] { - copy.swapAt(index, index + gap) + copy.swapAt(index, index + gap) + done = false } - index += 1 } } + return copy } diff --git a/Comb Sort/Comb Sort.swift b/Comb Sort/Comb Sort.swift index bd37fed42..72db871db 100644 --- a/Comb Sort/Comb Sort.swift +++ b/Comb Sort/Comb Sort.swift @@ -7,25 +7,22 @@ import Foundation -public func combSort(_ input: [T]) -> [T] { - var copy: [T] = input +public func combSort(_ input: [T]) -> [T] { + var copy = 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) { + var done = false + + while gap > 1 || !done { + gap = max(gap * 10 / 13, 1) + done = true + for index in 0 ..< copy.count - gap { if copy[index] > copy[index + gap] { copy.swapAt(index, index + gap) + done = false } - index += 1 } } + return copy }