Skip to content

Instantly share code, notes, and snippets.

View imvsharma's full-sized avatar
🎯
Focusing

Vaibhav Sharma imvsharma

🎯
Focusing
View GitHub Profile
@imvsharma
imvsharma / detectLoop.js
Last active July 8, 2021 14:42
Floyd's Cycle Detection Algo
/*
Complexity Analysis:
Time complexity: O(n).
Only one traversal of the loop is needed.
Auxiliary Space: O(1).
*/
detectLoop() {
let isLoopPresent = false;
@imvsharma
imvsharma / detectLoop.js
Last active July 8, 2021 11:52
Detect Loop using visited flag
// structure of node
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.visited = false;
}
}
/*
@imvsharma
imvsharma / detectLoop.js
Last active July 8, 2021 11:49
Detect Loop using Set
/*
Complexity Analysis:
Time complexity: O(n).
Only one traversal of the loop is needed.
Auxiliary Space: O(n).
n is the space required to store the value in hashmap.
*/
detectLoop() {
@imvsharma
imvsharma / createLoop.js
Created July 8, 2021 03:44
Create loop in linked list
createLoop(nNode) {
let temp = null;
let current = this.head;
let count = 1;
while(count < nNode) {
current = current.next;
count++
}
@imvsharma
imvsharma / singlyLinkedList.js
Created July 8, 2021 03:06
basic implementation of linked list in js
// structure of node
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
@imvsharma
imvsharma / mergeSort.js
Created September 1, 2020 11:17
Merge Sort
let arr = [38, 27, 43, 3, 9, 82, 10];
const customSort = function(arr) {
if(arr.length <= 1) return arr;
let mid = Math.ceil(arr.length/2);
let firstHalf= customSort(arr.splice(0, mid));
let secondHalf = customSort(arr.splice(-mid));
return merge(firstHalf, secondHalf);
@imvsharma
imvsharma / selectionSort.js
Created September 1, 2020 10:13
Selection Sort
let arr= [64,25,12,22,11];
const selectionSort = function(arr) {
if(arr.length=== 0) {
return arr;
}else {
for(let i = 0; i < arr.length-1; i++) {
let min = i;
for(let j = i+1; j < arr.length; j++) {
if(arr[j] < arr[min]) {
@imvsharma
imvsharma / binarySearch.js
Created September 1, 2020 10:13
Binary Search
let arr = [1, 3, 5, 7, 8, 9];
const binarySearch = function(arr, ele, start, end) {
if(start > end) {
return false;
}
let mid = Math.floor((start+end)/2);
console.log(mid);
@imvsharma
imvsharma / Priority-Queue.js
Created August 30, 2020 10:48
Priority Queue
/**
****
Priority Queue is an extension of queue with following properties:-
1. Every item has a priority associated with it.
2. An element with high priority is dequeued before an element with low priority.
3. If two elements have the same priority, they are served according to their order in the queue.
****
**/
@imvsharma
imvsharma / setting.json
Created October 6, 2019 08:41
vscode setting.json
{
"javascript.updateImportsOnFileMove.enabled": "always",
"workbench.colorTheme": "One Dark+ (Sublime)",
"explorer.confirmDragAndDrop": false,
"explorer.confirmDelete": false,
"workbench.startupEditor": "newUntitledFile",
"typescript.updateImportsOnFileMove.enabled": "always",
"git.enableSmartCommit": true,
"workbench.iconTheme": "vscode-icons",
"workbench.statusBar.visible": true,