Skip to content

Commit

Permalink
Bugfix for failing to sort correctly
Browse files Browse the repository at this point in the history
Fix issue as below:
kodecocodes#1011
  • Loading branch information
zhaoyuyu committed Feb 4, 2023
1 parent e592ed6 commit ad9fdae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
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

0 comments on commit ad9fdae

Please sign in to comment.