Giter Site home page Giter Site logo

recursion_cpp's Introduction

Recursion CPP:

Reversing an array using recursion in c++

This is a functional recursive code for reversing an array in C++. Recursion is used when we want a function to return a value or perform a repeated task. This type of recursion is commonly used in dynamic programming.

Two Pointers Approach to Reverse an Array

The two-pointers approach is an efficient way to reverse an array by utilizing two indices that start from the opposite ends of the array and move towards the center. This method is simple and works in-place, meaning it requires no extra space apart from the input array.

Explanation

Consider an array: [1, 2, 3, 4, 5].

We use two pointers:

  • start pointer, which starts at the beginning of the array.
  • end pointer, which starts at the end of the array.

The idea is to swap the elements at the start and end pointers and then move these pointers towards the center. This process is repeated until the start pointer is no longer less than the end pointer.

Step-by-Step Example

Initial array:

1 2 3 4 5
start end
  1. Swap arr[start] and arr[end]:
5 2 3 4 1
start end
  1. Move start pointer to the right and end pointer to the left:
5 2 3 4 1
start end
  1. Swap arr[start] and arr[end]:
5 4 3 2 1
start end
  1. Move start pointer to the right and end pointer to the left:
5 4 3 2 1
start, end
  1. Now, start is no longer less than end, so the process stops.

Recursive Implementation

Here's a C++ implementation of this approach using recursion:

void reverseArr(int arr[], int start, int end){
    if(start < end){
        swap(arr[start], arr[end]);
        reverseArr(arr, start+1, end-1);
    }
    else return;
}

recursion_cpp's People

Contributors

ritisha8 avatar sahilwep avatar

Watchers

 avatar

Forkers

sahilwep

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.