/** * Internal selection method that makes recursive calls. Uses median-of-three partitioning and a * cutoff of 10. Places the kth smallest item in a[k-1]. * * @param a an array of Comparable items. * @param low the left-most index of the subarray. * @param high the right-most index of the subarray. * @param k the desired rank (1 is minimum) in the entire array. */ private static <AnyType extends Comparable<? super AnyType>> void quickSelect( AnyType[] a, int low, int high, int k) { if (low + CUTOFF > high) insertionSort(a, low, high); else { // Sort low, middle, high int middle = (low + high) / 2; if (a[middle].compareTo(a[low]) < 0) swapReferences(a, low, middle); if (a[high].compareTo(a[low]) < 0) swapReferences(a, low, high); if (a[high].compareTo(a[middle]) < 0) swapReferences(a, middle, high); // Place pivot at position high - 1 swapReferences(a, middle, high - 1); AnyType pivot = a[high - 1]; // Begin partitioning int i, j; for (i = low, j = high - 1; ; ) { while (a[++i].compareTo(pivot) < 0) ; while (pivot.compareTo(a[--j]) < 0) ; if (i >= j) break; swapReferences(a, i, j); } // Restore pivot swapReferences(a, i, high - 1); // Recurse; only this part changes if (k <= i) quickSelect(a, low, i - 1, k); else if (k > i + 1) quickSelect(a, i + 1, high, k); } }
/** * Internal quicksort method that makes recursive calls. Uses median-of-three partitioning and a * cutoff of 10. * * @param a an array of Comparable items. * @param low the left-most index of the subarray. * @param high the right-most index of the subarray. */ private static <AnyType extends Comparable<? super AnyType>> void quicksort( AnyType[] a, int low, int high) { if (low + CUTOFF > high) insertionSort(a, low, high); else { // Sort low, middle, high int middle = (low + high) / 2; if (a[middle].compareTo(a[low]) < 0) swapReferences(a, low, middle); if (a[high].compareTo(a[low]) < 0) swapReferences(a, low, high); if (a[high].compareTo(a[middle]) < 0) swapReferences(a, middle, high); // Place pivot at position high - 1 swapReferences(a, middle, high - 1); AnyType pivot = a[high - 1]; // Begin partitioning int i, j; for (i = low, j = high - 1; ; ) { while (a[++i].compareTo(pivot) < 0) ; while (pivot.compareTo(a[--j]) < 0) ; if (i >= j) break; swapReferences(a, i, j); } // Restore pivot swapReferences(a, i, high - 1); quicksort(a, low, i - 1); // Sort small elements quicksort(a, i + 1, high); // Sort large elements } }
/** * Internal method to insert into a subtree. * * @param x the item to insert. * @param t the node that roots the tree. * @return the new root. * @throws DuplicateItemException if x is already present. */ protected BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) { if (t == null) t = new BinaryNode<AnyType>(x); else if (x.compareTo(t.element) < 0) t.left = insert(x, t.left); else if (x.compareTo(t.element) > 0) t.right = insert(x, t.right); else throw new DuplicateItemException(x.toString()); // Duplicate return t; }
/** * Simple insertion sort. * * @param a an array of Comparable items. */ public static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a) { for (int p = 1; p < a.length; p++) { AnyType tmp = a[p]; int j = p; for (; j > 0 && tmp.compareTo(a[j - 1]) < 0; j--) a[j] = a[j - 1]; a[j] = tmp; } }
/** * Internal method to find an item in a subtree. * * @param x is item to search for. * @param t the node that roots the tree. * @return node containing the matched item. */ private BinaryNode<AnyType> find(AnyType x, BinaryNode<AnyType> t) { while (t != null) { if (x.compareTo(t.element) < 0) t = t.left; else if (x.compareTo(t.element) > 0) t = t.right; else return t; // Match } return null; // Not found }
/** * Shellsort, using a sequence suggested by Gonnet. * * @param a an array of Comparable items. */ public static <AnyType extends Comparable<? super AnyType>> void shellsort(AnyType[] a) { for (int gap = a.length / 2; gap > 0; gap = gap == 2 ? 1 : (int) (gap / 2.2)) for (int i = gap; i < a.length; i++) { AnyType tmp = a[i]; int j = i; for (; j >= gap && tmp.compareTo(a[j - gap]) < 0; j -= gap) a[j] = a[j - gap]; a[j] = tmp; } }
/** * Internal insertion sort routine for subarrays that is used by quicksort. * * @param a an array of Comparable items. * @param low the left-most index of the subarray. * @param n the number of items to sort. */ private static <AnyType extends Comparable<? super AnyType>> void insertionSort( AnyType[] a, int low, int high) { for (int p = low + 1; p <= high; p++) { AnyType tmp = a[p]; int j; for (j = p; j > low && tmp.compareTo(a[j - 1]) < 0; j--) a[j] = a[j - 1]; a[j] = tmp; } }
/** * Internal insertion sort routine for subarrays that is used by quicksort. * * @param a an array of Comparable items. * @param left the left-most index of the subarray. * @param right the right-most index of the subarray. */ private static <AnyType extends Comparable<? super AnyType>> void insertionSort( AnyType[] a, int left, int right) { for (int p = left + 1; p <= right; p++) { AnyType tmp = a[p]; int j; for (j = p; j > left && tmp.compareTo(a[j - 1]) < 0; j--) a[j] = a[j - 1]; a[j] = tmp; } }
private static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a) { AnyType temp; int j = 0; for (int i = 1; i < a.length; i++) { temp = a[i]; for (j = i; j > 0 && temp.compareTo(a[j - 1]) < 0; j--) { a[j] = a[j - 1]; } a[j] = temp; } }
/** * Internal method to remove from a subtree. * * @param x the item to remove. * @param t the node that roots the tree. * @return the new root. * @throws ItemNotFoundException if x is not found. */ protected BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t) { if (t == null) throw new ItemNotFoundException(x.toString()); if (x.compareTo(t.element) < 0) t.left = remove(x, t.left); else if (x.compareTo(t.element) > 0) t.right = remove(x, t.right); else if (t.left != null && t.right != null) // Two children { t.element = findMin(t.right).element; t.right = removeMin(t.right); } else t = (t.left != null) ? t.left : t.right; return t; }
/** * Internal method for heapsort that is used in deleteMax and buildHeap. * * @param a an array of Comparable items. * @index i the position from which to percolate down. * @int n the logical size of the binary heap. */ private static <AnyType extends Comparable<? super AnyType>> void percDown( AnyType[] a, int i, int n) { int child; AnyType tmp; for (tmp = a[i]; leftChild(i) < n; i = child) { child = leftChild(i); if (child != n - 1 && a[child].compareTo(a[child + 1]) < 0) child++; if (tmp.compareTo(a[child]) < 0) a[i] = a[child]; else break; } a[i] = tmp; }
private void percolatingDown(int k) { AnyType tmp = heap[k]; int child; for (; 2 * k <= size; k = child) { child = 2 * k; if (child != size && heap[child].compareTo(heap[child + 1]) > 0) child++; if (tmp.compareTo(heap[child]) > 0) heap[k] = heap[child]; else break; } heap[k] = tmp; }
/** * Generate hash value for the input value. * * @param x * @return */ private int myhash(AnyType x) { int hashVal = x.hashCode(); hashVal %= array.length; if (hashVal < 0) hashVal += array.length; return hashVal; }
/** * Insert into the priority queue, maintaining heap order. Duplicates are allowed. * * @param x the item to insert. */ public void insert(AnyType x) { if (currentSize == array.length - 1) enlargeArray(array.length * 2 + 1); // Percolate up int hole = ++currentSize; for (array[0] = x; x.compareTo(array[hole / 2]) < 0; hole /= 2) array[hole] = array[hole / 2]; array[hole] = x; }
/** Inserts a new item */ public void insert(AnyType x) { if (size == heap.length - 1) doubleSize(); // Insert a new item to the end of the array int pos = ++size; // Percolate up for (; pos > 1 && x.compareTo(heap[pos / 2]) < 0; pos = pos / 2) heap[pos] = heap[pos / 2]; heap[pos] = x; }
/** * Internal method to insert into a subtree. * * @param x the item to insert. * @param t the node that roots the tree. * @return the new root. * @throws DuplicateItemException if x is already present. */ protected BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) { if (t == null) t = new BinaryNode<AnyType>(x); else if (x.compareTo(t.element) < 0) t.left = insert(x, t.left); else if (x.compareTo(t.element) > 0) t.right = insert(x, t.right); else { t.increaseDuplicateCount(); t.hasDuplicate = true; t.duplicate = duplicate(x, t.duplicate); } return t; }