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

Migrate Combsort to Swift 3 syntax #350

Merged
merged 2 commits into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Converted playground and combSort code to Swift 3 syntax.
Changed combSort to use generic type.
  • Loading branch information
taiheng committed Jan 8, 2017
commit bf46bb528139e05d41a48bed0a54d78030b420b2
24 changes: 1 addition & 23 deletions Comb Sort/Comb Sort.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,12 @@

import Cocoa

func combSort (input: [Int]) -> [Int] {
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 {
if copy[index] > copy[index + gap] {
swap(&copy[index], &copy[index + gap])
}
index += 1
}
}
return copy
}

// Test Comb Sort with small array of ten values
let array = [2, 32, 9, -1, 89, 101, 55, -10, -12, 67]
combSort(array)

// Test Comb Sort with large array of 1000 random values
var bigArray = [Int](count: 1000, repeatedValue: 0)
var bigArray = [Int](repeating: 0, count: 1000)
var i = 0
while i < 1000 {
bigArray[i] = Int(arc4random_uniform(1000) + 1)
Expand Down
36 changes: 36 additions & 0 deletions Comb Sort/Comb Sort.playground/Sources/Comb Sort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,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] {
swap(&copy[index], &copy[index + gap])
}
index += 1
}
}
return copy
}

fileprivate func swap<T: Comparable>(a: inout T, b: inout T) {
let temp = a
a = b
b = temp
}
6 changes: 3 additions & 3 deletions Comb Sort/Comb Sort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import Foundation

func combSort (input: [Int]) -> [Int] {
var copy: [Int] = input
public func combSort<T: Comparable>(_ input: [T]) -> [T] {
var copy: [T] = input
var gap = copy.count
let shrink = 1.3

Expand All @@ -29,7 +29,7 @@ func combSort (input: [Int]) -> [Int] {
return copy
}

func swap (inout a: Int, inout b: Int) {
fileprivate func swap<T: Comparable>(a: inout T, b: inout T) {
let temp = a
a = b
b = temp
Expand Down