Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for failing to sort correctly #1012

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions Comb Sort/Comb Sort.playground/Sources/Comb Sort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@

import Foundation

public func combSort<T: Comparable>(_ input: [T]) -> [T] {
var copy: [T] = input
public func combSort<T: Comparable>(_ 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
}

Expand Down
23 changes: 10 additions & 13 deletions Comb Sort/Comb Sort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@

import Foundation

public func combSort<T: Comparable>(_ input: [T]) -> [T] {
var copy: [T] = input
public func combSort<T: Comparable>(_ 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
}

Expand Down