Advertisement
Guest User

Selector.java

a guest
Jan 17th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.90 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3.  
  4.  
  5. /**
  6. * Defines a library of selection methods
  7. * on arrays of ints.
  8. *
  9. * @author   Molly Anderson (MPA0012@auburn.edu)
  10. * @author   Dean Hendrix (dh@auburn.edu)
  11. * @version  2019-01-15
  12. *
  13. */
  14. public final class Selector {
  15.  
  16.    /**
  17.     * Can't instantiate this class.
  18.     *
  19.     * D O   N O T   C H A N G E   T H I S   C O N S T R U C T O R
  20.     *
  21.     */
  22.    private Selector() { }
  23.  
  24.  
  25.    /**
  26.     * Selects the minimum value from the array a. This method
  27.     * throws IllegalArgumentException if a is null or has zero
  28.     * length. The array a is not changed by this method.
  29.     */
  30.    public static int min(int[] a) {
  31.      
  32.      
  33.       if (a == null) {
  34.          throw new IllegalArgumentException("array is null");
  35.       }
  36.      
  37.       if (a.length == 0) {
  38.          throw new IllegalArgumentException("array has length of 0");
  39.       }
  40.      
  41.      
  42.       int minimum = a[0];
  43.      
  44.       for (int i = 1; i < a.length; i++) {
  45.          
  46.          if (a[i] < minimum) {
  47.             minimum = a[i];
  48.          }
  49.          
  50.       }
  51.      
  52.       return minimum;
  53.    }
  54.  
  55.  
  56.    /**
  57.     * Selects the maximum value from the array a. This method
  58.     * throws IllegalArgumentException if a is null or has zero
  59.     * length. The array a is not changed by this method.
  60.     */
  61.    public static int max(int[] a) {
  62.      
  63.      
  64.       if (a == null) {
  65.          throw new IllegalArgumentException("array is null");
  66.       }
  67.      
  68.       if (a.length == 0) {
  69.          throw new IllegalArgumentException("array has length of 0");
  70.       }
  71.      
  72.       int maximum = a[0];
  73.      
  74.       for (int i = 1; i < a.length; i++) {
  75.          
  76.          if (a[i] > maximum) {
  77.             maximum = a[i];
  78.          }
  79.       }
  80.      
  81.       return maximum;
  82.    }
  83.  
  84.  
  85.    /**
  86.     * Selects the kth minimum value from the array a. This method
  87.     * throws IllegalArgumentException if a is null, has zero length,
  88.     * or if there is no kth minimum value. Note that there is no kth
  89.     * minimum value if k < 1, k > a.length, or if k is larger than
  90.     * the number of distinct values in the array. The array a is not
  91.     * changed by this method.
  92.     */
  93.    
  94.    
  95.    public static int kmin(int[] a, int k) {
  96.      
  97.      
  98.       int distinct = 1;
  99.       if (a == null) {
  100.          throw new IllegalArgumentException("array is null");
  101.       }
  102.      
  103.       if (a.length == 0) {
  104.          throw new IllegalArgumentException("array has length of 0");
  105.       }
  106.       if (k > a.length) {
  107.          throw new IllegalArgumentException("k is greater than the number of"
  108.                                           + " elements in the array");
  109.       }
  110.       if (k < 1) {
  111.          throw new IllegalArgumentException("k is less than 1");
  112.       }
  113.        
  114.       // make a copy of the array
  115.       int[] aCopy = new int[a.length];
  116.      
  117.      
  118.       for (int i = 0; i < a.length; i++) {
  119.          aCopy[i] = a[i];
  120.       }
  121.            
  122.       // sort the array into ascending order
  123.       Arrays.sort(aCopy);
  124.      
  125.       // count number of distinct values
  126.       for (int i = 0; i < a.length - 1; i++) {
  127.          if (!(a[i] == a[i + 1])) {
  128.             distinct++;
  129.          }
  130.       }
  131.      
  132.       // create a new array with only distinct values
  133.       int[] uniqueArr = new int[distinct];
  134.       int j = 0;
  135.      
  136.       for (int i = 0; i < a.length - 1; i++) {
  137.          if (aCopy[i] != aCopy[i + 1]) {
  138.             uniqueArr[j++] = aCopy[i];
  139.          }
  140.          
  141.          
  142.       }
  143.      
  144.       uniqueArr[j++] = aCopy[aCopy.length - 1];
  145.      
  146.        // create a new size array stop when you hit 0
  147.              
  148.                
  149.       if (k > uniqueArr.length) {
  150.          throw new IllegalArgumentException("k is greater than the number"
  151.                                           + " of distinct values in the array");
  152.       }
  153.      
  154.      
  155.       int kminimum = uniqueArr[k - 1];          
  156.      
  157.                    
  158.       return kminimum;
  159.    
  160.    
  161.    }
  162.  
  163.  
  164.    /**
  165.     * Selects the kth maximum value from the array a. This method
  166.     * throws IllegalArgumentException if a is null, has zero length,
  167.     * or if there is no kth maximum value. Note that there is no kth
  168.     * maximum value if k < 1, k > a.length, or if k is larger than
  169.     * the number of distinct values in the array. The array a is not
  170.     * changed by this method.
  171.     */
  172.    public static int kmax(int[] a, int k) {
  173.      
  174.    
  175.       int distinct = 1;
  176.       if (a == null) {
  177.          throw new IllegalArgumentException("array is null");
  178.       }
  179.      
  180.       if (a.length == 0) {
  181.          throw new IllegalArgumentException("array has length of 0");
  182.       }
  183.       if (k > a.length) {
  184.          throw new IllegalArgumentException("k is greater than the number of"
  185.                                           + " elements in the array");
  186.       }
  187.       if (k < 1) {
  188.          throw new IllegalArgumentException("k is less than 1");
  189.       }
  190.        
  191.       // make a copy of the array
  192.       int[] aCopy = new int[a.length];
  193.      
  194.      
  195.       for (int i = 0; i < a.length; i++) {
  196.          aCopy[i] = a[i];
  197.       }
  198.            
  199.       // sort the array into ascending order
  200.       Arrays.sort(aCopy);
  201.      
  202.       // sort the array into descending order
  203.       int last = aCopy.length - 1;
  204.       int middle = aCopy.length / 2;
  205.       for (int i = 0; i <= middle; i++) {
  206.          int temp = aCopy[i];
  207.          aCopy[i] = aCopy[last - i];
  208.          aCopy[last - i] = temp;
  209.          
  210.       }
  211.      
  212.       // count number of distinct values
  213.       for (int i = 0; i < a.length - 1; i++) {
  214.          if (!(a[i] == a[i + 1])) {
  215.             distinct++;
  216.          }
  217.       }
  218.    
  219.      
  220.        // create a new array with only distinct values
  221.       int[] uniqueArr = new int[distinct];
  222.       int j = 0;
  223.      
  224.       for (int i = 0; i < a.length - 1; i++) {
  225.          if (aCopy[i] != aCopy[i + 1]) {
  226.             uniqueArr[j++] = aCopy[i];
  227.          }
  228.          
  229.          
  230.       }
  231.      
  232.       uniqueArr[j++] = aCopy[aCopy.length - 1];
  233.      
  234.                
  235.                
  236.       if (k > uniqueArr.length) {
  237.          throw new IllegalArgumentException("k is greater than the number"
  238.                                           + " of distinct values in the array");
  239.       }
  240.      
  241.      
  242.       int kmaximum = uniqueArr[k - 1];          
  243.      
  244.                    
  245.      
  246.      
  247.    
  248.                    
  249.       return kmaximum;
  250.    }
  251.  
  252.  
  253.    /**
  254.     * Returns an array containing all the values in a in the
  255.     * range [low..high]; that is, all the values that are greater
  256.     * than or equal to low and less than or equal to high,
  257.     * including duplicate values. The length of the returned array
  258.     * is the same as the number of values in the range [low..high].
  259.     * If there are no qualifying values, this method returns a
  260.     * zero-length array. Note that low and high do not have
  261.     * to be actual values in a. This method throws an
  262.     * IllegalArgumentException if a is null or has zero length.
  263.     * The array a is not changed by this method.
  264.     */
  265.    public static int[] range(int[] a, int low, int high) {
  266.      
  267.      
  268.       if (a == null) {
  269.          throw new IllegalArgumentException("array is null");
  270.       }
  271.      
  272.       if (a.length == 0) {
  273.          throw new IllegalArgumentException("array has length of 0");
  274.       }
  275.      
  276.      
  277.       int inc = 0;
  278.    
  279.       for (int i = 0; i < a.length; i++) {
  280.          if ((a[i] >= low) && (a[i] <= high)) {
  281.             inc++;
  282.          }
  283.       }
  284.       int[] range = new int[inc];
  285.       int j = 0;
  286.       for (int k = 0; k < a.length; k++) {
  287.          
  288.          if ((a[k] >= low) && (a[k] <= high)) {
  289.             range[j] = a[k];
  290.             j++;
  291.          }
  292.       }
  293.      
  294.      
  295.       return range;
  296.    }
  297.    
  298.    
  299.  
  300.    /**
  301.     * Returns the smallest value in a that is greater than or equal to
  302.     * the given key. This method throws an IllegalArgumentException if
  303.     * a is null or has zero length, or if there is no qualifying
  304.     * value. Note that key does not have to be an actual value in a.
  305.     * The array a is not changed by this method.
  306.     */
  307.    public static int ceiling(int[] a, int key) {
  308.       int c = -1;
  309.      
  310.       if (a == null) {
  311.          throw new IllegalArgumentException("array is null");
  312.       }
  313.      
  314.       if (a.length == 0) {
  315.          throw new IllegalArgumentException("array has length of 0");
  316.       }
  317.      
  318.       for (int i = 0; i < a.length; i++) {
  319.          if (a[i] >= key) {
  320.             c = a[i];
  321.             break;
  322.          }
  323.          
  324.       }
  325.      
  326.       for (int i = 0; i < a.length; i++) {
  327.          
  328.          if ((a[i] >= key) && (a[i] < c)) {
  329.             c = a[i];
  330.          }
  331.          
  332.       }
  333.      
  334.       if (c == -1) {
  335.          throw new IllegalArgumentException("no qualifying value");
  336.       }
  337.      
  338.       return c;
  339.    
  340.    }
  341.  
  342.  
  343.    /**
  344.     * Returns the largest value in a that is less than or equal to
  345.     * the given key. This method throws an IllegalArgumentException if
  346.     * a is null or has zero length, or if there is no qualifying
  347.     * value. Note that key does not have to be an actual value in a.
  348.     * The array a is not changed by this method.
  349.     */
  350.    public static int floor(int[] a, int key) {
  351.       int f = -1;
  352.      
  353.       if (a == null) {
  354.          throw new IllegalArgumentException("array is null");
  355.       }
  356.      
  357.       if (a.length == 0) {
  358.          throw new IllegalArgumentException("array has length of 0");
  359.       }
  360.    
  361.       for (int i = 0; i < a.length; i++) {
  362.          if (a[i] <= key) {
  363.             f = a[i];
  364.             break;
  365.          }
  366.          
  367.      
  368.       }
  369.          
  370.       for (int i = 0; i < a.length; i++) {
  371.          
  372.          if ((a[i] <= key) && (a[i] > f)) {
  373.             f = a[i];
  374.          }
  375.          
  376.       }
  377.    
  378.       if (f == -1) {
  379.          throw new IllegalArgumentException("no qualifying value");
  380.       }
  381.      
  382.       return f;
  383.    
  384.    }
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement