Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Arrays1
- {
- /** Returns a string representation of the passed array.
- *
- * @param A An array.
- * @return A string representation of A.
- */
- public static String toString(int[] A)
- {
- if (A.length == 0) return "[]";
- String s = "[";
- for (int i = 0; i < A.length - 1; i++)
- s = s + A[i] + ", ";
- s = s + A[A.length - 1] + "]";
- return s;
- }
- /** Compares 2 int arrays for equality.
- * The arrays are equal if they are of the same length and contain the
- * same elements in the same order. Neither of the passed arrays should
- * be altered.
- *
- * @param a The first array.
- * @param b The second array.
- * @return <code>true<code> if array <em>a</em> equals array <em>b</em>
- * and <code>false</code> otherwise.
- */
- public static boolean areEqual(int[] a, int[] b)
- {
- if (a.length == b.length)
- {
- for (int i = 0; i < a.length; i++)
- {
- if (a[i] != b[i])
- return false;
- }
- return true;
- }
- else
- return false;
- }
- /**
- * Returns an array consisting of all the odd elements of the passed array.
- * The returned array must be "full", i.e. no longer than it needs to be
- * to hold all the odd elements of the passed array. For example, if the
- * passed array a is [3,3,4,7,8,2,1] then the array [3,3,7,1] is
- * returned. The passed array should not be altered.
- *
- * @param a An array.
- * @return An array holding all the odd elements of the passed array.
- */
- public static int[] oddElements(int[] a)
- {
- // Get needed length of the new array
- int arrayLength = 0;
- for (int i = 0; i < a.length; i++)
- {
- if (a[i] % 2 != 0)
- arrayLength++;
- }
- // Make new array
- int[] array = new int[arrayLength];
- int count = 0;
- for (int i = 0; i < a.length; i++)
- {
- if (a[i] % 2 != 0)
- {
- array[count++] = a[i];
- }
- }
- return array;
- }
- /**
- * Returns the length of the longest increasing subsequence of the passed array.
- * For example, if the passed array is [3,7,2,7,11,22,8,12,13], the longest
- * increasing subsequence is 2,7,11,22, so the return value is 4. The passed
- * array should not be changed.
- *
- * @param A The passed array.
- * @return The length of the longest increasing subsequence in A.
- */
- public static int longestRun(int[] A)
- {
- int currentLongest = 0;
- int counter = 0;
- for (int i = 1; i < A.length; i++)
- {
- if (A[i] >= A[i-1])
- {
- counter++;
- if (counter > currentLongest)
- currentLongest = counter;
- }
- else
- counter = 0;
- }
- return currentLongest+1;
- }
- /**
- * Returns the sorted array that contains all the elements of the two passed
- * sorted arrays. For example, if the passed arrays are [2,3,7,9] and
- * [1,3,5,7,12], the array returned is [1,2,3,3,5,7,7,9,12]. Neither of the
- * passed arrays should be changed.
- *
- * @param A An array.
- * @param B An array.
- * @return An array consisting of all the elements of A and B sorted in
- * non-decreasing order.
- * @pre. The arrays A and B are sorted in non-decreasing order.
- */
- public static int[] merge(int[] A, int[] B)
- {
- int[] merged = new int[A.length+B.length];
- int posA = 0;
- int posB = 0;
- int posR = 0;
- int[] remaining;
- while(posA < A.length && posB < B.length) {
- if(A[posA] < B[posB])
- merged[posR++] = A[posA++];
- else
- merged[posR++] = B[posB++];
- }
- if(posA == A.length) {
- posR = posB;
- remaining = B;
- }
- else {
- posR = posA;
- remaining = A;
- }
- while (posR < remaining.length) {
- merged[posR++] = remaining[posR++];
- }
- return(merged);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement