C # Shuffle algorithm

Source: Internet
Author: User
Tags shuffle

Recently realized a truth, here to share to everyone: education represents your past, the ability to represent your present, study represents your Future.

10 years Hedong 10 years hexi, mo Bullying Juvenile Poor

Lifelong learning, Excellence

C # Shuffle algorithm is as Follows:

classprogram {Static voidMain (string[] Args) {List<string> list =Newlist<string>();            Init (list);            Xipai (list);            Print (list);            Dipai (list); List.        Clear (); }        Static voidInit (list<string>List) {list. ADD ("King"); List. ADD ("Xiao Wang"); string[] color =New string[4] {"Red Peach","Black Peach","blocks","Plum Blossom" }; string[] cate =New string[] {"A","2","3","4","5","6","7","8","9","Ten","J","Q","K", };  for(inti =0; I < Color. Length; i++)            {                 for(intj =0; J < Cate. Length; J + +) {list. ADD (color[i]+cate[j]); }            }        }        Static voidPrint (list<string>List) {            string[] card =List.            ToArray ();  for(inti =0; I < Card. Length; i++) {console.writeline (card[i]);        } Console.readkey (); }        Static voidXipai (list<string>List) {            inti =List.            Count; intj; if(i = =0)            {                return; }             while(-i! =0) {Random ran=NewRandom (); J= Ran. Next ()% (i +1); stringTMP =list[i]; list[i]=list[j]; list[j]=tmp; }        }        Static voidDipai (list<string>List) {console.writeline ("here are the cards"); Console.WriteLine ("*************************");  for(inti =0; I <3; i++) {console.writeline (list[list. Count-1]); List. RemoveAt (list. Count-1); }        }    }

The Exchange position method is used, and the program executes 54 times. Efficiency is still quite high drop!

@ Chen Wolong's Blog

C # Shuffle algorithm

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.