Exemplo n.º 1
0
        public void Sort(T[] array)
        {
            Guard.IsNotNull(array, nameof(array));

            if (array.Length == 0 || array.Length == 1)
            {
                return;
            }

            for (int i = 0; i < array.Length - 1; i++)
            {
                // track the min value of the RHS of the array
                var minIndex = i;
                var min      = array[i];

                for (int k = i + 1; k < array.Length; k++)
                {
                    if (array[k].CompareTo(min) < 0)
                    {
                        minIndex = k;
                        min      = array[k];
                    }
                }

                if (minIndex != i)
                {
                    Swapper <T> .Swap(array, i, minIndex);
                }
            }
        }
Exemplo n.º 2
0
        public void Sort(T[] array)
        {
            Guard.IsNotNull(array, nameof(array));

            if (array.Length == 0 || array.Length == 1)
            {
                return;
            }

            // From the LHS of the array, swap the values if they are smaller
            for (int i = 1; i < array.Length; i++)
            {
                for (int k = i; k > 0; k--)
                {
                    if (array[k].CompareTo(array[k - 1]) < 0)
                    {
                        Swapper <T> .Swap(array, k, k - 1);
                    }
                }
            }
        }