Interface Sort
public interface Sort
Sort algorithm trait that includes useful static functions
such as swap and swift up/down used in many sorting algorithms.
-
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic voidsiftDown(double[] a, int k, int n) To restore the max-heap condition when a node's priority is decreased.static voidsiftDown(float[] a, int k, int n) To restore the max-heap condition when a node's priority is decreased.static voidsiftDown(int[] a, int k, int n) To restore the max-heap condition when a node's priority is decreased.static <T extends Comparable<? super T>>
voidsiftDown(T[] a, int k, int n) To restore the max-heap condition when a node's priority is decreased.static voidsiftUp(double[] a, int k) To restore the max-heap condition when a node's priority is increased.static voidsiftUp(float[] a, int k) To restore the max-heap condition when a node's priority is increased.static voidsiftUp(int[] a, int k) To restore the max-heap condition when a node's priority is increased.static <T extends Comparable<? super T>>
voidsiftUp(T[] a, int k) To restore the max-heap condition when a node's priority is increased.static voidswap(double[] a, int i, int j) Swap two positions.static voidswap(float[] a, int i, int j) Swap two positions.static voidswap(int[] a, int i, int j) Swap two positions.static voidSwap two positions.
-
Method Details
-
swap
static void swap(int[] a, int i, int j) Swap two positions.- Parameters:
a- the array.i- the index of array element.j- the index of other element.
-
swap
static void swap(float[] a, int i, int j) Swap two positions.- Parameters:
a- the array.i- the index of array element.j- the index of other element.
-
swap
static void swap(double[] a, int i, int j) Swap two positions.- Parameters:
a- the array.i- the index of array element.j- the index of other element.
-
swap
Swap two positions.- Parameters:
a- the array.i- the index of array element.j- the index of other element.
-
siftUp
static void siftUp(int[] a, int k) To restore the max-heap condition when a node's priority is increased. We move up the heap, exchanging the node at position k with its parent (at position k/2) if necessary, continuing as long asa[k/2] < a[k]or until we reach the top of the heap.- Parameters:
a- the heap array.k- the index of the node to sift up.
-
siftUp
static void siftUp(float[] a, int k) To restore the max-heap condition when a node's priority is increased. We move up the heap, exchanging the node at position k with its parent (at position k/2) if necessary, continuing as long asa[k/2] < a[k]or until we reach the top of the heap.- Parameters:
a- the heap array.k- the index of the node to sift up.
-
siftUp
static void siftUp(double[] a, int k) To restore the max-heap condition when a node's priority is increased. We move up the heap, exchanging the node at position k with its parent (at position k/2) if necessary, continuing as long asa[k/2] < a[k]or until we reach the top of the heap.- Parameters:
a- the heap array.k- the index of the node to sift up.
-
siftUp
To restore the max-heap condition when a node's priority is increased. We move up the heap, exchanging the node at position k with its parent (at position k/2) if necessary, continuing as long asa[k/2] < a[k]or until we reach the top of the heap.- Type Parameters:
T- the data type of array elements.- Parameters:
a- the heap array.k- the index of the node to sift up.
-
siftDown
static void siftDown(int[] a, int k, int n) To restore the max-heap condition when a node's priority is decreased. We move down the heap, exchanging the node at position k with the larger of that node's two children if necessary and stopping when the node at k is not smaller than either child or the bottom is reached. Note that if n is even and k is n/2, then the node at k has only one child -- this case must be treated properly.- Parameters:
a- the heap array.k- the index of the node to sift down.n- the current size of the heapn > k.
-
siftDown
static void siftDown(float[] a, int k, int n) To restore the max-heap condition when a node's priority is decreased. We move down the heap, exchanging the node at position k with the larger of that node's two children if necessary and stopping when the node at k is not smaller than either child or the bottom is reached. Note that if n is even and k is n/2, then the node at k has only one child -- this case must be treated properly.- Parameters:
a- the heap array.k- the index of the node to sift down.n- the current size of the heapn > k.
-
siftDown
static void siftDown(double[] a, int k, int n) To restore the max-heap condition when a node's priority is decreased. We move down the heap, exchanging the node at position k with the larger of that node's two children if necessary and stopping when the node at k is not smaller than either child or the bottom is reached. Note that if n is even and k is n/2, then the node at k has only one child -- this case must be treated properly.- Parameters:
a- the heap array.k- the index of the node to sift down.n- the current size of the heapn > k.
-
siftDown
To restore the max-heap condition when a node's priority is decreased. We move down the heap, exchanging the node at position k with the larger of that node's two children if necessary and stopping when the node at k is not smaller than either child or the bottom is reached. Note that if n is even and k is n/2, then the node at k has only one child -- this case must be treated properly.- Type Parameters:
T- the data type of array elements.- Parameters:
a- the heap array.k- the index of the node to sift down.n- the current size of the heapn > k.
-