Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Prof. Antonio C. Silvestri
- * Date: 11/20/17
- * Course Number: CSC-111
- * Course Name: Intro to Java Programming
- * Email: silvestri@stcc.edu
- * A Library of Useful Routines V3.0
- */
- package silvestri;
- public class Library {
- public static double versionNo() {
- return 3.0;
- }
- public static double fact(int i) {
- double prod = 1;
- for (int j = 1; j <= i; j++)
- prod *= j;
- return prod;
- }
- public static boolean isOdd(int x) {
- return x % 2 == 1;
- }
- public static boolean isEven(int x) {
- return !isOdd(x);
- }
- public static int min(int x, int y) {
- if (x < y)
- return x;
- return y;
- }
- public static double min(double x, double y) {
- if (x < y)
- return x;
- return y;
- }
- public static int sumDigits(int x) {
- int sum = 0;
- if (x < 0)
- x = -x;
- while (x > 0) {
- int lsd = x % 10;
- sum = sum + lsd;
- x = x / 10;
- }
- return sum;
- }
- public static int reverseDigits(int x) {
- int sum = 0;
- int sign = 1;
- if (x < 0) {
- x = -x;
- sign = -1;
- }
- while (x > 0) {
- int lsd = x % 10;
- sum = 10 * sum + lsd;
- x = x / 10;
- }
- return sign * sum;
- }
- public static int max(int x, int y) {
- if (x > y)
- return x;
- return y;
- }
- public static double max(double x, double y) {
- if (x > y)
- return x;
- return y;
- }
- public static boolean isPrime(int number) {
- if (number <= 1)
- return false;
- for (int i = 2; i <= Math.sqrt(number); i++)
- if (number % i == 0)
- return false;
- return true;
- }
- public static int gcd(int m, int n) {
- if (m < 0)
- m = -m;
- if (n < 0)
- n = -n;
- int temp = min(m, n);
- for (int i = temp; i > 1; i--) {
- if (m % i == 0 && n % i == 0)
- return i;
- }
- return 1;
- }
- // ****************************************************************************************
- // Linear Search Routine Variants
- // *****************************************************************************************
- public static int linearSearch(int data[], int key) {
- return linearSearch(data, key, 0);
- }
- public static int linearSearch(int data[], int key, int start) {
- for (int i = start; i < data.length; i++)
- if (data[i] == key)
- return i;
- return -1;
- }
- public static int linearSearch(double data[], double key) {
- return linearSearch(data, key, 0);
- }
- public static int linearSearch(double data[], double key, int start) {
- for (int i = start; i < data.length; i++)
- if (data[i] == key)
- return i;
- return -1;
- }
- public static int linearSearch(String data[], String key) {
- return linearSearch(data, key, 0);
- }
- public static int linearSearch(String data[], String key, int start) {
- for (int i = start; i < data.length; i++)
- if (data[i].equals(key))
- return i;
- return -1;
- }
- // ****************************************************************************************
- // Selection Sort Routine Variants
- // *****************************************************************************************
- public static void selectionSort(double[] list) {
- for (int i = 0; i < list.length - 1; i++) {
- // Find the minimum in the list[i..list.length-1]
- double currentMin = list[i];
- int currentMinIndex = i;
- for (int j = i + 1; j < list.length; j++) {
- if (list[j] < currentMin) {
- currentMin = list[j];
- currentMinIndex = j;
- }
- }
- // Swap list[i] with list[currentMinIndex] if necessary;
- if (currentMinIndex != i) {
- list[currentMinIndex] = list[i];
- list[i] = currentMin;
- }
- }
- }
- public static void selectionSort(int[] list) {
- for (int i = 0; i < list.length - 1; i++) {
- // Find the minimum in the list[i..list.length-1]
- int currentMin = list[i];
- int currentMinIndex = i;
- for (int j = i + 1; j < list.length; j++) {
- if (list[j] < currentMin) {
- currentMin = list[j];
- currentMinIndex = j;
- }
- }
- // Swap list[i] with list[currentMinIndex] if necessary;
- if (currentMinIndex != i) {
- list[currentMinIndex] = list[i];
- list[i] = currentMin;
- }
- }
- }
- public static void selectionSort(String[] list) {
- for (int i = 0; i < list.length - 1; i++) {
- // Find the minimum in the list[i..list.length-1]
- String currentMin = list[i];
- int currentMinIndex = i;
- for (int j = i + 1; j < list.length; j++) {
- if (list[j].compareTo(currentMin) < 0) {
- currentMin = list[j];
- currentMinIndex = j;
- }
- }
- // Swap list[i] with list[currentMinIndex] if necessary;
- if (currentMinIndex != i) {
- list[currentMinIndex] = list[i];
- list[i] = currentMin;
- }
- }
- }
- // ****************************************************************************************
- // BinarySearch Routine Variants
- // *****************************************************************************************
- public static int binarySearch(int[] list, int key) {
- int low = 0;
- int high = list.length - 1;
- while (high >= low) {
- int mid = (low + high) / 2;
- if (key < list[mid])
- high = mid - 1;
- else if (key == list[mid])
- return mid;
- else
- low = mid + 1;
- }
- return -1; // Now high < low
- }
- public static int binarySearch(double[] list, double key) {
- int low = 0;
- int high = list.length - 1;
- while (high >= low) {
- int mid = (low + high) / 2;
- if (key < list[mid])
- high = mid - 1;
- else if (key == list[mid])
- return mid;
- else
- low = mid + 1;
- }
- return -1; // Now high < low
- }
- public static int binarySearch(String[] list, String key) {
- int low = 0;
- int high = list.length - 1;
- while (high >= low) {
- int mid = (low + high) / 2;
- if (key.compareTo(list[mid]) < 0)
- high = mid - 1;
- else if (key.compareTo(list[mid]) == 0)
- return mid;
- else
- low = mid + 1;
- }
- return -1; // Now high < low
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement