Search in sources :

Example 1 with Priority

use of jcog.pri.Priority in project narchy by automenta.

the class ArrayBag method qsort.

// public void sortPartial(float sortPercentage) {
// int s = size();
// int sortRange = (int) Math.ceil(s * sortPercentage);
// int start = sampleIndex();
// int end = Math.min(start + sortRange, s - 1);
// 
// qsort(new int[sortSize(sortRange)], items.array(), start, end);
// }
/**
 * http://kosbie.net/cmu/summer-08/15-100/handouts/IterativeQuickSort.java
 */
public static void qsort(int[] stack, Object[] c, int left, int right) {
    int stack_pointer = -1;
    int cLenMin1 = c.length - 1;
    while (true) {
        int i, j;
        if (right - left <= 7) {
            // bubble sort on a region of right less than 8?
            for (j = left + 1; j <= right; j++) {
                Priority swap = (Priority) c[j];
                i = j - 1;
                float swapV = pCmp(swap);
                while (i >= left && cmpGT((Priority) c[i], swapV)) {
                    swap(c, i + 1, i--);
                }
                c[i + 1] = swap;
            }
            if (stack_pointer != -1) {
                right = stack[stack_pointer--];
                left = stack[stack_pointer--];
            } else {
                break;
            }
        } else {
            int median = (left + right) / 2;
            i = left + 1;
            j = right;
            swap(c, i, median);
            float cl = pCmp((Priority) c[left]);
            float cr = pCmp((Priority) c[right]);
            if (cmpGT(cl, cr)) {
                swap(c, right, left);
                float x = cr;
                cr = cl;
                cl = x;
            }
            float ci = pCmp((Priority) c[i]);
            if (cmpGT(ci, cr)) {
                swap(c, right, i);
                ci = cr;
            }
            if (cmpGT(cl, ci)) {
                swap(c, i, left);
            // float x = cl; cl = ci; ci = x;
            }
            Priority temp = (Priority) c[i];
            float tempV = pCmp(temp);
            while (true) {
                while (i < cLenMin1 && cmpLT((Priority) c[++i], tempV)) ;
                while (j > 0 && /* <- that added */
                cmpGT((Priority) c[--j], tempV)) ;
                if (j < i) {
                    break;
                }
                swap(c, j, i);
            }
            c[left + 1] = c[j];
            c[j] = temp;
            int a, b;
            if ((right - i + 1) >= (j - left)) {
                a = i;
                b = right;
                right = j - 1;
            } else {
                a = left;
                b = j - 1;
                left = i;
            }
            stack[++stack_pointer] = a;
            stack[++stack_pointer] = b;
        }
    }
}
Also used : Priority(jcog.pri.Priority)

Aggregations

Priority (jcog.pri.Priority)1