コード例 #1
0
ファイル: QuickSorter.cs プロジェクト: lkavale/LuKaSo.Sort
        /// <summary>
        /// Quick sort algorithm
        /// </summary>
        /// <param name="array"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        private void Sort(T[] array, uint start, uint end)
        {
            // Only one item, nothing to sort
            if (end <= start)
            {
                return;
            }

            var pivotPosition = end;
            var pivot         = array[pivotPosition];

            // Sorting by pivot
            for (uint i = start; i <= pivotPosition; i++)
            {
                if (array[i].CompareTo(pivot) > 0)
                {
                    SorterHelpers.Swap(array, pivotPosition - 1, pivotPosition);

                    if (i < pivotPosition - 1)
                    {
                        SorterHelpers.Swap(array, pivotPosition, i);
                        i--;
                    }

                    pivotPosition--;
                }
            }

            // Sort part before and after pivot
            Sort(array, start, pivotPosition - 1);
            Sort(array, pivotPosition + 1, end);
        }
コード例 #2
0
 /// <summary>
 /// Selection sort algorithm
 /// </summary>
 /// <param name="array"></param>
 /// <param name="sortedItems"></param>
 private void Sort(T[] array, uint sortedItems)
 {
     for (uint i = sortedItems; i < array.Length; i++)
     {
         var position = SorterHelpers.PositionOfMinimum(array, i, (uint)array.Length);
         SorterHelpers.Swap(array, i, position);
     }
 }
コード例 #3
0
        /// <summary>
        /// Forward sort
        /// </summary>
        /// <param name="array"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        private void SortForward(T[] array, uint start, uint end)
        {
            for (uint i = end; i > start; i--)
            {
                if (array[i].CompareTo(array[i - 1]) < 0)
                {
                    SorterHelpers.Swap(array, i - 1, i);
                }
            }

            start++;
        }
コード例 #4
0
        /// <summary>
        /// Backward sort
        /// </summary>
        /// <param name="array"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        private void SortBackward(T[] array, uint start, uint end)
        {
            for (uint i = start; i < end; i++)
            {
                if (array[i].CompareTo(array[i + 1]) > 0)
                {
                    SorterHelpers.Swap(array, i, i + 1);
                }
            }

            end--;
        }
コード例 #5
0
        /// <summary>
        /// Buble sort algorithm
        /// </summary>
        /// <param name="array"></param>
        /// <param name="iteration"></param>
        /// <returns></returns>
        public void Sort(T[] array, uint iteration)
        {
            if (iteration < 1)
            {
                return;
            }

            for (uint i = 0; i < iteration; i++)
            {
                if (array[i].CompareTo(array[i + 1]) > 0)
                {
                    SorterHelpers.Swap(array, i, i + 1);
                }
            }

            Sort(array, iteration - 1);
        }
コード例 #6
0
        /// <summary>
        /// Insertion sort algorithm
        /// </summary>
        /// <param name="array"></param>
        /// <param name="sortedItems"></param>
        private void Sort(T[] array, int sortedItems)
        {
            if (array.Length - 1 < sortedItems)
            {
                return;
            }

            for (int i = sortedItems - 1; i >= 0; i--)
            {
                if (array[i].CompareTo(array[i + 1]) > 0)
                {
                    SorterHelpers.Swap(array, (uint)i, (uint)i + 1);
                }
            }

            Sort(array, sortedItems + 1);
        }