public static int[] merge ( int[] arrayA, int[] arrayB ) { // Create an array C that will hold all the elements in arrays A and B. int[] arrayC = new int [ arrayA.length + arrayB.length ]; // Create pointers that will traverse the arrays. The pointers are // really pointing to the index of the array element that we are visiting int ptrA = 0; int ptrB = 0; int ptrC = 0; // Compare elements in each array A and B and write out the elements in // array C while ( ( ptrA < arrayA.length ) && ( ptrB < arrayB.length ) ) { if ( arrayA [ptrA] < arrayB [ptrB] ) { arrayC [ptrC++] = arrayA [ptrA++]; } else { arrayC [ptrC++] = arrayB [ptrB++]; } } // Write out array A if it is not empty while ( ptrA < arrayA.length ) { arrayC [ptrC++] = arrayA [ptrA++]; } // Write out array B if it is not empty while ( ptrB < arrayB.length ) { arrayC [ptrC++] = arrayB [ptrB++]; } return arrayC; }
import java.util.*; public class TestArray { // This method prints an array public static void printArray ( int[] anArray ) { for ( int i = 0; i < anArray.length; i++ ) System.out.print ( anArray[i] + " " ); System.out.println (); } public static void main ( String [] args ) { // Create an array and print it int[] intArray = { 87, 23, 67, 46, 91, 52 }; printArray ( intArray ); // Sort the array and print it Arrays.sort ( intArray ); printArray ( intArray ); // Search for an item in the array int x = 46; int i = Arrays.binarySearch ( intArray, x ); System.out.println ( "The number " + x + " is at " + i ); // Search for an item not in the array x = 73; i = Arrays.binarySearch ( intArray, x ); System.out.println ( "The number " + x + " is at " + i ); // Create another array and test if they are equal int[] intArray2 = { 87, 67, 23, 46, 91, 52 }; if ( Arrays.equals ( intArray, intArray2 ) ) System.out.println ( "The arrays are equal."); else System.out.println ( "The arrays are not equal." ); // Print the array, fill it with a specified value and print it // again to verify printArray ( intArray2 ); Arrays.fill ( intArray2, -1 ); printArray ( intArray2 ); } }
Recursion is based on two key problem solving concepts: divide and conquer and self-similarity. A recursive solution solves a problem by solving a smaller instance of the same problem. It solves this new problem by solving an even smaller instance of the same problem. Eventually, the new problem will be so small that its solution will either be obvious or known. This solution will lead to the solution of the original problem.
A recursive definition consists of two part: a recursive part in which the nth value is defined in terms of the (n-1)th value, and a non recursive boundary case or base case which defines a limiting condition. An infinite repetition will result if a recursive definition is not properly bounded. In a recursive algorithm, each recursive call must make progress toward the bound, or base case. A recursion parameter is a parameter whose value is used to control the progress of the recursion towards its bound.
Procedure call and return in Java uses a last-in-first-out protocol. As each method call is made, a representation of the method call is place on the method call stack. When a method returns, its block is removed from the top of the stack. Use an iterative algorithm instead of a recursive algorithm whenever efficiency and memory usage are important design factors. When all other factors are equal, choose the algorithm (recursive or iterative) that is easiest to understand, develop, and maintain.
Here is an example of a recursive method that calculates the factorial of n. The base case occurs when n is equal to 0. We know that 0! is equal to 1. Otherwise we use the relationship n! = n * ( n - 1 )!
public static long fact ( int n ) { if ( n == 0 ) return 1; else return n * fact ( n - 1 ); }
The best way to learn recursion is through practice. Here are some exercises on recursion to get you started. Work through the exercise before looking at the answer.
public static long fib ( int n ) { if ( n <= 1 ) return n; else return fib(n-1) + fib(n-2); }