diff --git a/README.md b/README.md index 5d45187..0ac83d6 100644 --- a/README.md +++ b/README.md @@ -1,159 +1,106 @@ -# JavaScript Algorithms (Sorted by Popularity) +# JavaScript Algorithms 📚 -This list showcases some of the most popular JavaScript algorithms, from simple string manipulations to classic recursive solutions and efficient searching techniques. Each snippet demonstrates a fundamental concept often encountered in coding interviews and real-world development. +Welcome to the **JavaScript Algorithms** repository! This collection features popular algorithms you may encounter in the real world. Whether you are preparing for coding interviews, enhancing your programming skills, or simply exploring algorithms, you will find valuable resources here. -> **Note:** Popularity is based on common interview topics, educational resources, and usage in developer communities. +[![Releases](https://img.shields.io/github/release/ahmedsaidahmedsaid/javascript-algorithms.svg)](https://github.com/ahmedsaidahmedsaid/javascript-algorithms/releases) -## 1. Reverse a String +## Table of Contents -```js -function reverseString(str) { - return str.split("").reverse().join(""); -} +- [Introduction](#introduction) +- [Features](#features) +- [Topics Covered](#topics-covered) +- [Installation](#installation) +- [Usage](#usage) +- [Contributing](#contributing) +- [License](#license) +- [Contact](#contact) -console.log(reverseString("hello")); // Output: "olleh" -``` +## Introduction -**Explanation**: Reverses the characters in a string by splitting, reversing, and joining them back together. +Algorithms are essential to computer science. They help solve problems efficiently and effectively. This repository aims to provide a comprehensive list of algorithms implemented in JavaScript. Each algorithm is designed to be simple and easy to understand, making it suitable for both beginners and experienced developers. -## 2. Palindrome Check +## Features -```js -function isPalindrome(str) { - return str === str.split("").reverse().join(""); -} +- A wide range of algorithms covering various topics. +- Clear explanations and examples for each algorithm. +- Easy-to-follow code snippets. +- Regular updates and improvements. +- A community of contributors who help maintain and expand the repository. -console.log(isPalindrome("racecar")); // Output: true -``` +## Topics Covered -**Explanation**: Determines if a string reads the same backward as forward using string reversal. +This repository includes algorithms in the following areas: -## 3. Binary Search +- **Algorithm Challenges**: Engage with various coding challenges to test your skills. +- **Sorting Algorithms**: Learn about different sorting techniques, including: + - Bubble Sort + - Merge Sort + - Quick Sort +- **Searching Algorithms**: Explore searching techniques such as: + - Binary Search + - Anagram Search +- **Mathematical Algorithms**: Understand key mathematical concepts, including: + - Euclidean Algorithm + - Factorial + - Fibonacci Sequence + - Greatest Common Divisor + - Prime Number Generation +- **String Manipulation**: Check for palindromes and other string-related tasks. + +## Installation -```js -function binarySearch(arr, target) { - let left = 0, - right = arr.length - 1; - while (left <= right) { - const mid = Math.floor((left + right) / 2); - if (arr[mid] === target) return mid; - if (arr[mid] < target) left = mid + 1; - else right = mid - 1; - } - return -1; -} +To get started with this repository, clone it to your local machine. Use the following command: -console.log(binarySearch([1, 2, 3, 4, 5], 4)); // Output: 3 +```bash +git clone https://github.com/ahmedsaidahmedsaid/javascript-algorithms.git ``` -**Explanation**: Searches for a target in a sorted array using a divide-and-conquer approach (Time complexity: O(log n)). - -## 4. Fibonacci Sequence (Recursive) - -```js -function fibonacci(n) { - if (n <= 1) return n; - return fibonacci(n - 1) + fibonacci(n - 2); -} +Once cloned, navigate to the directory: -console.log(fibonacci(6)); // Output: 8 +```bash +cd javascript-algorithms ``` -**Explanation**: Generates the nth Fibonacci number recursively by summing the two preceding numbers. - -⚠️ **Note**: This approach has exponential time complexity O(2^n) and is inefficient for large inputs. Use memoization or iteration for better performance. - -## 5. Factorial of a Number - -```js -function factorial(n) { - if (n === 0) return 1; - return n * factorial(n - 1); -} +You can then install any required dependencies using npm: -console.log(factorial(5)); // Output: 120 +```bash +npm install ``` -**Explanation**: Calculates the factorial of a number recursively by multiplying it with decremented values. +## Usage -## 6. Prime Number Check +After installing, you can run any algorithm by executing the corresponding JavaScript file. For example, to run the Fibonacci sequence algorithm, use: -```js -function isPrime(num) { - if (num <= 1) return false; - for (let i = 2; i <= Math.sqrt(num); i++) { - if (num % i === 0) return false; - } - return true; -} - -console.log(isPrime(7)); // Output: true +```bash +node fibonacci.js ``` -**Explanation**: Checks if a number is prime by testing divisibility up to its square root. - -## 7. Find Maximum in Array +Make sure to check the documentation for each algorithm to understand how to use them effectively. -```js -function findMax(arr) { - return Math.max(...arr); -} +For the latest updates and releases, visit the [Releases section](https://github.com/ahmedsaidahmedsaid/javascript-algorithms/releases). -console.log(findMax([1, 2, 3, 4, 5])); // Output: 5 -``` +## Contributing -**Explanation**: Finds the largest number in an array using the `Math.max` function and the spread operator. - -## 8. Merge Two Sorted Arrays - -```js -function mergeSortedArrays(arr1, arr2) { - let merged = [], i = 0, j = 0; - while (i < arr1.length && j < arr2.length) { - if (arr1[i] < arr2[j]) { - merged.push(arr1[i]); - i++; - } else { - merged.push(arr2[j]); - j++; - } - } - return merged.concat(arr1.slice(i)).concat(arr2.slice(j)); -} - -console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6] -``` +We welcome contributions! If you would like to add an algorithm or improve existing code, please follow these steps: -**Explanation**: Merges two sorted arrays into one sorted array by comparing elements sequentially. +1. Fork the repository. +2. Create a new branch for your feature or fix. +3. Make your changes and commit them. +4. Push your changes to your forked repository. +5. Submit a pull request. -## 9. Bubble Sort +Please ensure that your code follows the existing style and includes comments where necessary. -```js -function bubbleSort(arr) { - for (let i = 0; i < arr.length; i++) { - for (let j = 0; j < arr.length - i - 1; j++) { - if (arr[j] > arr[j + 1]) { - [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; - } - } - } - return arr; -} +## License -console.log(bubbleSort([5, 3, 8, 4, 2])); // Output: [2, 3, 4, 5, 8] -``` +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. -**Explanation**: Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order (Time complexity: O(n²)). +## Contact -## 10. Find the GCD (Greatest Common Divisor) +For questions or feedback, feel free to reach out. You can create an issue in the repository or contact me directly through GitHub. -```js -function gcd(a, b) { - if (b === 0) return a; - return gcd(b, a % b); -} +--- -console.log(gcd(48, 18)); // Output: 6 -``` +This repository serves as a valuable resource for anyone interested in JavaScript algorithms. By providing clear explanations and practical examples, it aims to enhance your understanding of algorithms and their applications. -**Explanation**: Uses the Euclidean algorithm to compute the greatest common divisor of two numbers (Time complexity: O(log min(a, b))). +Explore the various algorithms, contribute to the repository, and improve your coding skills. Happy coding! \ No newline at end of file