Given a task - write a function for shuffle array. Let's start from simple solution. This method called Pencil and paper method
Example of function which shuffle array
Function shuffle, that provided below, works by following algorithm:
Example of extension for shuffle array
The algorithm remains the same. But for now, because we use extension, any array can be shuffled, as we can see above now we can shuffle array of strings too.
Mutating self extension method
We can add modified version of our extension which modify current array in-place without return new array.
Example of extension which shuffle current array in-place
Example of shuffle function implemented modern algorithm
Generic function
We also can make our function generic and apply it to array of any type, for example for array of strings.
Example of generic implementation
Also let's create two extensions as before: 1) will return new shuffled array 2) will shuffle array in-place
Example of extension with modern shuffle algorithm (Immutable and In-place)
Example of function which shuffle array
Function shuffle, that provided below, works by following algorithm:
- Make copy of incoming array to variable arrayCopy (we will remove items from this copy)
- Make new empty array randomArray(we will add new values to this array)
- For each item in copied array
- Generate random index from 0 to arrayCopy length
- Remove item with random index from arrayCopy (when remove we can store removed item)
- Add removed item to randomArray
- Return randomArray
Making Extension for shuffle array
For more handy usage let's move this code to extensionExample of extension for shuffle array
The algorithm remains the same. But for now, because we use extension, any array can be shuffled, as we can see above now we can shuffle array of strings too.
Mutating self extension method
We can add modified version of our extension which modify current array in-place without return new array.
Example of extension which shuffle current array in-place
Complexity of Pencil and paper algorithm
Complexity of this algorithm will be O(nˆ2) because- O(n) is for one 'for loop'
- O(n) is for removing from array
Model Shuffle Algorithm
How we can improve time complexity of shuffling? There is Modern method which allow us to get linear O(n) complexity. It is because we have only one loop over the array and swap random item on each iteration.
Example of shuffle function implemented modern algorithm
Generic function
We also can make our function generic and apply it to array of any type, for example for array of strings.
Example of generic implementation
Also let's create two extensions as before: 1) will return new shuffled array 2) will shuffle array in-place
Example of extension with modern shuffle algorithm (Immutable and In-place)
Really nice article. its really helpful me. Very interesting and good post thanks for sharing such a good blog.
ReplyDelete-Custom Web Design and Development
After a long time i found a unique and on purpose information about Custom Designed Websites. Can't wait to have more from you.
ReplyDeleteThank you very much for bringing this to my attention. Your blog is chock-full of useful information. I'd have no idea unless I read that. I'll return for more excellent content. Best wishes and best of success to you. Best Custom Websites
ReplyDeleteThank you for providing me with such unique content.
ReplyDeleteYou must provide such type of content constantly. I used to read your content regularly.
Custom Website Building
Thank you for writing this quality informational content.Your writing technique is impressive and enjoyable to read. I'll come back for more fantastic material.
ReplyDeleteBest wishes, and good luck.
Custom Build Website
Thank you for providing me with such unique content.
ReplyDeleteYou must provide such type of content constantly. I used to read your content regularly.
Mobile Performance Meter Hack
I adore your work, and it greatly motivates me.
ReplyDelete<a href="https://www.iptvfilms.com/things-to-think-about-before-hiring-a-sem-agency/> SEM Agency</a>
I adore your work, and it greatly motivates me.
ReplyDeleteMobile App Testing Services
I thought this was an excellent post from you, and I really enjoyed reading it.
ReplyDeleteRaymond Goodwill | Buy CBGA isolate
It's one of the most exquisite blogs I've ever seen, and it's captivating. I think this is one of the most informative blogs and webpages I have ever seen.
ReplyDeleteMateo | Deep tissue massage
I truly lack the words to express my gratitude.
ReplyDeleteIvy James| Full Spectrum CBD Distillate