-
Method Summary
Modifier and TypeMethodDescriptionstatic void
compareExchange
(double[] array, int minTargetIndex, int maxTargetIndex) The compare-exchange operation of sorting networks.static void
compareExchange
(int[] array, int minTargetIndex, int maxTargetIndex) The compare-exchange operation of sorting networks.static void
sort
(double[] array, int minTargetIndex, int medianTargetIndex, int maxTargetIndex) 3-element sorting network.static void
sort
(int[] array, int minTargetIndex, int medianTargetIndex, int maxTargetIndex) 3-element sorting network.
-
Method Details
-
compareExchange
public static void compareExchange(double[] array, int minTargetIndex, int maxTargetIndex) The compare-exchange operation of sorting networks. Compares two elements, swapping them if out of order. Assumes the indexes are in bounds. No elements other than the two specified elements change.- Parameters:
array
- the array to apply the compare-exchangeminTargetIndex
- the target index for the minimum of the two elementsmaxTargetIndex
- the target index for the maximum of the two elements- Throws:
ArrayIndexOutOfBoundsException
- if either of the indexes are outside the bounds of the array
-
compareExchange
public static void compareExchange(int[] array, int minTargetIndex, int maxTargetIndex) The compare-exchange operation of sorting networks. Compares two elements, swapping them if out of order. Assumes the indexes are in bounds. No elements other than the two specified elements change.- Parameters:
array
- the array to apply the compare-exchangeminTargetIndex
- the target index for the minimum of the two elementsmaxTargetIndex
- the target index for the maximum of the two elements- Throws:
ArrayIndexOutOfBoundsException
- if either of the indexes are outside the bounds of the array
-
sort
public static void sort(double[] array, int minTargetIndex, int medianTargetIndex, int maxTargetIndex) 3-element sorting network. Assumes the indexes are different and in bounds. No elements other than the three specified elements change.This sorting network makes 3 comparisons. Assuming all initial orderings are equally likely, the average number of swaps is 1.167. The maximum swaps is 2.
This sorting network uses the following sequence of compare-exchange operations, specified by indexes beginning at 0 and consecutive (the methods of this class enable specifying the indexes of the elements to sort): (0,2), (0,1), (1,2). Note that although other 3-element sorting networks are possible with the same number of compare-exchange operations, only those that begin with (0,2) minimize the average number of swaps.
- Parameters:
array
- the array to apply the compare-exchangeminTargetIndex
- the target index for the minimum of the three elementsmedianTargetIndex
- the target index for the median of the three elementsmaxTargetIndex
- the target index for the maximum of the three elements- Throws:
ArrayIndexOutOfBoundsException
- if any of the indexes are outside the bounds of the array
-
sort
public static void sort(int[] array, int minTargetIndex, int medianTargetIndex, int maxTargetIndex) 3-element sorting network. Assumes the indexes are different and in bounds. No elements other than the three specified elements change.This sorting network makes 3 comparisons. Assuming all initial orderings are equally likely, the average number of swaps is 1.167. The maximum swaps is 2.
This sorting network uses the following sequence of compare-exchange operations, specified by indexes beginning at 0 and consecutive (the methods of this class enable specifying the indexes of the elements to sort): (0,2), (0,1), (1,2). Note that although other 3-element sorting networks are possible with the same number of compare-exchange operations, only those that begin with (0,2) minimize the average number of swaps.
- Parameters:
array
- the array to apply the compare-exchangeminTargetIndex
- the target index for the minimum of the three elementsmedianTargetIndex
- the target index for the median of the three elementsmaxTargetIndex
- the target index for the maximum of the three elements- Throws:
ArrayIndexOutOfBoundsException
- if any of the indexes are outside the bounds of the array
-