Skip to content

Commit

Permalink
Updated files for 2 spaced-indentations
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinlauKL committed Aug 1, 2017
1 parent 7ce3763 commit 403e11c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
14 changes: 7 additions & 7 deletions Shuffle/Shuffle.playground/Sources/Shuffle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import Foundation

/* Returns a random integer between 0 and n-1. */
public func random(_ n: Int) -> Int {
return Int(arc4random_uniform(UInt32(n)))
return Int(arc4random_uniform(UInt32(n)))
}

extension Array {
/*
Randomly shuffles the array in-place
This is the Fisher-Yates algorithm, also known as the Knuth shuffle.
Time complexity: O(n)
*/
Randomly shuffles the array in-place
This is the Fisher-Yates algorithm, also known as the Knuth shuffle.
Time complexity: O(n)
*/
public mutating func shuffle() {
for i in (1...count-1).reversed() {
let j = random(i + 1)
Expand All @@ -24,8 +24,8 @@ extension Array {
}

/*
Simultaneously initializes an array with the values 0...n-1 and shuffles it.
*/
Simultaneously initializes an array with the values 0...n-1 and shuffles it.
*/
public func shuffledArray(_ n: Int) -> [Int] {
var a = Array(repeating: 0, count: n)
for i in 0..<n {
Expand Down
46 changes: 23 additions & 23 deletions Shuffle/Shuffle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ import Foundation

/* Returns a random integer between 0 and n-1. */
public func random(_ n: Int) -> Int {
return Int(arc4random_uniform(UInt32(n)))
return Int(arc4random_uniform(UInt32(n)))
}

extension Array {
/*
Randomly shuffles the array in-place
This is the Fisher-Yates algorithm, also known as the Knuth shuffle.
Time complexity: O(n)
*/
public mutating func shuffle() {
for i in (1...count-1).reversed() {
let j = random(i + 1)
if i != j {
let t = self[i]
self[i] = self[j]
self[j] = t
}
}
/*
Randomly shuffles the array in-place
This is the Fisher-Yates algorithm, also known as the Knuth shuffle.
Time complexity: O(n)
*/
public mutating func shuffle() {
for i in (1...count-1).reversed() {
let j = random(i + 1)
if i != j {
let t = self[i]
self[i] = self[j]
self[j] = t
}
}
}
}

/*
Simultaneously initializes an array with the values 0...n-1 and shuffles it.
*/
public func shuffledArray(_ n: Int) -> [Int] {
var a = Array(repeating: 0, count: n)
for i in 0..<n {
let j = random(i + 1)
if i != j {
a[i] = a[j]
}
a[j] = i // insert next number from the sequence
var a = Array(repeating: 0, count: n)
for i in 0..<n {
let j = random(i + 1)
if i != j {
a[i] = a[j]
}
return a
a[j] = i // insert next number from the sequence
}
return a
}

0 comments on commit 403e11c

Please sign in to comment.