Write a recursive, void method , reverse, that accepts an integer array , a starting index and an ending index, and reverses the array.

1
Write a recursive, void method , reverse, that accepts an integer  array , a starting index and an ending index, and reverses the array.

LANGUAGE: JAVA

CHALLENGE:

Write a recursive, void method , reverse, that accepts an integer array, a starting index and an ending index, and reverses the array. Reversing an array involves:
• Nothing if the array has 0 or 1 elements .
• swapping the first and last elements of the array and then reversing the remainder of the array (2nd through next-to-last elements ).

SOLUTION:

public void reverse(int a[],int start,int stop)
{
    int temp;
    if(start==stop)
         return;
    else
    {
        if(stop>start)
        {
            temp=a[start];
            a[start]=a[stop];
            a[stop]=temp;
            reverse(a,(start+1),(stop-1));
        }
    else
        return;
    }
}

One comment

Add comment