This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

ArrayUtil

By: a guest on Jan 23rd, 2013  |  syntax: None  |  size: 4.32 KB  |  views: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * Name:
  3.  *
  4.  * Date:
  5.  */
  6.  
  7.  
  8.  
  9. public class ArrayUtil
  10. {
  11.  
  12.     /**
  13.      *Precondition: length > 0
  14.      *Postconditon: Creates an array filled with 'length'
  15.      *            number of random values between 1 and n.
  16.      *@param length: the length of the array
  17.      *@param n: the number of possible random values
  18.      *@return an array filled with 'length' numbers between 1 and n.
  19.      */
  20.         public static int[] randomIntArray(int length, int n)
  21.      {
  22.          int[] ans = new int[length];
  23.          for (int i = 0; i < length; i++)
  24.          {
  25.                  ans[i] = (int) (Math.random() * n + 1);
  26.          }
  27.          return ans;
  28.        
  29.      }
  30.  
  31.  
  32.      /**
  33.        *Precondition: 0 <= start <= end < nums.length
  34.        *Postcondition: Returns a String of the values of
  35.        *the array from start to end inclusive.
  36.        *Print 15 values per line with a newline after all values
  37.        *@param start: the first position to print
  38.        *@param end: the last position to print.
  39.        *@return a String of the values in nums from start to end.
  40.        */
  41.       public static String printArray(int start, int end, int[] nums)
  42.       {
  43.           String ans = "";
  44.           for (int i = start; i <= end; i++)
  45.           {
  46.                   if (i % 15 == 0 && i != 0)
  47.                           ans += "\n";
  48.                   ans += nums[i] + " ";
  49.                  
  50.           }
  51.           return ans;
  52.       }
  53.  
  54.     /**
  55.      *Precondition: 0 <= from < nums.length
  56.      *Postcondition: Finds the smallest element in nums in the
  57.      *tail range of the array.
  58.      *@param from: the first position in 'nums' to compare
  59.      *@return the position of the smallest element in the
  60.      *range 'from' to 'nums.length-1'.
  61.      */
  62.      public static int minimumPosition(int from, int[] nums)
  63.      {
  64.         int minp = from;
  65.         for (int i = from + 1; i < nums.length; i++)
  66.         {
  67.                 if (nums[i] < nums[minp])
  68.                         minp = i;
  69.         }
  70.         return minp;
  71.      }
  72.  
  73.  
  74.      /**
  75.       *Precondition: 0 <= i < a.length, 0 <= j < a.length
  76.       *Postcondition: Swaps two entries of the array.
  77.       *@param i: the first position to swap
  78.       *@param j: the second position to swap
  79.       *@return a with indexes i and j swapped
  80.       */
  81.       public static int[] swap(int i, int j, int[] a)
  82.       {
  83.           int hold = a[i];
  84.           a[i] = a[j];
  85.           a[j] = hold;
  86.           return a;
  87.                        
  88.       }
  89.      
  90.       /*Precondition: 0 <= start <= end < nums.length
  91.                 Postcondition: shift all values in nums right one
  92.                                from start to end
  93.                 *@return nums with all values from start to
  94.                 * to end shifted one index higher. keep all other
  95.                 * values in nums the same
  96.                 */
  97.                 public static int[] shiftRight(int start, int end, int[] nums)
  98.                 {
  99.                         for (int i = end; i > start; i--)
  100.                         {
  101.                                 nums[i] = nums[i-1];
  102.                         }
  103.                         return nums;
  104.                 }
  105.  
  106.  
  107.      /**
  108.        *Precondition: a.length > 0 && a is sorted in ascending order
  109.        *Postcondition: Returns the index where insertNum could be placed
  110.        *to keep the array a in ascending sorted order.
  111.        *@param insertNum: the number to insert into a.
  112.        *@return: An index of a.  insertNum could be inserted @return to keep
  113.        *a in ascending sorted order.
  114.        */
  115.       public static int findIndexToInsert(int insertNum, int[] a)
  116.       {
  117.           for (int i = 0; i < a.length; i++)
  118.           {
  119.                   if (insertNum < a[i])
  120.                           return i;
  121.           }
  122.           return -1;
  123.       }
  124.  
  125.      /**
  126.        *Precondition: a.length > 0 && a is sorted in ascending order
  127.        *Postcondition: Insert insertNum into the sorted array into the
  128.        *correct position to keep a sorted in ascending order. This will
  129.        *increase the length of the a array by 1. All elements greater
  130.        *than insertNum will move to index + 1. Hint: You will need to create
  131.        *a new array one bigger than a. Copy all the old values of a
  132.        *into the new array with inserting insertNum.  
  133.         *You must include calls to findIndexToInsert.
  134.        *@param insertNum: the number to insert into a.
  135.        *@return the array in ascending order with insertNum
  136.        */
  137.       public static int[] insertSorted(int insertNum, int[] a)
  138.       {
  139.           int[] b = new int[a.length + 1];
  140.           b = a;
  141.           int index = findIndexToInsert(insertNum, a);
  142.           shiftRight(index, b.length - 1, b);
  143.           b[index] = insertNum;
  144.           return b;
  145.       }
clone this paste RAW Paste Data