Array reversal in Recurrsion

Array reversal in Recurrsion

This problem can be performed in o(n) with normal iterative approach.
Here is vanilla flavor of its implementation in recursion

#include 

/**
 * Recursion version of array swap
 *
 * Kaleb Woldearegay
 */

void reverse(int*arr, int i, int size);
void print(int*arr, int size);

void main()
{
    int nums[] = {4,5,2,13,0}, i=0, size=5;
    i = 0;
    print(nums, 5);
    reverse(nums, i, size-1);
    print(nums, 5);
}
void reverse(int*stk, int i, int size)
{
    if (i>=size)
    {
        return;
    }
    else
    {
        int tmp = stk[i];
        reverse(stk, i+1, size-1);
        stk[i] = stk[size];
        stk[size] = tmp;
    }
}

void print(int*nums, int size)
{
    int i=0;
    while (i<size)
    {
        printf("%in", nums[i]);
        i++;
    }
}

See how you would solve these known algorithm problems

Java solution for checking anagram strings – tell if phrases are anagrams

Changing decimal number to its binary equivalent

testing k complementary pairs algorithm with junit

binary tree problems with solution

Kadane’s algorithm in C – Dynamic Programming

Find longest palindrom from sequence of characters

Find the pairs that makes K Complementary in the given array java solution

Find K Complementary numbers from array Java implementation

String Ordered Permutation Algorithm Problem

Finding missing numbers from billion sequential number list file

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*