Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: Java | Size: 2.46 KB | Hits: 7 | Expires: Never
Copy text to clipboard
  1. package lab2;
  2.  
  3. import java.util.Random;
  4.  
  5. /**
  6.  *
  7.  * @author James Valente
  8.  */
  9. public class Randmax {
  10.     /*Because every java app needs main()*/
  11.  
  12.     /**
  13.      *
  14.      * @param args
  15.      */
  16.     public static void main(String[] args) {
  17.         long startTime, endTime;
  18.         int[] dummyArray;
  19.         int maximum;
  20.         double bigO;
  21.         int sizes[] = {10, 100, 500, 1000, 5000, 10000, 50000, 100000, 1000000, 2000000, 5000000, 10000000};
  22.         /*Time for output*/
  23.         System.out.printf("%-10s   %-8s   %-8s   %-8s\n", "Size", "Max", "Runtime (ns)", "Big-O"); //Header part 1
  24.         System.out.printf("%-10s   %-8s   %-8s   %-8s\n", "--------", "--------", "-----------", "--------"); //Header part 2
  25.         String specify = "%-10s   %-8d   %-8d ns   %-8f\n"; //Output format specifer.
  26.         /*Now begins the point at which I must output each max and its runtime.*/
  27.         for (int i = 0; i < sizes.length; i++) {
  28.             dummyArray = populate(sizes[i]);
  29.             startTime = System.nanoTime();
  30.             maximum = findMax(dummyArray);
  31.             endTime = System.nanoTime() - startTime;
  32.             bigO = (double)endTime/(double)sizes[i];
  33.             System.out.printf(specify, sizes[i], maximum, endTime, bigO);
  34.         }
  35.     }
  36.     /*Some method that most likely exists as a java builtin.*/
  37.  
  38.     /**
  39.      *
  40.      * @param arr Array to find max
  41.      * @return  Max value of array
  42.      */
  43.     public static int findMax(int[] arr) {
  44.         int max = arr[0]; /*Value was zero
  45.          * Negatives could cause issues
  46.          * Here's my fix for that
  47.          */
  48.         for (int i = 1; i < arr.length; i++) {
  49.             if (arr[i] > max) {
  50.                 max = arr[i]; //Highlander style: there can be only one.
  51.             }
  52.         }
  53.         return max;
  54.     }
  55.     /*With 9+ arrays being populated, creating a function is much more efficient*/
  56.  
  57.     /**
  58.      *
  59.      * @param n Size of Array
  60.      * @return Randomly populated array of size n
  61.      */
  62.     public static int[] populate(int n) {
  63.         int array[] = new int[n];
  64.         Random randGen = new Random(); //Yay OOP
  65.         int seed = 10000000; //uh sure
  66.         for (int i = 0; i < array.length; i++) {
  67.             int phobos = randGen.nextInt(seed); //Okay make a random
  68.             int deimos = randGen.nextInt(seed / 2); //You too, pick a random.
  69.             array[i] = deimos - phobos; //Do mathy things to be SUPER RANDOM
  70.         }
  71.         return array;
  72.     }
  73. }