Ejemplo n.º 1
0
        private static int Partition <T>(IList <T> collection, int lo, int hi) where T : IComparable <T>
        {
            int i     = lo;
            int j     = hi + 1;
            var pivot = collection[lo];

            while (true)
            {
                while (collection[++i].CompareTo(pivot) < 0)
                {
                    if (i == hi)
                    {
                        break;
                    }
                }

                while (collection[--j].CompareTo(pivot) > 0)
                {
                    if (j == lo)
                    {
                        break;
                    }
                }

                if (i >= j)
                {
                    break;
                }

                SwapElements.Swap(collection, i, j);
            }

            SwapElements.Swap(collection, j, lo);
            return(j);
        }
        public static void Shuffle <T>(IList <T> collection) where T : IComparable <T>
        {
            var random = new Random();

            for (int i = 0; i < collection.Count - 1; i++)
            {
                var j = random.Next(i + 1, collection.Count);
                SwapElements.Swap(collection, i, j);
            }
        }
        public static void Sort <T>(IList <T> collection) where T : IComparable <T>
        {
            bool swap = true;

            while (swap)
            {
                swap = false;
                for (int j = 0; j < collection.Count - 1; j++)
                {
                    if (collection[j].CompareTo(collection[j + 1]) > 0)
                    {
                        SwapElements.Swap(collection, j, j + 1);
                        swap = true;
                    }
                }
            }
        }
        public static void Sort <T>(IList <T> collection) where T : IComparable <T>
        {
            for (int i = 0; i < collection.Count - 1; i++)
            {
                var min = i;

                for (int j = i + 1; j < collection.Count; j++)
                {
                    if (collection[j].CompareTo(collection[min]) < 0)
                    {
                        min = j;
                    }
                }

                if (min != i)
                {
                    SwapElements.Swap(collection, min, i);
                }
            }
        }