Пример #1
0
        /// <summary>
        /// Merge from top to down
        /// </summary>
        /// <param name="data"></param>
        /// <param name="low"></param>
        /// <param name="mid"></param>
        /// <param name="hi"></param>
        private void MergeTopDown(T[] data, int low, int mid, int hi)
        {
            int i = 0;

            for (i = low; i <= hi; i++)
            {
                _copy[i] = data[i];
            }

            int j = mid + 1;

            i = low;

            for (int k = low; k <= hi; k++)
            {
                if (j <= hi && _copy[j].CompareTo(_copy[i]) < 0)
                {
                    _strategy.Swap(data, _copy, j++, k);
                    // data[k] = _copy[j++];
                }
                else if (i <= mid)
                {
                    _strategy.Swap(data, _copy, i++, k);
                    // data[k] = _copy[i++];
                }
            }
        }
Пример #2
0
        public void Sort(T[] data, ISwapStrategy <T> strategy)
        {
            if (data == null)
            {
                throw new ArgumentNullException();
            }

            int maxDist = 1;

            while (maxDist < (data.Length / 3))
            {
                maxDist = 3 * maxDist + 1;
            }

            for (int d = maxDist; d > 0; d = d / 3)
            {
                for (int i = d; i < data.Length; i = i + d)
                {
                    int index = i;
                    for (int j = i - d; j >= 0; j = j - d)
                    {
                        if (data[index].CompareTo(data[j]) >= 0)
                        {
                            break;
                        }

                        strategy.Swap(data, index, j);
                        index = j;
                    }
                }
            }
        }
Пример #3
0
        public void Sort(T[] data, ISwapStrategy <T> strategy)
        {
            if (data == null)
            {
                throw new ArgumentNullException();
            }

            for (int i = 0; i < data.Length; i++)
            {
                for (int j = i; j < data.Length; j++)
                {
                    if (((IComparable <T>)data[i]).CompareTo(data[j]) > 0)
                    {
                        strategy.Swap(data, i, j);
                    }
                }
            }
        }
        public void Sort(T[] data, ISwapStrategy <T> strategy)
        {
            for (int i = 0; i < data.Length; i++)
            {
                int index = i;
                for (int j = i - 1; j >= 0; j--)
                {
                    if (data[index].CompareTo(data[j]) >= 0) // If data[i] is greate then or equals the previous one, then it is in order
                    {
                        break;
                    }

                    // Else, swap the data[i] with the previous one,until it is greater than the previous one.
                    strategy.Swap(data, index, j);
                    index = j;
                }
            }
        }
        public void Sort(T[] data, ISwapStrategy <T> strategy)
        {
            if (data == null)
            {
                throw new ArgumentNullException();
            }

            for (int i = 0; i < data.Length; i++)
            {
                int min = i;
                for (int j = i; j < data.Length; j++)
                {
                    if (((IComparable <T>)data[j]).CompareTo((data[min])) < 0)
                    {
                        min = j;
                    }
                }

                strategy.Swap(data, i, min);
            }
        }