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

testing k complementary pairs algorithm with junit

Find the first occurence of number in the sorted array

Kadane’s algorithm in C – Dynamic Programming

Changing decimal number to its binary equivalent

Check if two strings are anagrams or not

String Ordered Permutation Algorithm Problem

Find longest palindrom from sequence of characters

Check if there are three numbers a, b, c giving a total T from array A

find longest word in the sentence

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

Leave a Reply

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

*
*