Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of random sorting of arrays by JS

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what are the methods of random sorting of arrays by JS". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Using array method sort to realize random sorting

There are still many ways to achieve random sorting, which can be written with for loops or tripartite js method libraries such as Lodash. But personally, I think it is more convenient to use sort, but it also has shortcomings, which is not so random. After I have seen the operating mechanism of sort, I found that it is actually compared by using a comparator.

Var arr = [1,2,3,4,5] arr.sort (function () {return Math.random ()-0.5}) console.log (arr)

two。 Shuffle algorithm to realize random sorting

One element at a time is randomly selected from the original array and placed in the new array. After each element is extracted, it is taken out of the original array (using the splice method), and the length of the original array is reduced by one.

For example:

Now I have three cards of hearts, spades An and Xiao Wang, which are required to be out of order. You can draw any one of the three cards. Suppose you draw Xiao Wang, then three cards in my hand become two cards, and your hand becomes one card, and so on. Achieve random sorting.

Code implementation:

Function shuffle (array) {let res = [], random; while (array.length > 0) {random = Math.floor (Math.random () * array.length); res.push (Array [random]); array.splice (random, 1);} return res;} console.log (shuffle)

3. In-depth Analysis of shuffling algorithm

Idea: I have a deck of cards, randomly selected cards, the number of old cards reduced accordingly!

Function shuffle (m) / / shuffle / / draw method {/ / generate m cards var arr = new Array (m); for (var item0; i0; I -) {var rnd = Math.floor (Math.random () * I); arr2.push (arrnd); arr.splice (rnd,1);} return arr2;}

But in this case, because if the array is very large, deleting an element in the middle will cause the later data to be executed, which will have a great impact on performance.

The purpose of deleting that element is not to create an empty card.

In addition to deleting that element, are there any other ways to remove empty cards?

All you have to do is put the last undrawn card in the drawn position.

Following this line of thinking, we can optimize it like this:

The code is as follows:

Function shuffle_pick (m) / / shuffle / / optimize m cards {/ / generate m cards var arr = new Array (m); for (var item0; i0;) {var rnd = Math.floor (Math.random () * I); arr2.push (arrnd]); arr [rnd] = arr [- I];} return arr2;}

In addition to drawing cards, we can also use the idea of changing cards:

Two positions are randomly exchanged for a total of n times. The larger the n is, the closer it is to the random.

Follow this line of thinking, just make a little adjustment: the first card and any card to change seats, after a round.

The code is as follows:

Function shuffle_swap (m) / / shuffle / / change method {/ / generate m cards var arr = new Array (m); for (var iTuno; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report