Skip to content

Commit

Permalink
Fixes syntax in playground for Swift3.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelvin Lau authored and Kelvin Lau committed Sep 18, 2016
1 parent 5fbcd67 commit e9ce16b
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Quicksort/Quicksort.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation

// *** Simple but inefficient version of quicksort ***

func quicksort<T: Comparable>(a: [T]) -> [T] {
func quicksort<T: Comparable>(_ a: [T]) -> [T] {
guard a.count > 1 else { return a }

let pivot = a[a.count/2]
Expand Down Expand Up @@ -34,7 +34,7 @@ quicksort(list1)
partition is [low...p-1]; the right partition is [p+1...high], where p is the
return value.
*/
func partitionLomuto<T: Comparable>(inout a: [T], low: Int, high: Int) -> Int {
func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int {
let pivot = a[high]

var i = low
Expand All @@ -53,7 +53,7 @@ var list2 = [ 10, 0, 3, 9, 2, 14, 26, 27, 1, 5, 8, -1, 8 ]
partitionLomuto(&list2, low: 0, high: list2.count - 1)
list2

func quicksortLomuto<T: Comparable>(inout a: [T], low: Int, high: Int) {
func quicksortLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
if low < high {
let p = partitionLomuto(&a, low: low, high: high)
quicksortLomuto(&a, low: low, high: p - 1)
Expand All @@ -75,7 +75,7 @@ quicksortLomuto(&list2, low: 0, high: list2.count - 1)
where p is the return value. The pivot value is placed somewhere inside one
of the two partitions, but the algorithm doesn't tell you which one or where.
*/
func partitionHoare<T: Comparable>(inout a: [T], low: Int, high: Int) -> Int {
func partitionHoare<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int {
let pivot = a[low]
var i = low - 1
var j = high + 1
Expand All @@ -96,7 +96,7 @@ var list3 = [ 8, 0, 3, 9, 2, 14, 10, 27, 1, 5, 8, -1, 26 ]
partitionHoare(&list3, low: 0, high: list3.count - 1)
list3

func quicksortHoare<T: Comparable>(inout a: [T], low: Int, high: Int) {
func quicksortHoare<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
if low < high {
let p = partitionHoare(&a, low: low, high: high)
quicksortHoare(&a, low: low, high: p)
Expand All @@ -111,12 +111,12 @@ quicksortHoare(&list3, low: 0, high: list3.count - 1)
// *** Randomized sorting ***

/* Returns a random integer in the range min...max, inclusive. */
public func random(min min: Int, max: Int) -> Int {
public func random(min: Int, max: Int) -> Int {
assert(min < max)
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}

func quicksortRandom<T: Comparable>(inout a: [T], low: Int, high: Int) {
func quicksortRandom<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
if low < high {
let pivotIndex = random(min: low, max: high)
(a[pivotIndex], a[high]) = (a[high], a[pivotIndex])
Expand All @@ -139,7 +139,7 @@ list4
Swift's swap() doesn't like it if the items you're trying to swap refer to
the same memory location. This little wrapper simply ignores such swaps.
*/
public func swap<T>(inout a: [T], _ i: Int, _ j: Int) {
public func swap<T>(_ a: inout [T], _ i: Int, _ j: Int) {
if i != j {
swap(&a[i], &a[j])
}
Expand All @@ -149,7 +149,7 @@ public func swap<T>(inout a: [T], _ i: Int, _ j: Int) {
Dutch national flag partitioning.
Returns a tuple with the start and end index of the middle area.
*/
func partitionDutchFlag<T: Comparable>(inout a: [T], low: Int, high: Int, pivotIndex: Int) -> (Int, Int) {
func partitionDutchFlag<T: Comparable>(_ a: inout [T], low: Int, high: Int, pivotIndex: Int) -> (Int, Int) {
let pivot = a[pivotIndex]

var smaller = low
Expand All @@ -175,7 +175,7 @@ var list5 = [ 10, 0, 3, 9, 2, 14, 8, 27, 1, 5, 8, -1, 26 ]
partitionDutchFlag(&list5, low: 0, high: list5.count - 1, pivotIndex: 10)
list5

func quicksortDutchFlag<T: Comparable>(inout a: [T], low: Int, high: Int) {
func quicksortDutchFlag<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
if low < high {
let pivotIndex = random(min: low, max: high)
let (p, q) = partitionDutchFlag(&a, low: low, high: high, pivotIndex: pivotIndex)
Expand Down

0 comments on commit e9ce16b

Please sign in to comment.