What's the quickest way to find all possible pairs in list?

前端 未结 4 793
走了就别回头了
走了就别回头了 2021-01-14 05:03

Basically I have list of players, and I want to pair them up so that each player will play everyone once. What\'s the quickest way to find this data?

4条回答
  •  悲哀的现实
    2021-01-14 05:55

    I put together 2 implementations to compare performance with. The very naive version 1 is about 50% slower than the version 2. That's not to say that nothing faster exists.

    class Program
    {
        class Player
        {
            public string Name { get; set; }
    
            public Player(string name)
            {
                Name = name;
            }
        }
    
        class Match
        {
            public readonly Player Player1;
            public readonly Player Player2;
    
            public Match(Player player1, Player player2)
            {
                Player1 = player1;
                Player2 = player2;
            }
    
            public override string ToString()
            {
                return string.Format("{0} vs. {1}", Player1.Name, Player2.Name);
            }
        }
    
        static readonly List _players = new List()
        {
            new Player("John"),
            new Player("Lisa"),
            new Player("Matt"),
            new Player("Dan"),
            new Player("Steve"),
            new Player("Sarah"),
            new Player("Tim")
        };
    
        static void Main(string[] args)
        {
            const int count = 1000000;
    
            {
                var v1 = V1();
                var sw = Stopwatch.StartNew();
                for (int i = 0; i < count; i++)
                {
                    v1 = V1();
                }
                Console.WriteLine(v1);
                Console.WriteLine(sw.Elapsed);
            }
    
            {
                var v2 = V2();
                var sw = Stopwatch.StartNew();
                for (int i = 0; i < count; i++)
                {
                    v2 = V2();
                }
                Console.WriteLine(v2);
                Console.WriteLine(sw.Elapsed);
            }
    
    
            Console.ReadLine();
        }
    
        static List V1()
        {
            var challengers = new List(_players);
            var matches = new List();
            foreach (var player in _players)
            {
                challengers.Remove(player);
                foreach (var challenger in challengers)
                {
                    matches.Add(new Match(player, challenger));
                }
            }
            return matches;
        }
    
        static List V2()
        {
            var matches = new List();
            for (int i = 0; i < _players.Count; i++)
            {
                for (int j = i + 1; j < _players.Count; j++)
                {
                    matches.Add(new Match(_players[i], _players[j]));
                }
            }
            return matches;
        }
    }
    

提交回复
热议问题