private static unsafe int QuickSortPartion(TemplateStructType *array, int start, int end, Comparer <TemplateStructType> comparer) { TemplateStructType pivot, startValue, endValue; pivot = array[start]; while (start < end) { startValue = array[start]; while (start < end && comparer.Compare(startValue, pivot) > 0) { start++; startValue = array[start]; } endValue = array[end]; while (start < end && comparer.Compare(endValue, pivot) < 0) { end--; endValue = array[end]; } if (start < end) { array[end] = startValue; array[start] = endValue; } } return(start); }
private static unsafe int QuickSortPartion(TemplateStructType *array, int start, int end, bool descending) { TemplateStructType pivot, startValue, endValue; pivot = array[start]; while (start < end) { startValue = array[start]; while ((start < end) && ((descending && (startValue.CompareTo(pivot) > 0)) || (!descending) && (startValue.CompareTo(pivot) < 0))) { start++; startValue = array[start]; } endValue = array[end]; while ((start < end) && ((descending && (endValue.CompareTo(pivot) < 0)) || (!descending) && (endValue.CompareTo(pivot) > 0))) { end--; endValue = array[end]; } if (start < end) { array[end] = startValue; array[start] = endValue; } } return(start); }
private static unsafe void QuickSort(IntPtr array, Comparer <TemplateStructType> comparer, Stack <int> stack) { TemplateStructType *pointer = (TemplateStructType *)array.ToPointer(); while (stack.Count > 0) { int start = stack.Pop(); int end = stack.Pop(); int index = QuickSortPartion(pointer, start, end, comparer); if (start < index - 1) { stack.Push(index - 1); stack.Push(start); } if (index + 1 < end) { stack.Push(end); stack.Push(index + 1); } } }