Advertisement
Guest User

Silvestri Library of Useful Routines V3.0

a guest
Nov 20th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. /*
  2.  * Name: Prof. Antonio C. Silvestri
  3.  * Date: 11/20/17
  4.  * Course Number: CSC-111  
  5.  * Course Name: Intro to Java Programming
  6.  * Email: silvestri@stcc.edu
  7.  * A Library of Useful Routines V3.0
  8.  */
  9.  
  10. package silvestri;
  11.  
  12. public class Library {
  13.  
  14.     public static double versionNo() {
  15.         return 3.0;
  16.     }
  17.  
  18.     public static double fact(int i) {
  19.         double prod = 1;
  20.         for (int j = 1; j <= i; j++)
  21.             prod *= j;
  22.         return prod;
  23.     }
  24.  
  25.     public static boolean isOdd(int x) {
  26.         return x % 2 == 1;
  27.     }
  28.  
  29.     public static boolean isEven(int x) {
  30.         return !isOdd(x);
  31.     }
  32.  
  33.     public static int min(int x, int y) {
  34.         if (x < y)
  35.             return x;
  36.         return y;
  37.     }
  38.  
  39.     public static double min(double x, double y) {
  40.         if (x < y)
  41.             return x;
  42.         return y;
  43.     }
  44.  
  45.     public static int sumDigits(int x) {
  46.         int sum = 0;
  47.         if (x < 0)
  48.             x = -x;
  49.         while (x > 0) {
  50.             int lsd = x % 10;
  51.             sum = sum + lsd;
  52.             x = x / 10;
  53.         }
  54.         return sum;
  55.     }
  56.  
  57.     public static int reverseDigits(int x) {
  58.         int sum = 0;
  59.         int sign = 1;
  60.         if (x < 0) {
  61.             x = -x;
  62.             sign = -1;
  63.         }
  64.         while (x > 0) {
  65.             int lsd = x % 10;
  66.             sum = 10 * sum + lsd;
  67.             x = x / 10;
  68.         }
  69.         return sign * sum;
  70.     }
  71.  
  72.     public static int max(int x, int y) {
  73.         if (x > y)
  74.             return x;
  75.         return y;
  76.     }
  77.  
  78.     public static double max(double x, double y) {
  79.         if (x > y)
  80.             return x;
  81.         return y;
  82.     }
  83.  
  84.     public static boolean isPrime(int number) {
  85.         if (number <= 1)
  86.             return false;
  87.         for (int i = 2; i <= Math.sqrt(number); i++)
  88.             if (number % i == 0)
  89.                 return false;
  90.         return true;
  91.     }
  92.  
  93.     public static int gcd(int m, int n) {
  94.         if (m < 0)
  95.             m = -m;
  96.         if (n < 0)
  97.             n = -n;
  98.  
  99.         int temp = min(m, n);
  100.         for (int i = temp; i > 1; i--) {
  101.             if (m % i == 0 && n % i == 0)
  102.                 return i;
  103.         }
  104.         return 1;
  105.     }
  106.  
  107.     // ****************************************************************************************
  108.     // Linear Search Routine Variants
  109.     // *****************************************************************************************
  110.  
  111.     public static int linearSearch(int data[], int key) {
  112.         return linearSearch(data, key, 0);
  113.     }
  114.  
  115.     public static int linearSearch(int data[], int key, int start) {
  116.         for (int i = start; i < data.length; i++)
  117.             if (data[i] == key)
  118.                 return i;
  119.         return -1;
  120.     }
  121.  
  122.     public static int linearSearch(double data[], double key) {
  123.         return linearSearch(data, key, 0);
  124.     }
  125.  
  126.     public static int linearSearch(double data[], double key, int start) {
  127.         for (int i = start; i < data.length; i++)
  128.             if (data[i] == key)
  129.                 return i;
  130.         return -1;
  131.     }
  132.  
  133.     public static int linearSearch(String data[], String key) {
  134.         return linearSearch(data, key, 0);
  135.     }
  136.  
  137.     public static int linearSearch(String data[], String key, int start) {
  138.         for (int i = start; i < data.length; i++)
  139.             if (data[i].equals(key))
  140.                 return i;
  141.         return -1;
  142.     }
  143.  
  144.     // ****************************************************************************************
  145.     // Selection Sort Routine Variants
  146.     // *****************************************************************************************
  147.  
  148.     public static void selectionSort(double[] list) {
  149.         for (int i = 0; i < list.length - 1; i++) {
  150.             // Find the minimum in the list[i..list.length-1]
  151.             double currentMin = list[i];
  152.             int currentMinIndex = i;
  153.  
  154.             for (int j = i + 1; j < list.length; j++) {
  155.                 if (list[j] < currentMin) {
  156.                     currentMin = list[j];
  157.                     currentMinIndex = j;
  158.                 }
  159.             }
  160.  
  161.             // Swap list[i] with list[currentMinIndex] if necessary;
  162.             if (currentMinIndex != i) {
  163.                 list[currentMinIndex] = list[i];
  164.                 list[i] = currentMin;
  165.             }
  166.         }
  167.     }
  168.  
  169.     public static void selectionSort(int[] list) {
  170.         for (int i = 0; i < list.length - 1; i++) {
  171.             // Find the minimum in the list[i..list.length-1]
  172.             int currentMin = list[i];
  173.             int currentMinIndex = i;
  174.  
  175.             for (int j = i + 1; j < list.length; j++) {
  176.                 if (list[j] < currentMin) {
  177.                     currentMin = list[j];
  178.                     currentMinIndex = j;
  179.                 }
  180.             }
  181.  
  182.             // Swap list[i] with list[currentMinIndex] if necessary;
  183.             if (currentMinIndex != i) {
  184.                 list[currentMinIndex] = list[i];
  185.                 list[i] = currentMin;
  186.             }
  187.         }
  188.     }
  189.  
  190.     public static void selectionSort(String[] list) {
  191.         for (int i = 0; i < list.length - 1; i++) {
  192.             // Find the minimum in the list[i..list.length-1]
  193.             String currentMin = list[i];
  194.             int currentMinIndex = i;
  195.  
  196.             for (int j = i + 1; j < list.length; j++) {
  197.                 if (list[j].compareTo(currentMin) < 0) {
  198.                     currentMin = list[j];
  199.                     currentMinIndex = j;
  200.                 }
  201.             }
  202.  
  203.             // Swap list[i] with list[currentMinIndex] if necessary;
  204.             if (currentMinIndex != i) {
  205.                 list[currentMinIndex] = list[i];
  206.                 list[i] = currentMin;
  207.             }
  208.         }
  209.     }
  210.  
  211.     // ****************************************************************************************
  212.     // BinarySearch Routine Variants
  213.     // *****************************************************************************************
  214.  
  215.     public static int binarySearch(int[] list, int key) {
  216.         int low = 0;
  217.         int high = list.length - 1;
  218.  
  219.         while (high >= low) {
  220.             int mid = (low + high) / 2;
  221.             if (key < list[mid])
  222.                 high = mid - 1;
  223.             else if (key == list[mid])
  224.                 return mid;
  225.             else
  226.                 low = mid + 1;
  227.         }
  228.         return -1; // Now high < low
  229.     }
  230.    
  231.     public static int binarySearch(double[] list, double key) {
  232.         int low = 0;
  233.         int high = list.length - 1;
  234.  
  235.         while (high >= low) {
  236.             int mid = (low + high) / 2;
  237.             if (key < list[mid])
  238.                 high = mid - 1;
  239.             else if (key == list[mid])
  240.                 return mid;
  241.             else
  242.                 low = mid + 1;
  243.         }
  244.         return -1; // Now high < low
  245.     }
  246.    
  247.     public static int binarySearch(String[] list, String key) {
  248.         int low = 0;
  249.         int high = list.length - 1;
  250.  
  251.         while (high >= low) {
  252.             int mid = (low + high) / 2;
  253.             if (key.compareTo(list[mid]) < 0)
  254.                 high = mid - 1;
  255.             else if (key.compareTo(list[mid]) == 0)
  256.                 return mid;
  257.             else
  258.                 low = mid + 1;
  259.         }
  260.         return -1; // Now high < low
  261.     }
  262.    
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement