Data Structure in Java

Source: Internet
Author: User
Tags shuffle
All are obtained from JDK source code or Apache or Google jar package source code.

/**

* Reverses the order of the elements in the specified list. <p> * * This method runs in linear time. * * @ Param list the list whose elements are to be reversed. * @ Throws unsupportedoperationexception if the specified list or * Its list-iterator does not support the <tt> set </tt> method. */ Public static void Reverse (List <?> List) {int size = List. size (); If (size <reverse_threshold | list instanceof randomaccess) {for (INT I = 0, mid = size> 1, j = size-1; I <mid; I ++, j --) Swap (list, I, j);} else {listiterator FWD = List. listiterator (); listiterator REV = List. listiterator (size); For (INT I = 0, mid = List. size ()> 1; I <mid; I ++ ){ Object TMP = FWD. Next (); FWD. Set (rev. Previous (); rev. Set (TMP );}}} /** * Randomly permutes the specified list using a default source * Randomness. All permutations occur with approximately equal * Likelihood. <p> * * The Hedge "approximately" is used in the foregoing description because * Default source of randomness is only approximately an unbiased source * Of independently chosen bits. If it were a perfect source of randomly * Chosen bits, then the algorithm wowould choose permutations with perfect * Uniformity. <p> * * This implementation traverses the list backwards, from the last element * Up to the second, repeatedly swapping a randomly selected element * The "Current Position". elements are randomly selected from * Portion of the list that runs from the first element to the current * Position, inclusive. <p> * * This method runs in linear time. If the specified list does not * Implement the {@ link randomaccess} interface and is large, this * Implementation dumps the specified list into an array before shuffling * It, and dumps the shuffled array back into the list. This avoids * Quadratic behavior that wowould result from shuffling a "sequential * Access "list in place. * * @ Param list the list to be shuffled. * @ Throws unsupportedoperationexception if the specified list or * Its list-iterator does not support the <tt> set </tt> method. */ Public static void Shuffle (List <?> List) {shuffle (list, R);} Private Static Random R = New random (); /** * Randomly Permute the specified list using the specified source * Randomness. All permutations occur with equal likelihood * Assuming that the source of randomness is fair. <p> * * This implementation traverses the list backwards, from the last element * Up to the second, repeatedly swapping a randomly selected element * The "Current Position". elements are randomly selected from * Portion of the list that runs from the first element to the current * Position, inclusive. <p> * * This method runs in linear time. If the specified list does not * Implement the {@ link randomaccess} interface and is large, this * Implementation dumps the specified list into an array before shuffling * It, and dumps the shuffled array back into the list. This avoids * Quadratic behavior that wowould result from shuffling a "sequential * Access "list in place. * * @ Param list the list to be shuffled. * @ Param RND the source of randomness to use to shuffle the list. * @ Throws unsupportedoperationexception if the specified list or its * List-iterator does not support the <tt> set </tt> operation. */ Public static void Shuffle (List <?> List, random RND) {int size = List. size (); If (size <shuffle_threshold | list instanceof randomaccess) {for (INT I = size; I> 1; I --) Swap (list, I-1, RND. nextint (I);} else {object arr [] = List. toarray (); // shuffle array for (INT I = size; I> 1; I --) Swap (ARR, I-1, RND. nextint (I); // dump array back into list listiterator it = List. listiterator (); For (INT I = 0; I <arr. length; I ++) {It. next (); it. set (ARR [I]) ;}} /** * Swaps the elements at the specified positions in the specified list. * (If the specified positions are equal, invoking this method leaves * The list unchanged .) * * @ Param list the list in which to swap elements. * @ Param I the index of one element to be swapped. * @ Param J the index of the other element to be swapped. * @ Throws indexoutofboundsexception if either <tt> I </tt> or <tt> j </tt> * Is out of range (I & lt; 0 | I & gt; = List. Size () * | J & lt; 0 | J & gt; = List. Size ()). * @ Since 1.4 */ Public static void Swap (List <?> List, int I, Int J ){ Final list l = List; L. Set (I, L. Set (J, L. Get (I )));} /** * Swaps the two specified elements in the specified array. */ Private Static void Swap (Object [] arr, int I, Int J) {object TMP = arr [I]; arr [I] = arr [J]; arr [J] = TMP ;}.. to be continued

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.