Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @jsundefined/algojs with all npm packages installed. Try it out:

var algojs = require("@jsundefined/algojs")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@jsundefined/algojs v1.1.2

Algorithms in javascript

Algorithms:

This is js library for algorithms related to data structure. Till now we have published below algorthims.

How to use all methods through AlgoJS class:

let Algojs = require('@jsundefined/algojs');
let sortedArr = Algojs.bubbleSort([12, 11, 13, 5, 6, 7]);
console.log('bubbleSort', sortedArr);

Typescript / Angular:

import Algojs from '@jsundefined/algojs';
let sortedArr = Algojs.bubbleSort([12, 11, 13, 5, 6, 7]);
console.log('bubbleSort', sortedArr);

** Newest Addition in Library ::: String Operations **

String Methods:

  • capitalize
  • casefold
  • center
  • count
  • expandtabs
  • isUpperCase
  • isLowerCase
  • swapcase
  • toUpperFirstLetter
  • format
  • formatter
  • isPositive
  • returnSign
  • numberChangeWithChar
  • isAlnum
  • isDecimal
  • isAlpha
  • isDigit
  • isIdentifier
  • isSpace
  • unicodeToChar
  • binaryToDec
  • decToBinary
  • zfill
  • jsonCleaner

Matrix Methods:

  • add
  • sub
  • mult
  • transpose
  • isIdentityMatrix
  • isSparseMatrix
  • isEqual
  • getUpperTraingularMatrix
  • getLowerTraingularMatrix

Javascript:

let Matrix = require('@jsundefined/algojs/math/matrix');
const matrices1 = new Matrix([[1,1,1],[2,2,2],[3,3,3]]);
const matrices2 = new Matrix([[1,0,0],[0,1,0],[0,0,1]]);
const matrices3 = new Matrix([[1,0,0],[0,1,0],[0,0,1]]);
const matrices4 = new Matrix([[1,2],[3,4],[1,0]]);
const matrices5 = new Matrix([[0,5,2],[6,7,3]]);
console.log(matrices3.isIdentityMatrix(), 
matrices1.isIdentityMatrix(), matrices3.isSparseMatrix(), 
matrices1.isEqual(matrices3), matrices1.getLowerTraingularMatrix(),
 matrices1.getUpperTraingularMatrix(), matrices1.getTransposeMatrix(),
  matrices1.mult(matrices1), matrices4.kroneckerProduct(matrices5));

Typescript / Angular:

import Matrix from '@jsundefined/algojs/math/matrix';
const matrices1 = new Matrix([[1,1,1],[2,2,2],[3,3,3]]);
const matrices2 = new Matrix([[1,0,0],[0,1,0],[0,0,1]]);
const matrices3 = new Matrix([[1,0,0],[0,1,0],[0,0,1]]);
const matrices4 = new Matrix([[1,2],[3,4],[1,0]]);
const matrices5 = new Matrix([[0,5,2],[6,7,3]]);
console.log(matrices3.isIdentityMatrix(), 
matrices1.isIdentityMatrix(), matrices3.isSparseMatrix(), 
matrices1.isEqual(matrices3), matrices1.getLowerTraingularMatrix(),
 matrices1.getUpperTraingularMatrix(), matrices1.getTransposeMatrix(),
  matrices1.mult(matrices1), matrices4.kroneckerProduct(matrices5));

Sorting

  • Bead Sort, Bitonic Sort, Bogo Sort, Bubble Sort, Bucket Sort, Cocktail Sort, Comb Sort, Count Sort, Cycle Sort, Gnome Sort, Heap Sort, Insertion Sort, Merge Sort, Pancake Sort, Pigeonhole Sort, Quick Sort, Radix Sort, Selection Sort, Shell Sort, Sleep Sort, Stooge Sort, Strand Sort, Swap Sort, Tim Sort

How to use sort methods:

Javascript:

let bubbleSort = require('@jsundefined/algojs/sort/bubble.js');
let sortedArr = bubbleSort([12, 11, 13, 5, 6, 7]);
console.log('bubbleSort', sortedArr);

Typescript / Angular:

import bubbleSort from '@jsundefined/algojs/sort/bubble';
let sortedArr = bubbleSort([12, 11, 13, 5, 6, 7]);
console.log('bubbleSort', sortedArr);

Searching

  • Binary
  • Exponential
  • Fibonacci
  • Interpolation
  • Jump
  • Linear

How to use search methods:

Javascript:

let linearSearch = require('@jsundefined/algojs/search/linear.js');
let indexOfSearchResult = linearSearch([ -5, -10, 0, -3, 8, 5, -1, 10], 5); // if -1 means not found
console.log('bubbleSort', indexOfSearchResult);

Typescript / Angular:

import linearSearch from '@jsundefined/algojs/search/linear';
let indexOfSearchResult = linearSearch([ -5, -10, 0, -3, 8, 5, -1, 10], 5); // if -1 means not found
console.log('bubbleSort', indexOfSearchResult);

Collection

  • Queue
  • Deque
  • Linked List
  • Stack

How to use collection classes:

Javascript:

let LinkedList = require('@jsundefined/algojs/collections/linked-list/LinkedList.js');
let ll = new LinkedList();
ll.insertAtBeginning(11);
ll.add(4);
ll.add(12);
ll.add(91);
ll.add(21);
ll.add(88);
ll.add(27);
ll.add(45);
ll.add(77);
ll.insertAtEnd(34);
ll.deleteNodeAt(1);
ll.deleteNode(34);
ll.deleteFirstNode();
ll.deleteLastNode();
ll.insertAt(8,3);
ll.reverse();
let curr = ll.swap(2,5);
console.log(JSON.stringify(curr));
console.log(JSON.stringify(ll.sort()));

Typescript / Angular:

import LinkedList from '@jsundefined/algojs/collections/linked-list/LinkedList';
let ll = new LinkedList();
ll.insertAtBeginning(11);
ll.add(4);
ll.add(12);
ll.add(91);
ll.add(21);
ll.add(88);
ll.add(27);
ll.add(45);
ll.add(77);
ll.insertAtEnd(34);
ll.deleteNodeAt(1);
ll.deleteNode(34);
ll.deleteFirstNode();
ll.deleteLastNode();
ll.insertAt(8,3);
ll.reverse();
let curr = ll.swap(2,5);
console.log(JSON.stringify(curr));
console.log(JSON.stringify(ll.sort()));

Single Linked List:

MethodsUsesParamatersReturn
addinserting at end of linked listone parameter, data to be addedfull list
insertAtEndinserting data at end of linked listone parameter, data to be addedfull list
insertAtStartinserting data at start of linked listone parameter, data to be addedfull list
insertAtinserting data at particular index of linked list2 parameters, (data to be added, index)full list
deleteNodeAtdeleting data at particular index of linked list1 parameter, indexdeleted node
deleteNodedeleting data of linked list1 parameter, datafull list
deleteFirstNodedeleting first node of linked listno parametersfull list
deleteLastNodedeleting last data of linked listno parametersfull list
indexOfindexOf a particular data in linked list1 parameter, dataindex else -1
searchindex of a particular data in linked list1 parameter, dataindex else false
getDataAtgetting data of a node at particular index of linked list1 parameter, indexdata else null
getNodeAtgetting node at particular index of linked list1 parameter, indexdata else null
clearclears full linked listno parameterstrue
isEmptychecks linked list is empty or notno parameterstrue or false
lengthget length of a linked listno parameterslength
reversereverse the linked listno parametersfull list
swapswap two nodes the linked list2 parameters, 2 nodes indexfull list
toArraycreates array from linked listno parametersarray
fromArraycreates linked list from arrayno parameterslinked list
sortsorts linked listno parameterslinked list

Doubly Linked List:

MethodsUsesParamatersReturn
addinserting at end of linked listone parameter, data to be addedfull list
insertAtinserting data at particular index of linked list2 parameters, (data to be added, index)full list
deleteNodeAtdeleting data at particular index of linked list1 parameter, indexdeleted node
deleteNodedeleting data of linked list1 parameter, datafull list
deleteFirstNodedeleting first node of linked listno parametersfull list
deleteLastNodedeleting last data of linked listno parametersfull list
searchindex of a particular data in linked list1 parameter, dataindex else false
getDataAtgetting data of a node at particular index of linked list1 parameter, indexdata else null
getNodeAtgetting node at particular index of linked list1 parameter, indexdata else null
clearclears full linked listno parameterstrue
isEmptychecks linked list is empty or notno parameterstrue or false
lengthget length of a linked listno parameterslength
reversereverse the linked listno parametersfull list
swapswap two nodes the linked list2 parameters, 2 nodes indexfull list
toArraycreates array from linked listno parametersarray
fromArraycreates linked list from arrayno parameterslinked list
sortsorts linked listno parameterslinked list

Stack:

MethodsUsesParamatersReturn
pushpush data into stackone parameter, datafull data
poppop from stackno parametersdata or false
peekpeeks from stackno parametersdata or null
clearclears full stackno parameterstrue
isEmptychecks stack is empty or notno parameterstrue or false
lengthget length of a stackno parameterslength
reversereverse the stackno parametersfull stack
searchindex of a particular data in stack1 parameter, dataindex else false
sortsorts stackno parametersstack

Queue:

MethodsUsesParamatersReturn
enqueuepush data into queueone parameter, datafull data
dequeuepop from queueno parametersdata or false
peekpeeks from queueno parametersdata or null
isEmptychecks queue is empty or notno parameterstrue or false
lengthget length of a queueno parameterslength
sortsorts queueno parametersqueue

Deque:

MethodsUsesParamatersReturn
add or offeradd data into queueone parameter, datafull list
addFirst or offerFirstadd first postion of queueone parameter, datafull list
addLast or offerLastadd end of queueone parameter, datafull list
removeFirstremoves from firstno parametersfull list
removeLastremoves from lastno parametersfull list
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free