Advertisement
Guest User

Selector.java

a guest
Jan 16th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.49 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.       // create a new array with only distinct values
  126.       int[] uniqueArr = new int[a.length];
  127.       int j = 0;
  128.      
  129.       for (int i = 0; i < a.length - 1; i++) {
  130.          if (aCopy[i] != aCopy[i + 1]) {
  131.             uniqueArr[j++] = aCopy[i];
  132.          }
  133.          
  134.          
  135.       }
  136.      
  137.       uniqueArr[j++] = aCopy[aCopy.length - 1];
  138.      
  139.                
  140.                
  141.       if (k > uniqueArr.length) {
  142.          throw new IllegalArgumentException("k is greater than the number"
  143.                                           + " of distinct values in the array");
  144.       }
  145.      
  146.      
  147.       int kminimum = uniqueArr[k - 1];          
  148.      
  149.                    
  150.       return kminimum;
  151.    
  152.    
  153.    }
  154.  
  155.  
  156.    /**
  157.     * Selects the kth maximum value from the array a. This method
  158.     * throws IllegalArgumentException if a is null, has zero length,
  159.     * or if there is no kth maximum value. Note that there is no kth
  160.     * maximum value if k < 1, k > a.length, or if k is larger than
  161.     * the number of distinct values in the array. The array a is not
  162.     * changed by this method.
  163.     */
  164.    public static int kmax(int[] a, int k) {
  165.      
  166.    
  167.       int distinct = 1;
  168.       if (a == null) {
  169.          throw new IllegalArgumentException("array is null");
  170.       }
  171.      
  172.       if (a.length == 0) {
  173.          throw new IllegalArgumentException("array has length of 0");
  174.       }
  175.       if (k > a.length) {
  176.          throw new IllegalArgumentException("k is greater than the number of"
  177.                                           + " elements in the array");
  178.       }
  179.       if (k < 1) {
  180.          throw new IllegalArgumentException("k is less than 1");
  181.       }
  182.        
  183.       // make a copy of the array
  184.       int[] aCopy = new int[a.length];
  185.      
  186.      
  187.       for (int i = 0; i < a.length; i++) {
  188.          aCopy[i] = a[i];
  189.       }
  190.            
  191.       // sort the array into ascending order
  192.       Arrays.sort(aCopy);
  193.      
  194.       // sort the array into descending order
  195.       int last = aCopy.length - 1;
  196.       int middle = aCopy.length / 2;
  197.       for (int i = 0; i <= middle; i++) {
  198.          int temp = aCopy[i];
  199.          aCopy[i] = aCopy[last - i];
  200.          aCopy[last - i] = temp;
  201.          
  202.       }
  203.      
  204.        // create a new array with only distinct values
  205.       int[] uniqueArr = new int[a.length];
  206.       int j = 0;
  207.      
  208.       for (int i = 0; i < a.length - 1; i++) {
  209.          if (aCopy[i] != aCopy[i + 1]) {
  210.             uniqueArr[j++] = aCopy[i];
  211.          }
  212.          
  213.          
  214.       }
  215.      
  216.       uniqueArr[j++] = aCopy[aCopy.length - 1];
  217.      
  218.                
  219.                
  220.       if (k > uniqueArr.length) {
  221.          throw new IllegalArgumentException("k is greater than the number"
  222.                                           + " of distinct values in the array");
  223.       }
  224.      
  225.      
  226.       int kmaximum = uniqueArr[k - 1];          
  227.      
  228.                    
  229.      
  230.      
  231.    
  232.                    
  233.       return kmaximum;
  234.    }
  235.  
  236.  
  237.    /**
  238.     * Returns an array containing all the values in a in the
  239.     * range [low..high]; that is, all the values that are greater
  240.     * than or equal to low and less than or equal to high,
  241.     * including duplicate values. The length of the returned array
  242.     * is the same as the number of values in the range [low..high].
  243.     * If there are no qualifying values, this method returns a
  244.     * zero-length array. Note that low and high do not have
  245.     * to be actual values in a. This method throws an
  246.     * IllegalArgumentException if a is null or has zero length.
  247.     * The array a is not changed by this method.
  248.     */
  249.    public static int[] range(int[] a, int low, int high) {
  250.      
  251.      
  252.       if (a == null) {
  253.          throw new IllegalArgumentException("array is null");
  254.       }
  255.      
  256.       if (a.length == 0) {
  257.          throw new IllegalArgumentException("array has length of 0");
  258.       }
  259.      
  260.      
  261.       int inc = 0;
  262.    
  263.       for (int i = 0; i < a.length; i++) {
  264.          if ((a[i] >= low) && (a[i] <= high)) {
  265.             inc++;
  266.          }
  267.       }
  268.       int[] range = new int[inc];
  269.       int j = 0;
  270.       for (int k = 0; k < a.length; k++) {
  271.          
  272.          if ((a[k] >= low) && (a[k] <= high)) {
  273.             range[j] = a[k];
  274.             j++;
  275.          }
  276.       }
  277.      
  278.      
  279.       return range;
  280.    }
  281.    
  282.    
  283.  
  284.    /**
  285.     * Returns the smallest value in a that is greater than or equal to
  286.     * the given key. This method throws an IllegalArgumentException if
  287.     * a is null or has zero length, or if there is no qualifying
  288.     * value. Note that key does not have to be an actual value in a.
  289.     * The array a is not changed by this method.
  290.     */
  291.    public static int ceiling(int[] a, int key) {
  292.       int c = -1;
  293.      
  294.       if (a == null) {
  295.          throw new IllegalArgumentException("array is null");
  296.       }
  297.      
  298.       if (a.length == 0) {
  299.          throw new IllegalArgumentException("array has length of 0");
  300.       }
  301.      
  302.       for (int i = 0; i < a.length; i++) {
  303.          if (a[i] >= key) {
  304.             c = a[i];
  305.             break;
  306.          }
  307.          
  308.       }
  309.      
  310.       for (int i = 0; i < a.length; i++) {
  311.          
  312.          if ((a[i] >= key) && (a[i] < c)) {
  313.             c = a[i];
  314.          }
  315.          
  316.       }
  317.      
  318.       if (c == -1) {
  319.          throw new IllegalArgumentException("no qualifying value");
  320.       }
  321.      
  322.       return c;
  323.    
  324.    }
  325.  
  326.  
  327.    /**
  328.     * Returns the largest value in a that is less than or equal to
  329.     * the given key. This method throws an IllegalArgumentException if
  330.     * a is null or has zero length, or if there is no qualifying
  331.     * value. Note that key does not have to be an actual value in a.
  332.     * The array a is not changed by this method.
  333.     */
  334.    public static int floor(int[] a, int key) {
  335.       int f = -1;
  336.      
  337.       if (a == null) {
  338.          throw new IllegalArgumentException("array is null");
  339.       }
  340.      
  341.       if (a.length == 0) {
  342.          throw new IllegalArgumentException("array has length of 0");
  343.       }
  344.    
  345.       for (int i = 0; i < a.length; i++) {
  346.          if (a[i] <= key) {
  347.             f = a[i];
  348.             break;
  349.          }
  350.          
  351.      
  352.       }
  353.          
  354.       for (int i = 0; i < a.length; i++) {
  355.          
  356.          if ((a[i] <= key) && (a[i] > f)) {
  357.             f = a[i];
  358.          }
  359.          
  360.       }
  361.    
  362.       if (f == -1) {
  363.          throw new IllegalArgumentException("no qualifying value");
  364.       }
  365.      
  366.       return f;
  367.    
  368.    }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement