1.0.2 • Published 4 months ago

dsa-master v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

DSA-Algorithms-JS

A Comprehensive Data Structures and Algorithms Library for JavaScript

Introduction

DSA-Algorithms-JS is a JavaScript library that provides efficient implementations of sorting algorithms, searching algorithms, and graph data structures. Whether you're a beginner learning DSA or an experienced developer working on competitive programming, this package will help you write optimized and well-structured code. It is designed for:

  • Sorting and Searching Algorithms
  • Dynamic Array
  • Linked List
  • Stack
  • Queue
  • Heap

Installation

  • To use this library, first install it using npm:

    npm install dsa-master
  • Then, require the necessary modules in your JavaScript file:

    const dsa = require('dsa-master');
    const { SearchingAlgorithm, SortingAlgorithm, DataStructure } = dsa;

📚 Usage

Sorting Algorithms

This table provides the time and space complexities of common sorting algorithms.

AlgorithmBest CaseWorst CaseAverage CaseSpace Complexity
Bubble SortO(n)O(n²)O(n²)O(1)
Selection SortO(n²)O(n²)O(n²)O(1)
Insertion SortO(n)O(n²)O(n²)O(1)
Merge SortO(n log n)O(n log n)O(n log n)O(n)
Quick SortO(n log n)O(n²)O(n log n)O(log n)
const arr1 = [6, 7, -1, 0, 100, 7];
SortingAlgorithm.BubbleSort(arr1);
console.log(arr1); // Output: [-1, 0, 6, 7, 7, 100]

Searching Algorithms

AlgorithmBest CaseWorst CaseAverage Case
Linear SearchO(1)O(n)O(n)
Binary SearchO(1)O(log n)O(log n)
const arr2 = [-1, 0, 6, 7, 9, 100];
const index = SearchingAlgorithm.BinarySearch(arr2, 7);
console.log(index); // Output: 3

Dynamic Array

const DynamicArray = DataStructure.Array;
const array = new DynamicArray();
array.push(10);
array.push(20);
array.display(); // [10, 20]
array.pop();
array.display(); // [10]

Linked List

const LinkedList = DataStructure.LinkedList;
const list = new LinkedList();
list.append(5);
list.append(10);
list.display(); // 5 -> 10 -> null
list.delete(5);
list.display(); // 10 -> null

Stack

const Stack = DataStructure.Stack;
const stack = new Stack();
stack.push(5);
stack.push(10);
stack.display(); // [5, 10]
stack.pop();
stack.display(); // [5]

Queue

const Queue = DataStructure.Queue;
const queue = new Queue();
queue.enqueue(5);
queue.enqueue(10);
queue.display(); // [5, 10]
queue.dequeue();
queue.display(); // [10]

Heap

const Heap = DataStructure.PriorityQueue;
const minHeap = new Heap((a, b) => a < b); // Min Heap
minHeap.insert(10);
minHeap.insert(5);
minHeap.display(); // [5, 10]
minHeap.remove();
minHeap.display(); // [10]

Graph (Adjacency List Representation)

const Graph = dsa.Graph;
const graph = new Graph();
graph.addVertex('A');
graph.addVertex('B');
graph.addEdge('A', 'B');
graph.display();
// A : [ 'B' ]
// B : [ 'A' ]

📞 Contact

Feel free to reach out: