Custom shuffle algorithm - Swift [closed]
I have two arrays like,..
let array1 = ["A", "B", "C", "D","E"]
let array2 = ["1", "2", "3", "4", "5", "6"]
I want to keep the order of array1 and merge both arrays and shuffle.
The output will be like
shuffledArray = ["1", "A", "3", "B", "2", "5", "C", "4", "D", "E", "6"]
How to achieve this with minimum complexity
ios swift algorithm
closed as too broad by Cristik, Mark Rotteveel, greg-449, Tomasz Mularczyk, gnat Jan 1 at 11:55
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 2 more comments
I have two arrays like,..
let array1 = ["A", "B", "C", "D","E"]
let array2 = ["1", "2", "3", "4", "5", "6"]
I want to keep the order of array1 and merge both arrays and shuffle.
The output will be like
shuffledArray = ["1", "A", "3", "B", "2", "5", "C", "4", "D", "E", "6"]
How to achieve this with minimum complexity
ios swift algorithm
closed as too broad by Cristik, Mark Rotteveel, greg-449, Tomasz Mularczyk, gnat Jan 1 at 11:55
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Why do you want to merge them?
– Rakesha Shastri
Jan 1 at 6:49
Is array lengths always with difference 1 (or equal)
– MBo
Jan 1 at 6:53
its actually for a game so special spritenods need to come randomly in game. So im mixing both nodes
– Saranjith
Jan 1 at 6:54
That doesn't explain why you need it as a single array though :/
– Rakesha Shastri
Jan 1 at 6:54
@MBo arrays are of diffrent sizes. no relation.
– Saranjith
Jan 1 at 6:54
|
show 2 more comments
I have two arrays like,..
let array1 = ["A", "B", "C", "D","E"]
let array2 = ["1", "2", "3", "4", "5", "6"]
I want to keep the order of array1 and merge both arrays and shuffle.
The output will be like
shuffledArray = ["1", "A", "3", "B", "2", "5", "C", "4", "D", "E", "6"]
How to achieve this with minimum complexity
ios swift algorithm
I have two arrays like,..
let array1 = ["A", "B", "C", "D","E"]
let array2 = ["1", "2", "3", "4", "5", "6"]
I want to keep the order of array1 and merge both arrays and shuffle.
The output will be like
shuffledArray = ["1", "A", "3", "B", "2", "5", "C", "4", "D", "E", "6"]
How to achieve this with minimum complexity
ios swift algorithm
ios swift algorithm
edited Jan 1 at 7:00
Saranjith
asked Jan 1 at 6:40
SaranjithSaranjith
4,68312363
4,68312363
closed as too broad by Cristik, Mark Rotteveel, greg-449, Tomasz Mularczyk, gnat Jan 1 at 11:55
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Cristik, Mark Rotteveel, greg-449, Tomasz Mularczyk, gnat Jan 1 at 11:55
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Why do you want to merge them?
– Rakesha Shastri
Jan 1 at 6:49
Is array lengths always with difference 1 (or equal)
– MBo
Jan 1 at 6:53
its actually for a game so special spritenods need to come randomly in game. So im mixing both nodes
– Saranjith
Jan 1 at 6:54
That doesn't explain why you need it as a single array though :/
– Rakesha Shastri
Jan 1 at 6:54
@MBo arrays are of diffrent sizes. no relation.
– Saranjith
Jan 1 at 6:54
|
show 2 more comments
Why do you want to merge them?
– Rakesha Shastri
Jan 1 at 6:49
Is array lengths always with difference 1 (or equal)
– MBo
Jan 1 at 6:53
its actually for a game so special spritenods need to come randomly in game. So im mixing both nodes
– Saranjith
Jan 1 at 6:54
That doesn't explain why you need it as a single array though :/
– Rakesha Shastri
Jan 1 at 6:54
@MBo arrays are of diffrent sizes. no relation.
– Saranjith
Jan 1 at 6:54
Why do you want to merge them?
– Rakesha Shastri
Jan 1 at 6:49
Why do you want to merge them?
– Rakesha Shastri
Jan 1 at 6:49
Is array lengths always with difference 1 (or equal)
– MBo
Jan 1 at 6:53
Is array lengths always with difference 1 (or equal)
– MBo
Jan 1 at 6:53
its actually for a game so special spritenods need to come randomly in game. So im mixing both nodes
– Saranjith
Jan 1 at 6:54
its actually for a game so special spritenods need to come randomly in game. So im mixing both nodes
– Saranjith
Jan 1 at 6:54
That doesn't explain why you need it as a single array though :/
– Rakesha Shastri
Jan 1 at 6:54
That doesn't explain why you need it as a single array though :/
– Rakesha Shastri
Jan 1 at 6:54
@MBo arrays are of diffrent sizes. no relation.
– Saranjith
Jan 1 at 6:54
@MBo arrays are of diffrent sizes. no relation.
– Saranjith
Jan 1 at 6:54
|
show 2 more comments
2 Answers
2
active
oldest
votes
This is working O(n)
var array1 = ["A", "B", "C", "D","E"]
let array2 = ["1", "2", "3", "4", "5", "6"]
array2.forEach {
let index = Int(arc4random_uniform(UInt32(array2.count)))
array1.insert($0, at: index)
}
print(array1)
add a comment |
To obtain good complexity:
Make output array of needed size - this more effective than increase its length one-by-one or inserting. Don't bother for small length.
If you need some degree of uniformity - use algorithm like Bresenham choose at every step the first or the second array (x or y step in Bresenham algo)
To choose elements from the second array in random order - use Fisher-Yates shuffling
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is working O(n)
var array1 = ["A", "B", "C", "D","E"]
let array2 = ["1", "2", "3", "4", "5", "6"]
array2.forEach {
let index = Int(arc4random_uniform(UInt32(array2.count)))
array1.insert($0, at: index)
}
print(array1)
add a comment |
This is working O(n)
var array1 = ["A", "B", "C", "D","E"]
let array2 = ["1", "2", "3", "4", "5", "6"]
array2.forEach {
let index = Int(arc4random_uniform(UInt32(array2.count)))
array1.insert($0, at: index)
}
print(array1)
add a comment |
This is working O(n)
var array1 = ["A", "B", "C", "D","E"]
let array2 = ["1", "2", "3", "4", "5", "6"]
array2.forEach {
let index = Int(arc4random_uniform(UInt32(array2.count)))
array1.insert($0, at: index)
}
print(array1)
This is working O(n)
var array1 = ["A", "B", "C", "D","E"]
let array2 = ["1", "2", "3", "4", "5", "6"]
array2.forEach {
let index = Int(arc4random_uniform(UInt32(array2.count)))
array1.insert($0, at: index)
}
print(array1)
answered Jan 1 at 7:07
SaranjithSaranjith
4,68312363
4,68312363
add a comment |
add a comment |
To obtain good complexity:
Make output array of needed size - this more effective than increase its length one-by-one or inserting. Don't bother for small length.
If you need some degree of uniformity - use algorithm like Bresenham choose at every step the first or the second array (x or y step in Bresenham algo)
To choose elements from the second array in random order - use Fisher-Yates shuffling
add a comment |
To obtain good complexity:
Make output array of needed size - this more effective than increase its length one-by-one or inserting. Don't bother for small length.
If you need some degree of uniformity - use algorithm like Bresenham choose at every step the first or the second array (x or y step in Bresenham algo)
To choose elements from the second array in random order - use Fisher-Yates shuffling
add a comment |
To obtain good complexity:
Make output array of needed size - this more effective than increase its length one-by-one or inserting. Don't bother for small length.
If you need some degree of uniformity - use algorithm like Bresenham choose at every step the first or the second array (x or y step in Bresenham algo)
To choose elements from the second array in random order - use Fisher-Yates shuffling
To obtain good complexity:
Make output array of needed size - this more effective than increase its length one-by-one or inserting. Don't bother for small length.
If you need some degree of uniformity - use algorithm like Bresenham choose at every step the first or the second array (x or y step in Bresenham algo)
To choose elements from the second array in random order - use Fisher-Yates shuffling
answered Jan 1 at 7:12
MBoMBo
48.6k23050
48.6k23050
add a comment |
add a comment |
Why do you want to merge them?
– Rakesha Shastri
Jan 1 at 6:49
Is array lengths always with difference 1 (or equal)
– MBo
Jan 1 at 6:53
its actually for a game so special spritenods need to come randomly in game. So im mixing both nodes
– Saranjith
Jan 1 at 6:54
That doesn't explain why you need it as a single array though :/
– Rakesha Shastri
Jan 1 at 6:54
@MBo arrays are of diffrent sizes. no relation.
– Saranjith
Jan 1 at 6:54