/// <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); }
/// <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); } }
/// <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++; }
/// <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--; }
/// <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); }
/// <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); }