JavaScript Algorithms and Data Structures. How to Code – Coding for Beginners and How to Learn …

Welcome to this beginner-friendly guide on the most common JavaScript algorithms and data structures! This guide is perfect for developers looking to expand their skill set or anyone new to JavaScript. We will provide you with simple explanations and code samples to ensure you can understand and implement these essential concepts.



Introduction

JavaScript is a versatile programming language used for a variety of tasks, including web development, data manipulation, and more. To excel in JavaScript, it’s essential to have a strong understanding of algorithms and data structures. In this guide, we’ll cover some of the most common data structures, including arrays, strings, and objects, and algorithms, such as sorting and searching algorithms.

Arrays

An array is a data structure that stores a collection of elements, each identified by an index. Arrays are very versatile and widely used in JavaScript.


Adding Elements

To add elements to an array, you can use the push() method.

const fruits = ['apple', 'banana'];
fruits.push('orange');
console.log(fruits); // Output: ['apple', 'banana', 'orange']



Removing Elements

To remove elements from an array, you can use the splice() method.

const fruits = ['apple', 'banana', 'orange'];
fruits.splice(1, 1);
console.log(fruits); // Output: ['apple', 'orange']



Searching for Elements

To find the index of an element in an array, you can use the indexOf() method.

const fruits = ['apple', 'banana', 'orange'];
console.log(fruits.indexOf('banana')); // Output: 1



Strings

A string is a data structure that represents a sequence of characters. Strings are used to store text in JavaScript.

const hello = 'Hello, World!';
console.log(hello.length); // Output: 13



Objects

An object is a collection of key-value pairs, where each key is a unique string, and the value can be any data type. Objects are commonly used to store and manage complex data in JavaScript.

const person = {
name: 'Alice',
age: 30,
hobbies: ['reading', 'traveling']
};
console.log(person.name); // Output: 'Alice'


Algorithms

Algorithms are step-by-step procedures for performing calculations, data processing, or automated reasoning tasks. In this section, we will cover common sorting and searching algorithms in JavaScript.


Sorting Algorithms

Sorting algorithms are techniques for arranging data in a particular order. In this guide, we’ll cover three popular sorting algorithms: bubble sort, selection sort, and insertion sort. 


Bubble Sort

Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The algorithm continues until the list is sorted.

function bubbleSort(arr) {
let n = arr.length;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
console.log(bubbleSort([64, 34, 25, 12, 22, 11, 90])); // Output: [11, 12, 22, 25, 34, 64, 90]



Selection Sort

Selection sort is another simple sorting algorithm that selects the smallest element from the unsorted part of the list and moves it to the beginning. This process is repeated for each position in the list until the list is sorted.

function selectionSort(arr) {
let n = arr.length;
for (let i = 0; i < n - 1; i++) {
let minIdx = i;
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
let temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
return arr;
}
console.log(selectionSort([64, 34, 25, 12, 22, 11, 90])); // Output: [11, 12, 22, 25, 34, 64, 90]



Insertion Sort <a name=”insertion-sort”></a>

Insertion sort is a simple sorting algorithm that builds the final sorted list one element at a time. It is similar to the way you sort playing cards in your hands.

function insertionSort(arr) {
let n = arr.length;
for (let i = 1; i < n; i++) {
let key = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
return arr;
}
console.log(insertionSort([64, 34, 25, 12, 22, 11, 90])); // Output: [11, 12, 22, 25, 34, 64, 90]




Searching Algorithms

Searching algorithms are techniques for finding a specific element or set of elements within a data structure. In this guide, we’ll cover two popular search algorithms: linear search and binary search.


Linear Search

Linear search is the simplest searching algorithm, which checks each element in the list sequentially until the target element is found or the end of the list is reached.

function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i;
}
}
return -1;
}

console.log(linearSearch([64, 34, 25, 12, 22, 11, 90], 22)); // Output: 4



Binary Search

Binary search is an efficient searching algorithm that works on sorted lists. It repeatedly divides the list in half, checking if the middle element is the target element. If not, it continues searching either the left or right half of the list, depending on whether the middle element is greater or smaller than the target.

function binarySearch(arr, target, left = 0, right = arr.length - 1) {
if (left > right) {
return -1;
}
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid;
} else if (arr[mid] > target) {
return binarySearch(arr, target, left, mid - 1);
} else {
return binarySearch(arr, target, mid + 1, right);
}
}
const sortedArray = [11, 12, 22, 25, 34, 64, 90];
console.log(binarySearch(sortedArray, 22)); // Output: 2




𝐋𝐢𝐤𝐞

𝐒𝐡𝐚𝐫𝐞

Tags