Random traversal Array

Source: Internet
Author: User

After the failed postgraduate entrance exam, I had no time, and I was not in the mood to update my blog. I was unable to study, but I had to find a job. In spring school recruitment, there were no good companies, the previous big dog Teng and Meituan resumes were also decisively flushed and forced to move to social recruitment. Fortunately, they took a satisfactory offer in spring recruitment. There will be another month of graduation reply, which is not completed at all. It is possible that there will be less time to write blogs during this period of time. Let's get down to the truth. In the past few days, there is a need to traverse the array randomly. I thought about it and wrote two algorithms:

1. The first thing that comes to mind is to use hash to generate a random index sequence to traverse arrays.

Code List:


[Java]
Import java. util. HashSet;
Import java. util. Random;
 
 
Public class RandomVisitArray {
Public static void main (String [] args ){
RandomVisitArray robot = new RandomVisitArray ();
Int [] a = {1, 2, 4, 5, 6 };
Robot. visit ();
}
 
 
Public void visit (int [] ){
Random random = new Random ();
HashSet set = new HashSet ();
Int [] index_array = new int [a. length];
Int index = 0;
While (set. size () <a. length ){
Int random_index = random. nextInt (a. length );
If (! Set. contains (random_index )){
Set. add (random_index );
Index_array [index ++] = random_index;
}
}
For (int I = 0; I <index_array.length; I ++ ){
System. out. print (a [index_array [I] + "");
}
}
}

Import java. util. HashSet;
Import java. util. Random;


Public class RandomVisitArray {
Public static void main (String [] args ){
RandomVisitArray robot = new RandomVisitArray ();
Int [] a = {1, 2, 4, 5, 6 };
Robot. visit ();
}


Public void visit (int [] ){
Random random = new Random ();
HashSet set = new HashSet ();
Int [] index_array = new int [a. length];
Int index = 0;
While (set. size () <a. length ){
Int random_index = random. nextInt (a. length );
If (! Set. contains (random_index )){
Set. add (random_index );
Index_array [index ++] = random_index;
}
}
For (int I = 0; I <index_array.length; I ++ ){
System. out. print (a [index_array [I] + "");
}
}
}
The disadvantage of this method is also obvious. As the sequence increases, the probability of repetition increases. For example, for an array with a size of 100, the probability of each element obtaining success is 100% at a time, 99% ,...... 2%, 1%. Therefore, the expected number of cycles is (1 + 2 +... + 99 + 100) So the time complexity is O (n * (n + 1)/2) = O (n ^ 2 ). obviously, this result is not satisfactory.

 


2. my second idea is to generate 0 at will first .. A number between N-1 and then exchanged with the last number of the array, then 0 is randomly generated .. A tree between N-2 and the second to last number of arrays. Until the traversal of the entire array ends, it is clear that the time complexity of this algorithm is O (n ).

Code List:

[Java]
Import java. util. Random;
 
 
Public class RandomVisitArray {
Public static void main (String [] args ){
RandomVisitArray robot = new RandomVisitArray ();
Int [] a = {1, 2, 4, 5, 6 };
Robot. visit2 ();
}
 
Public void visit (int [] ){
Int index = a. length;
Random random = new Random ();
While (index> 0 ){
Int random_index = random. nextInt (index --);
System. out. print (a [random_index] + "");
A [random_index] ^ = a [index];
A [index] ^ = a [random_index];
A [random_index] ^ = a [index];
}
}
 
}

Import java. util. Random;


Public class RandomVisitArray {
Public static void main (String [] args ){
RandomVisitArray robot = new RandomVisitArray ();
Int [] a = {1, 2, 4, 5, 6 };
Robot. visit2 ();
}

Public void visit (int [] ){
Int index = a. length;
Random random = new Random ();
While (index> 0 ){
Int random_index = random. nextInt (index --);
System. out. print (a [random_index] + "");
A [random_index] ^ = a [index];
A [index] ^ = a [random_index];
A [random_index] ^ = a [index];
}
}

}
The disadvantage of this algorithm is the destruction of the original data structure. The solution is to generate a non-repeated random index sequence using similar ideas.

 


This problem is similar to the shuffling algorithm. I think there will be some other clever algorithms.

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.