Ad Code

✨🎆 JOIN MERN, JAVA, PYTHON, AI, DEVOPS, SALESFORCE Courses 🎆✨

Get 100% Placement Oriented Program CLICK to new more info click

DSA in JS | JAVASCRIPT DSA Tutorials by Shiva Sir | DSA Practical Tutorials

Data Structures & Algorithms in JavaScript — Complete Step-by-Step Tutorial

1. Introduction to DSA

What is DSA?

DSA stands for:

  • Data Structures → Ways to store data efficiently
  • Algorithms → Step-by-step procedures to solve problems

Why Learn DSA?

  • Crack coding interviews
  • Improve problem-solving skills
  • Write optimized code
  • Build scalable applications

2. JavaScript Basics for DSA

Variables


let name = "Shiva";
const age = 25;

console.log(name);
console.log(age);

Functions


function add(a, b) {
    return a + b;
}

console.log(add(10, 20));

Loops


for(let i = 0; i < 5; i++) {
    console.log(i);
}

3. Time Complexity (Big O Notation)

Complexity Meaning
O(1) Constant
O(log n) Logarithmic
O(n) Linear
O(n²) Quadratic

Example O(1)


function getFirst(arr) {
    return arr[0];
}

Example O(n)


function printArray(arr) {
    for(let item of arr) {
        console.log(item);
    }
}

4. Arrays in JavaScript

Create Array


let numbers = [10, 20, 30, 40];

Insert Element


numbers.push(50);

console.log(numbers);

Remove Element


numbers.pop();

console.log(numbers);

Traverse Array


for(let i = 0; i < numbers.length; i++) {
    console.log(numbers[i]);
}

Find Maximum Number


function findMax(arr) {
    let max = arr[0];

    for(let i = 1; i < arr.length; i++) {
        if(arr[i] > max) {
            max = arr[i];
        }
    }

    return max;
}

console.log(findMax([10, 90, 30, 50]));

5. Strings in JavaScript

Reverse a String


function reverseString(str) {
    return str.split('').reverse().join('');
}

console.log(reverseString("javascript"));

Check Palindrome


function isPalindrome(str) {
    let reversed = str.split('').reverse().join('');

    return str === reversed;
}

console.log(isPalindrome("madam"));

6. Searching Algorithms

Linear Search


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

    return -1;
}

console.log(linearSearch([10, 20, 30, 40], 30));

Time Complexity: O(n)

Binary Search

Condition: Array must be sorted.


function binarySearch(arr, target) {
    let left = 0;
    let right = arr.length - 1;

    while(left <= right) {
        let 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;
}

console.log(binarySearch([10,20,30,40,50], 40));

Time Complexity: O(log n)


7. Sorting Algorithms

Bubble Sort


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]) {

                let temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    return arr;
}

console.log(bubbleSort([5,3,8,1,2]));

Time Complexity: O(n²)

Selection Sort


function selectionSort(arr) {

    for(let i = 0; i < arr.length; i++) {

        let minIndex = i;

        for(let j = i + 1; j < arr.length; j++) {

            if(arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }

        let temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }

    return arr;
}

console.log(selectionSort([9,4,7,1]));

Insertion Sort


function insertionSort(arr) {

    for(let i = 1; i < arr.length; i++) {

        let current = arr[i];
        let j = i - 1;

        while(j >= 0 && arr[j] > current) {
            arr[j + 1] = arr[j];
            j--;
        }

        arr[j + 1] = current;
    }

    return arr;
}

console.log(insertionSort([5,2,4,1]));

8. Stack Data Structure

LIFO → Last In First Out


class Stack {

    constructor() {
        this.items = [];
    }

    push(element) {
        this.items.push(element);
    }

    pop() {
        return this.items.pop();
    }

    peek() {
        return this.items[this.items.length - 1];
    }
}

const stack = new Stack();

stack.push(10);
stack.push(20);

console.log(stack.pop());

9. Queue Data Structure

FIFO → First In First Out


class Queue {

    constructor() {
        this.items = [];
    }

    enqueue(element) {
        this.items.push(element);
    }

    dequeue() {
        return this.items.shift();
    }
}

const queue = new Queue();

queue.enqueue(10);
queue.enqueue(20);

console.log(queue.dequeue());

10. Linked List


class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {

    constructor() {
        this.head = null;
    }

    insert(data) {

        let newNode = new Node(data);

        if(this.head === null) {
            this.head = newNode;
            return;
        }

        let current = this.head;

        while(current.next !== null) {
            current = current.next;
        }

        current.next = newNode;
    }

    print() {

        let current = this.head;

        while(current !== null) {
            console.log(current.data);
            current = current.next;
        }
    }
}

const list = new LinkedList();

list.insert(10);
list.insert(20);
list.insert(30);

list.print();

11. Recursion

Factorial Using Recursion


function factorial(n) {

    if(n === 1) {
        return 1;
    }

    return n * factorial(n - 1);
}

console.log(factorial(5));

12. Hash Map (Object)


let student = {
    name: "Shiva",
    course: "DSA"
};

console.log(student.name);

13. Set in JavaScript


let set = new Set();

set.add(10);
set.add(20);
set.add(10);

console.log(set);

14. Tree Data Structure


class TreeNode {

    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

const root = new TreeNode(10);

root.left = new TreeNode(20);
root.right = new TreeNode(30);

console.log(root);

15. Graph Basics


class Graph {

    constructor() {
        this.list = {};
    }

    addVertex(vertex) {
        this.list[vertex] = [];
    }

    addEdge(v1, v2) {
        this.list[v1].push(v2);
        this.list[v2].push(v1);
    }
}

const graph = new Graph();

graph.addVertex("A");
graph.addVertex("B");

graph.addEdge("A", "B");

console.log(graph.list);

16. DFS (Depth First Search)


const graphData = {
    A: ["B", "C"],
    B: ["D"],
    C: [],
    D: []
};

function dfs(node, visited = new Set()) {

    if(visited.has(node)) {
        return;
    }

    console.log(node);

    visited.add(node);

    for(let neighbor of graphData[node]) {
        dfs(neighbor, visited);
    }
}

dfs("A");

17. BFS (Breadth First Search)


function bfs(graph, start) {

    let queue = [start];
    let visited = new Set();

    visited.add(start);

    while(queue.length > 0) {

        let node = queue.shift();

        console.log(node);

        for(let neighbor of graph[node]) {

            if(!visited.has(neighbor)) {

                visited.add(neighbor);
                queue.push(neighbor);
            }
        }
    }
}

bfs(graphData, "A");

18. Dynamic Programming

Fibonacci Using DP


function fibonacci(n) {

    let dp = [0, 1];

    for(let i = 2; i <= n; i++) {
        dp[i] = dp[i - 1] + dp[i - 2];
    }

    return dp[n];
}

console.log(fibonacci(10));

19. Two Pointer Technique


function twoSum(arr, target) {

    let left = 0;
    let right = arr.length - 1;

    while(left < right) {

        let sum = arr[left] + arr[right];

        if(sum === target) {
            return [left, right];
        }

        if(sum < target) {
            left++;
        } else {
            right--;
        }
    }

    return [];
}

console.log(twoSum([1,2,3,4,6], 6));

20. Sliding Window Technique


function maxSum(arr, k) {

    let windowSum = 0;

    for(let i = 0; i < k; i++) {
        windowSum += arr[i];
    }

    let max = windowSum;

    for(let i = k; i < arr.length; i++) {

        windowSum += arr[i] - arr[i-k];

        max = Math.max(max, windowSum);
    }

    return max;
}

console.log(maxSum([2,1,5,1,3,2], 3));

21. Best Platforms for Practice


22. Final Learning Roadmap

Beginner

  • Arrays
  • Strings
  • Loops
  • Functions

Intermediate

  • Stack
  • Queue
  • Linked List
  • Recursion

Advanced

  • Trees
  • Graphs
  • Dynamic Programming

23. Mini Practice Problems

  1. Reverse Array
  2. Find Duplicate Elements
  3. Check Palindrome
  4. Find Largest Number
  5. Implement Stack
  6. Implement Queue
  7. Binary Search
  8. Bubble Sort
  9. DFS Traversal
  10. Fibonacci Using DP

24. Conclusion

If you practice coding daily, solve problems consistently, and revise concepts weekly, you can become strong in DSA with JavaScript within 2–3 months.

Join DSA LIVE Session with Shiva Sir connect Learn DSA in JS, C++, JAVA, C# with Shiva Sir

Post a Comment

0 Comments