File tree Expand file tree Collapse file tree 2 files changed +30
-30
lines changed
Shuffle.playground/Sources Expand file tree Collapse file tree 2 files changed +30
-30
lines changed Original file line number Diff line number Diff line change @@ -2,15 +2,15 @@ import Foundation
2
2
3
3
/* Returns a random integer between 0 and n-1. */
4
4
public func random( _ n: Int ) -> Int {
5
- return Int ( arc4random_uniform ( UInt32 ( n) ) )
5
+ return Int ( arc4random_uniform ( UInt32 ( n) ) )
6
6
}
7
7
8
8
extension Array {
9
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
- */
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
14
public mutating func shuffle( ) {
15
15
for i in ( 1 ... count- 1 ) . reversed ( ) {
16
16
let j = random ( i + 1 )
@@ -24,8 +24,8 @@ extension Array {
24
24
}
25
25
26
26
/*
27
- Simultaneously initializes an array with the values 0...n-1 and shuffles it.
28
- */
27
+ Simultaneously initializes an array with the values 0...n-1 and shuffles it.
28
+ */
29
29
public func shuffledArray( _ n: Int ) -> [ Int ] {
30
30
var a = Array ( repeating: 0 , count: n)
31
31
for i in 0 ..< n {
Original file line number Diff line number Diff line change @@ -2,39 +2,39 @@ import Foundation
2
2
3
3
/* Returns a random integer between 0 and n-1. */
4
4
public func random( _ n: Int ) -> Int {
5
- return Int ( arc4random_uniform ( UInt32 ( n) ) )
5
+ return Int ( arc4random_uniform ( UInt32 ( n) ) )
6
6
}
7
7
8
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
- }
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
+ }
23
22
}
23
+ }
24
24
}
25
25
26
26
/*
27
27
Simultaneously initializes an array with the values 0...n-1 and shuffles it.
28
28
*/
29
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
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]
37
35
}
38
- return a
36
+ a [ j] = i // insert next number from the sequence
37
+ }
38
+ return a
39
39
}
40
40
You can’t perform that action at this time.
0 commit comments