Custom shuffle algorithm - Swift [closed]












-4















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










share|improve this 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
















-4















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










share|improve this 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














-4












-4








-4








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












2 Answers
2






active

oldest

votes


















1














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)





share|improve this answer































    1














    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






    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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)





      share|improve this answer




























        1














        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)





        share|improve this answer


























          1












          1








          1







          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)





          share|improve this answer













          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)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 7:07









          SaranjithSaranjith

          4,68312363




          4,68312363

























              1














              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






              share|improve this answer




























                1














                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






                share|improve this answer


























                  1












                  1








                  1







                  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






                  share|improve this answer













                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 7:12









                  MBoMBo

                  48.6k23050




                  48.6k23050















                      Popular posts from this blog

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'