JavaScript Algorithms and Data Structures_ Comprehensive Guide
JavaScript Algorithms and Data Structures_ Comprehensive Guide
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
3. Stacks 4
4. Queues 4
Common Algorithms 5
1. Searching Algorithms 5
2. Sorting Algorithms 6
Exercises 7
Exercise 1: Reverse a String Using a Stack 7
Exercise 2: Find the Maximum in a Linked List 7
Exercise 3: Implement a Queue Using Two Stacks 8
Multiple-Choice Questions 8
Question 1: 8
Question 2: 9
Question 3: 9
Best Practices 9
This guide introduces essential algorithms and data structures, implemented using JavaScript.
You'll learn key concepts, code examples, and practical applications, with exercises and quiz
questions to reinforce your understanding.
1. Arrays
Common Operations:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
● Access: array[index]
● Insert: push(), unshift()
● Remove: pop(), shift()
2. Linked Lists
A linked list is a collection of nodes where each node contains data and a reference to the next
node.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
append(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
}
display() {
let current = this.head;
while (current) {
console.log(current.data);
current = current.next;
}
}
}
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
const list = new LinkedList();
list.append(10);
list.append(20);
list.display(); // Output: 10, 20
3. Stacks
class Stack {
constructor() {
this.items = [];
}
push(item) {
this.items.push(item);
}
pop() {
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
}
const stack = new Stack();
stack.push(5);
stack.push(10);
console.log(stack.pop()); // Output: 10
4. Queues
class Queue {
constructor() {
this.items = [];
}
enqueue(item) {
this.items.push(item);
}
dequeue() {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
4
return this.items.shift();
}
}
const queue = new Queue();
queue.enqueue(5);
queue.enqueue(10);
console.log(queue.dequeue()); // Output: 5
Common Algorithms
1. Searching Algorithms
● Binary Search: Searches a sorted array by repeatedly dividing the search interval in
half.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
}
return -1;
}
console.log(binarySearch([1, 2, 3, 4, 5], 3)); // Output: 2
2. Sorting Algorithms
● Bubble Sort: Repeatedly swaps adjacent elements if they are in the wrong order.
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;
}
console.log(bubbleSort([5, 3, 8, 4, 2])); // Output: [2, 3, 4, 5, 8]
● Merge Sort: Divides the array into halves, sorts them, and merges the results.
function mergeSort(arr) {
if (arr.length <= 1) {
return arr;
}
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
return merge(left, right);
}
function merge(left, right) {
const result = [];
while (left.length && right.length) {
if (left[0] < right[0]) {
result.push(left.shift());
} else {
result.push(right.shift());
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
}
}
return result.concat(left, right);
}
console.log(mergeSort([5, 3, 8, 4, 2])); // Output: [2, 3, 4, 5, 8]
Exercises
Solution:
function reverseString(str) {
const stack = [];
for (const char of str) {
stack.push(char);
}
return stack.reverse().join('');
}
console.log(reverseString("hello")); // Output: "olleh"
Solution:
function findMax(linkedList) {
let max = -Infinity;
let current = linkedList.head;
while (current) {
if (current.data > max) {
max = current.data;
}
current = current.next;
}
return max;
}
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
7
Exercise 3: Implement a Queue Using Two Stacks
Solution:
class QueueUsingStacks {
constructor() {
this.stack1 = [];
this.stack2 = [];
}
enqueue(item) {
this.stack1.push(item);
}
dequeue() {
if (!this.stack2.length) {
while (this.stack1.length) {
this.stack2.push(this.stack1.pop());
}
}
return this.stack2.pop();
}
}
const queue = new QueueUsingStacks();
queue.enqueue(1);
queue.enqueue(2);
console.log(queue.dequeue()); // Output: 1
Multiple-Choice Questions
Question 1:
1. Array
2. Stack
3. Queue
4. Linked List
Answer: 2. Stack
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
8
Question 2:
1. O(1)O(1)O(1)
2. O(n)O(n)O(n)
3. O(logn)O(\log n)O(logn)
4. O(n2)O(n^2)O(n2)
Question 3:
1. Bubble Sort
2. Selection Sort
3. Merge Sort
4. Insertion Sort
Best Practices
1. Analyze Complexity: Understand the time and space complexity of your algorithms.
2. Use Built-in Methods: Leverage JavaScript’s built-in methods for simpler operations
(e.g., sort, map).
3. Practice Frequently: Solve problems on platforms like LeetCode or HackerRank.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis