Skip to content

Commit 7ce3763

Browse files
authored
Merge pull request #522 from remlostime/shuffle-swift
[Swift 4] Update Shuffle to Swift 4
2 parents 7077bc1 + 48eced4 commit 7ce3763

File tree

4 files changed

+75
-59
lines changed

4 files changed

+75
-59
lines changed
Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,15 @@
11
//: Playground - noun: a place where people can play
22

3-
import Foundation
4-
5-
/* Returns a random integer between 0 and n-1. */
6-
public func random(_ n: Int) -> Int {
7-
return Int(arc4random_uniform(UInt32(n)))
8-
}
3+
// last checked with Xcode 9.0b4
4+
#if swift(>=4.0)
5+
print("Hello, Swift 4!")
6+
#endif
97

10-
/* Fisher-Yates / Knuth shuffle */
11-
extension Array {
12-
public mutating func shuffle() {
13-
for i in stride(from: count - 1, through: 1, by: -1) {
14-
let j = random(i + 1)
15-
if i != j {
16-
swap(&self[i], &self[j])
17-
}
18-
}
19-
}
20-
}
8+
import Foundation
219

2210
var list = [ "a", "b", "c", "d", "e", "f", "g" ]
2311
list.shuffle()
2412
list.shuffle()
2513
list.shuffle()
2614

27-
/* Create a new array of numbers that is already shuffled. */
28-
public func shuffledArray(_ n: Int) -> [Int] {
29-
var a = [Int](repeating: 0, count: n)
30-
for i in 0..<n {
31-
let j = random(i + 1)
32-
if i != j {
33-
a[i] = a[j]
34-
}
35-
a[j] = i
36-
}
37-
return a
38-
}
39-
4015
let numbers = shuffledArray(10)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Foundation
2+
3+
/* Returns a random integer between 0 and n-1. */
4+
public func random(_ n: Int) -> Int {
5+
return Int(arc4random_uniform(UInt32(n)))
6+
}
7+
8+
extension Array {
9+
/*
10+
Randomly shuffles the array in-place
11+
This is the Fisher-Yates algorithm, also known as the Knuth shuffle.
12+
Time complexity: O(n)
13+
*/
14+
public mutating func shuffle() {
15+
for i in (1...count-1).reversed() {
16+
let j = random(i + 1)
17+
if i != j {
18+
let t = self[i]
19+
self[i] = self[j]
20+
self[j] = t
21+
}
22+
}
23+
}
24+
}
25+
26+
/*
27+
Simultaneously initializes an array with the values 0...n-1 and shuffles it.
28+
*/
29+
public func shuffledArray(_ n: Int) -> [Int] {
30+
var a = Array(repeating: 0, count: n)
31+
for i in 0..<n {
32+
let j = random(i + 1)
33+
if i != j {
34+
a[i] = a[j]
35+
}
36+
a[j] = i // insert next number from the sequence
37+
}
38+
return a
39+
}

Shuffle/Shuffle.playground/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

Shuffle/Shuffle.swift

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
import Foundation
22

3+
/* Returns a random integer between 0 and n-1. */
4+
public func random(_ n: Int) -> Int {
5+
return Int(arc4random_uniform(UInt32(n)))
6+
}
7+
38
extension Array {
4-
/*
5-
Randomly shuffles the array in-place
6-
This is the Fisher-Yates algorithm, also known as the Knuth shuffle.
7-
Time complexity: O(n)
8-
*/
9-
public mutating func shuffle() {
10-
for i in (count - 1).stride(through: 1, by: -1) {
11-
let j = random(i + 1)
12-
if i != j {
13-
swap(&self[i], &self[j])
14-
}
9+
/*
10+
Randomly shuffles the array in-place
11+
This is the Fisher-Yates algorithm, also known as the Knuth shuffle.
12+
Time complexity: O(n)
13+
*/
14+
public mutating func shuffle() {
15+
for i in (1...count-1).reversed() {
16+
let j = random(i + 1)
17+
if i != j {
18+
let t = self[i]
19+
self[i] = self[j]
20+
self[j] = t
21+
}
22+
}
1523
}
16-
}
1724
}
1825

1926
/*
20-
Simultaneously initializes an array with the values 0...n-1 and shuffles it.
21-
*/
22-
public func shuffledArray(n: Int) -> [Int] {
23-
var a = [Int](count: n, repeatedValue: 0)
24-
for i in 0..<n {
25-
let j = random(i + 1)
26-
if i != j {
27-
a[i] = a[j]
27+
Simultaneously initializes an array with the values 0...n-1 and shuffles it.
28+
*/
29+
public func shuffledArray(_ n: Int) -> [Int] {
30+
var a = Array(repeating: 0, count: n)
31+
for i in 0..<n {
32+
let j = random(i + 1)
33+
if i != j {
34+
a[i] = a[j]
35+
}
36+
a[j] = i // insert next number from the sequence
2837
}
29-
a[j] = i // insert next number from the sequence
30-
}
31-
return a
38+
return a
3239
}
40+

0 commit comments

Comments
 (0)