This project demonstrates the implementation of classic recursive algorithms in JavaScript, including Fibonacci sequence generation and Merge Sort. Both recursive and iterative approaches are explored to highlight the power and trade-offs of recursion.
-
fibonacci.js
Implements functions to compute the Fibonacci sequence using both iteration and recursion. -
mergeSort.js
Implements the Merge Sort algorithm recursively.
git clone https://github.com/your-username/recursion-algorithms.git
cd recursion-algorithm
You can run the files directly with Node.js:
node fibonacci.js
node mergeSort.js
For automatic restarts on file changes, use nodemon:
nodemon fibonacci.js
Stop nodemon anytime with ctrl + C
Fibonacci (fibonacci.js
)
- fibs(n): Iterative method to compute the first
n
Fibonacci numbers. - fibsRec(n): Recursive method to compute the first
n
Fibonacci numbers. Example:
console.log(fibsRec(8)); // [0, 1, 1, 2, 3, 5, 8, 13]
Merge Sort (mergeSort.js
)
mergeSort(arr)
: Recursively sorts an array using the divide-and-conquer approach.
Example:
console.log(mergeSort([38, 27, 43, 3, 9])); // [3, 9, 27, 38, 43]
- Recursion allows breaking down problems into smaller subproblems.
- Iterative methods can sometimes be more efficient, but recursion often makes code cleaner and more elegant.