Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. public class Arrays1
  2. {
  3.     /** Returns a string representation of the passed array.
  4.      *
  5.      * @param A An array.
  6.      * @return A string representation of A.
  7.      */
  8.     public static String toString(int[] A)
  9.     {
  10.         if (A.length == 0) return "[]";
  11.         String s = "[";
  12.         for (int i = 0; i < A.length - 1; i++)
  13.             s = s + A[i] + ", ";
  14.         s =  s + A[A.length - 1] + "]";
  15.         return s;
  16.     }
  17.  
  18.  
  19.     /** Compares 2 int arrays for equality.
  20.      * The arrays are equal if they are of the same length and contain the
  21.      * same elements in the same order. Neither of the passed arrays should
  22.      * be altered.
  23.      *
  24.      * @param a The first array.
  25.      * @param b The second array.
  26.      * @return <code>true<code> if array <em>a</em> equals array <em>b</em>
  27.      *      and <code>false</code> otherwise.
  28.      */
  29.     public static boolean areEqual(int[] a, int[] b)
  30.     {
  31.         if (a.length == b.length)
  32.         {
  33.             for (int i = 0; i < a.length; i++)
  34.             {
  35.                 if (a[i] != b[i])
  36.                     return false;
  37.             }
  38.             return true;
  39.         }          
  40.         else
  41.             return false;
  42.     }
  43.  
  44.     /**
  45.      * Returns an array consisting of all the odd elements of the passed array.
  46.      * The returned array must be "full", i.e. no longer than it needs to be
  47.      * to hold all the odd elements of the passed array.  For example, if the
  48.      * passed array a is [3,3,4,7,8,2,1] then the array [3,3,7,1] is
  49.      * returned. The passed array should not be altered.
  50.      *
  51.      * @param a An array.
  52.      * @return An array holding all the odd elements of the passed array.
  53.      */
  54.     public static int[] oddElements(int[] a)
  55.     {
  56.         // Get needed length of the new array
  57.         int arrayLength = 0;
  58.         for (int i = 0; i < a.length; i++)
  59.         {
  60.             if (a[i] % 2 != 0)
  61.                 arrayLength++;
  62.         }
  63.         // Make new array
  64.         int[] array = new int[arrayLength];
  65.         int count = 0;
  66.         for (int i = 0; i < a.length; i++)
  67.         {
  68.             if (a[i] % 2 != 0)
  69.             {
  70.                 array[count++] = a[i];
  71.             }
  72.         }
  73.         return array;
  74.     }
  75.  
  76.     /**
  77.      * Returns the length of the longest increasing subsequence of the passed array.
  78.      * For example, if the passed array is [3,7,2,7,11,22,8,12,13], the longest
  79.      * increasing subsequence is 2,7,11,22, so the return value is 4.  The passed
  80.      * array should not be changed.
  81.      *
  82.      * @param A The passed array.
  83.      * @return The length of the longest increasing subsequence in A.
  84.      */
  85.     public static int longestRun(int[] A)
  86.     {
  87.             int currentLongest = 0;
  88.             int counter = 0;
  89.             for (int i = 1; i < A.length; i++)
  90.             {
  91.                 if (A[i] >= A[i-1])
  92.                 {
  93.                     counter++;
  94.                    
  95.                     if (counter > currentLongest)
  96.                         currentLongest = counter;
  97.                 }      
  98.                 else
  99.                     counter = 0;
  100.             }
  101.             return currentLongest+1;
  102.     }
  103.  
  104.     /**
  105.      * Returns the sorted array that contains all the elements of the two passed
  106.      * sorted arrays.  For example, if the passed arrays are [2,3,7,9] and
  107.      * [1,3,5,7,12], the array returned is [1,2,3,3,5,7,7,9,12]. Neither of the
  108.      * passed arrays should be changed.
  109.      *
  110.      * @param A An array.
  111.      * @param B An array.
  112.      * @return An array consisting of all the elements of A and B sorted in
  113.      * non-decreasing order.
  114.      * @pre. The arrays A and B are sorted in non-decreasing order.
  115.      */
  116.     public static int[] merge(int[] A, int[] B)
  117.     {
  118.         int[] merged = new int[A.length+B.length];
  119.         int posA = 0;
  120.         int posB = 0;
  121.         int posR = 0;
  122.         int[] remaining;
  123.        
  124.         while(posA < A.length && posB < B.length) {
  125.         if(A[posA] < B[posB])
  126.             merged[posR++] = A[posA++];
  127.         else
  128.             merged[posR++] = B[posB++];
  129.         }
  130.        
  131.         if(posA == A.length) {
  132.           posR = posB;
  133.          remaining = B;
  134.         }
  135.         else {
  136.           posR = posA;    
  137.           remaining = A;
  138.         }
  139.              
  140.         while (posR < remaining.length) {
  141.           merged[posR++] = remaining[posR++];
  142.         }
  143.        
  144.         return(merged);
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement