static private void QSort_h(int[] array, int oi, int oj, ISortComparatorL comp) { int[] stack = new int[MAXSTACKSIZE]; // Stack for array bounds int top = -1; stack[++top] = oi; // Initialize stack stack[++top] = oj; while (top > 0) // While there are unprocessed subarrays { // Pop stack var j = stack[top--]; var i = stack[top--]; // Findpivot var pivotindex = (i + j) / 2; var pivot = array[pivotindex]; //DSutil.swap(array, pivotindex, j); // Stick pivot at end var tmp = array[pivotindex]; array[pivotindex] = array[j]; array[j] = tmp; // Partition var l = i - 1; var r = j; do { while (comp.lt(array[++l], pivot)) { ; } while ((r != 0) && (comp.gt(array[--r], pivot))) { ; } //DSutil.swap(array, l, r); tmp = array[l]; array[l] = array[r]; array[r] = tmp; }while (l < r); //DSutil.swap(array, l, r); // Undo swap tmp = array[l]; array[l] = array[r]; array[r] = tmp; //DSutil.swap(array, l, j); // Put pivot value in place tmp = array[l]; array[l] = array[j]; array[j] = tmp; // Put new subarrays onto stack if they are small if ((l - i) > THRESHOLD) // Left partition { stack[++top] = i; stack[++top] = l - 1; } if ((j - l) > THRESHOLD) // Right partition { stack[++top] = l + 1; stack[++top] = j; } } InsSort(array, comp); // Final Insertion Sort }
/// <summary> /// Insertion Sort. /// </summary> static public void InsSort(int[] array, ISortComparatorL comp) { for (var i = 1; i < array.Length; i++) { // Insert i'th record for (var j = i; (j > 0) && (comp.lt(array[j], array[j - 1])); j--) //DSutil.swap(array, j, j-1); { var tmp = array[j]; array[j] = array[j - 1]; array[j - 1] = tmp; } } }