Skip to content

Commit b5aedfa

Browse files
Commit
1 parent d80a8c5 commit b5aedfa

File tree

1 file changed

+69
-122
lines changed

1 file changed

+69
-122
lines changed

README.md

Lines changed: 69 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,106 @@
1-
# JavaScript Algorithms (Sorted by Popularity)
1+
# JavaScript Algorithms 📚
22

3-
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.
3+
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.
44

5-
> **Note:** Popularity is based on common interview topics, educational resources, and usage in developer communities.
5+
[![Releases](https://img.shields.io/github/release/ahmedsaidahmedsaid/javascript-algorithms.svg)](https://github.com/ahmedsaidahmedsaid/javascript-algorithms/releases)
66

7-
## 1. Reverse a String
7+
## Table of Contents
88

9-
```js
10-
function reverseString(str) {
11-
return str.split("").reverse().join("");
12-
}
9+
- [Introduction](#introduction)
10+
- [Features](#features)
11+
- [Topics Covered](#topics-covered)
12+
- [Installation](#installation)
13+
- [Usage](#usage)
14+
- [Contributing](#contributing)
15+
- [License](#license)
16+
- [Contact](#contact)
1317

14-
console.log(reverseString("hello")); // Output: "olleh"
15-
```
18+
## Introduction
1619

17-
**Explanation**: Reverses the characters in a string by splitting, reversing, and joining them back together.
20+
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.
1821

19-
## 2. Palindrome Check
22+
## Features
2023

21-
```js
22-
function isPalindrome(str) {
23-
return str === str.split("").reverse().join("");
24-
}
24+
- A wide range of algorithms covering various topics.
25+
- Clear explanations and examples for each algorithm.
26+
- Easy-to-follow code snippets.
27+
- Regular updates and improvements.
28+
- A community of contributors who help maintain and expand the repository.
2529

26-
console.log(isPalindrome("racecar")); // Output: true
27-
```
30+
## Topics Covered
2831

29-
**Explanation**: Determines if a string reads the same backward as forward using string reversal.
32+
This repository includes algorithms in the following areas:
3033

31-
## 3. Binary Search
34+
- **Algorithm Challenges**: Engage with various coding challenges to test your skills.
35+
- **Sorting Algorithms**: Learn about different sorting techniques, including:
36+
- Bubble Sort
37+
- Merge Sort
38+
- Quick Sort
39+
- **Searching Algorithms**: Explore searching techniques such as:
40+
- Binary Search
41+
- Anagram Search
42+
- **Mathematical Algorithms**: Understand key mathematical concepts, including:
43+
- Euclidean Algorithm
44+
- Factorial
45+
- Fibonacci Sequence
46+
- Greatest Common Divisor
47+
- Prime Number Generation
48+
- **String Manipulation**: Check for palindromes and other string-related tasks.
49+
50+
## Installation
3251

33-
```js
34-
function binarySearch(arr, target) {
35-
let left = 0,
36-
right = arr.length - 1;
37-
while (left <= right) {
38-
const mid = Math.floor((left + right) / 2);
39-
if (arr[mid] === target) return mid;
40-
if (arr[mid] < target) left = mid + 1;
41-
else right = mid - 1;
42-
}
43-
return -1;
44-
}
52+
To get started with this repository, clone it to your local machine. Use the following command:
4553

46-
console.log(binarySearch([1, 2, 3, 4, 5], 4)); // Output: 3
54+
```bash
55+
git clone https://github.com/ahmedsaidahmedsaid/javascript-algorithms.git
4756
```
4857

49-
**Explanation**: Searches for a target in a sorted array using a divide-and-conquer approach (Time complexity: O(log n)).
50-
51-
## 4. Fibonacci Sequence (Recursive)
52-
53-
```js
54-
function fibonacci(n) {
55-
if (n <= 1) return n;
56-
return fibonacci(n - 1) + fibonacci(n - 2);
57-
}
58+
Once cloned, navigate to the directory:
5859

59-
console.log(fibonacci(6)); // Output: 8
60+
```bash
61+
cd javascript-algorithms
6062
```
6163

62-
**Explanation**: Generates the nth Fibonacci number recursively by summing the two preceding numbers.
63-
64-
⚠️ **Note**: This approach has exponential time complexity O(2^n) and is inefficient for large inputs. Use memoization or iteration for better performance.
65-
66-
## 5. Factorial of a Number
67-
68-
```js
69-
function factorial(n) {
70-
if (n === 0) return 1;
71-
return n * factorial(n - 1);
72-
}
64+
You can then install any required dependencies using npm:
7365

74-
console.log(factorial(5)); // Output: 120
66+
```bash
67+
npm install
7568
```
7669

77-
**Explanation**: Calculates the factorial of a number recursively by multiplying it with decremented values.
70+
## Usage
7871

79-
## 6. Prime Number Check
72+
After installing, you can run any algorithm by executing the corresponding JavaScript file. For example, to run the Fibonacci sequence algorithm, use:
8073

81-
```js
82-
function isPrime(num) {
83-
if (num <= 1) return false;
84-
for (let i = 2; i <= Math.sqrt(num); i++) {
85-
if (num % i === 0) return false;
86-
}
87-
return true;
88-
}
89-
90-
console.log(isPrime(7)); // Output: true
74+
```bash
75+
node fibonacci.js
9176
```
9277

93-
**Explanation**: Checks if a number is prime by testing divisibility up to its square root.
94-
95-
## 7. Find Maximum in Array
78+
Make sure to check the documentation for each algorithm to understand how to use them effectively.
9679

97-
```js
98-
function findMax(arr) {
99-
return Math.max(...arr);
100-
}
80+
For the latest updates and releases, visit the [Releases section](https://github.com/ahmedsaidahmedsaid/javascript-algorithms/releases).
10181

102-
console.log(findMax([1, 2, 3, 4, 5])); // Output: 5
103-
```
82+
## Contributing
10483

105-
**Explanation**: Finds the largest number in an array using the `Math.max` function and the spread operator.
106-
107-
## 8. Merge Two Sorted Arrays
108-
109-
```js
110-
function mergeSortedArrays(arr1, arr2) {
111-
let merged = [], i = 0, j = 0;
112-
while (i < arr1.length && j < arr2.length) {
113-
if (arr1[i] < arr2[j]) {
114-
merged.push(arr1[i]);
115-
i++;
116-
} else {
117-
merged.push(arr2[j]);
118-
j++;
119-
}
120-
}
121-
return merged.concat(arr1.slice(i)).concat(arr2.slice(j));
122-
}
123-
124-
console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6]
125-
```
84+
We welcome contributions! If you would like to add an algorithm or improve existing code, please follow these steps:
12685

127-
**Explanation**: Merges two sorted arrays into one sorted array by comparing elements sequentially.
86+
1. Fork the repository.
87+
2. Create a new branch for your feature or fix.
88+
3. Make your changes and commit them.
89+
4. Push your changes to your forked repository.
90+
5. Submit a pull request.
12891

129-
## 9. Bubble Sort
92+
Please ensure that your code follows the existing style and includes comments where necessary.
13093

131-
```js
132-
function bubbleSort(arr) {
133-
for (let i = 0; i < arr.length; i++) {
134-
for (let j = 0; j < arr.length - i - 1; j++) {
135-
if (arr[j] > arr[j + 1]) {
136-
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
137-
}
138-
}
139-
}
140-
return arr;
141-
}
94+
## License
14295

143-
console.log(bubbleSort([5, 3, 8, 4, 2])); // Output: [2, 3, 4, 5, 8]
144-
```
96+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
14597

146-
**Explanation**: Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order (Time complexity: O(n²)).
98+
## Contact
14799

148-
## 10. Find the GCD (Greatest Common Divisor)
100+
For questions or feedback, feel free to reach out. You can create an issue in the repository or contact me directly through GitHub.
149101

150-
```js
151-
function gcd(a, b) {
152-
if (b === 0) return a;
153-
return gcd(b, a % b);
154-
}
102+
---
155103

156-
console.log(gcd(48, 18)); // Output: 6
157-
```
104+
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.
158105

159-
**Explanation**: Uses the Euclidean algorithm to compute the greatest common divisor of two numbers (Time complexity: O(log min(a, b))).
106+
Explore the various algorithms, contribute to the repository, and improve your coding skills. Happy coding!

0 commit comments

Comments
 (0)