File tree 2 files changed +20
-26
lines changed
Comb Sort.playground/Sources
2 files changed +20
-26
lines changed Original file line number Diff line number Diff line change 7
7
8
8
import Foundation
9
9
10
- public func combSort< T: Comparable > ( _ input: [ T ] ) -> [ T ] {
11
- var copy : [ T ] = input
10
+ public func combSort< T: Comparable > ( _ input: [ T ] ) -> [ T ] {
11
+ var copy = input
12
12
var gap = copy. count
13
- let shrink = 1.3
13
+ var done = false
14
14
15
- while gap > 1 {
16
- gap = ( Int) ( Double ( gap) / shrink)
17
- if gap < 1 {
18
- gap = 1
19
- }
20
-
21
- var index = 0
22
- while !( index + gap >= copy. count) {
15
+ while gap > 1 || !done {
16
+ gap = max ( gap * 10 / 13 , 1 )
17
+ done = true
18
+ for index in 0 ..< copy. count - gap {
23
19
if copy [ index] > copy [ index + gap] {
24
- copy. swapAt ( index, index + gap)
20
+ copy. swapAt ( index, index + gap)
21
+ done = false
25
22
}
26
- index += 1
27
23
}
28
24
}
25
+
29
26
return copy
30
27
}
31
28
Original file line number Diff line number Diff line change 7
7
8
8
import Foundation
9
9
10
- public func combSort< T: Comparable > ( _ input: [ T ] ) -> [ T ] {
11
- var copy : [ T ] = input
10
+ public func combSort< T: Comparable > ( _ input: [ T ] ) -> [ T ] {
11
+ var copy = input
12
12
var gap = copy. count
13
- let shrink = 1.3
14
-
15
- while gap > 1 {
16
- gap = ( Int) ( Double ( gap) / shrink)
17
- if gap < 1 {
18
- gap = 1
19
- }
20
-
21
- var index = 0
22
- while !( index + gap >= copy. count) {
13
+ var done = false
14
+
15
+ while gap > 1 || !done {
16
+ gap = max ( gap * 10 / 13 , 1 )
17
+ done = true
18
+ for index in 0 ..< copy. count - gap {
23
19
if copy [ index] > copy [ index + gap] {
24
20
copy. swapAt ( index, index + gap)
21
+ done = false
25
22
}
26
- index += 1
27
23
}
28
24
}
25
+
29
26
return copy
30
27
}
31
28
You can’t perform that action at this time.
0 commit comments