From 852563a2eccd450426c29b5951bbb8288a22c049 Mon Sep 17 00:00:00 2001 From: itsvinayak Date: Mon, 4 May 2020 10:48:45 +0530 Subject: [PATCH 1/2] Data Structures/Graph --- Data Structures/Graph/Graph.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data Structures/Graph/Graph.js b/Data Structures/Graph/Graph.js index 5af996c0e1..ef1a952a86 100644 --- a/Data Structures/Graph/Graph.js +++ b/Data Structures/Graph/Graph.js @@ -1,4 +1,3 @@ - class Graph { constructor () { this.adjacencyMap = {} @@ -42,3 +41,4 @@ const example = () => { g.addEdge(1, 3) g.printGraph() } +example() From 622ad9e41f886fff9abd880082f1011f12b9d5b0 Mon Sep 17 00:00:00 2001 From: itsvinayak Date: Mon, 4 May 2020 11:58:44 +0530 Subject: [PATCH 2/2] sorts/ --- Sorts/TopologicalSort.js | 105 +++++++++++++++++++-------------------- Sorts/bubblesort.js | 95 ++++++++++++++++++----------------- Sorts/bucketSort.js | 4 +- Sorts/combSort.js | 3 +- Sorts/cycleSort.js | 9 ++-- Sorts/heapSort.js | 1 + Sorts/selectionSort.js | 3 +- 7 files changed, 112 insertions(+), 108 deletions(-) diff --git a/Sorts/TopologicalSort.js b/Sorts/TopologicalSort.js index 1c8b18a4d1..e784ad0e6b 100644 --- a/Sorts/TopologicalSort.js +++ b/Sorts/TopologicalSort.js @@ -1,60 +1,59 @@ -function TopologicalSorter() { - var graph = {}, - isVisitedNode, - finishTimeCount, - finishingTimeList, - nextNode; - - this.addOrder = function (nodeA, nodeB) { - nodeA = String(nodeA); - nodeB = String(nodeB); - graph[nodeA] = graph[nodeA] || []; - graph[nodeA].push(nodeB); - } - - this.sortAndGetOrderedItems = function () { - isVisitedNode = Object.create(null); - finishTimeCount = 0; - finishingTimeList = []; - - for (var node in graph) { - if (graph.hasOwnProperty(node) && !isVisitedNode[node]) { - dfsTraverse(node); - } - } - - finishingTimeList.sort(function (item1, item2) { - return item1.finishTime > item2.finishTime ? -1 : 1; - }); - - return finishingTimeList.map(function (value) { return value.node }) +function TopologicalSorter () { + var graph = {} + var isVisitedNode + var finishTimeCount + var finishingTimeList + var nextNode + + this.addOrder = function (nodeA, nodeB) { + nodeA = String(nodeA) + nodeB = String(nodeB) + graph[nodeA] = graph[nodeA] || [] + graph[nodeA].push(nodeB) + } + + this.sortAndGetOrderedItems = function () { + isVisitedNode = Object.create(null) + finishTimeCount = 0 + finishingTimeList = [] + + for (var node in graph) { + if (Object.prototype.hasOwnProperty.call(graph, node) && !isVisitedNode[node]) { + dfsTraverse(node) + } } - function dfsTraverse(node) { - isVisitedNode[node] = true; - if (graph[node]) { - for (var i = 0; i < graph[node].length; i++) { - nextNode = graph[node][i]; - if (isVisitedNode[nextNode]) continue; - dfsTraverse(nextNode); - } - } - - finishingTimeList.push({ - node: node, - finishTime: ++finishTimeCount - }); + finishingTimeList.sort(function (item1, item2) { + return item1.finishTime > item2.finishTime ? -1 : 1 + }) + + return finishingTimeList.map(function (value) { return value.node }) + } + + function dfsTraverse (node) { + isVisitedNode[node] = true + if (graph[node]) { + for (var i = 0; i < graph[node].length; i++) { + nextNode = graph[node][i] + if (isVisitedNode[nextNode]) continue + dfsTraverse(nextNode) + } } -} + finishingTimeList.push({ + node: node, + finishTime: ++finishTimeCount + }) + } +} /* TEST */ -var topoSorter = new TopologicalSorter(); -topoSorter.addOrder(5, 2); -topoSorter.addOrder(5, 0); -topoSorter.addOrder(4, 0); -topoSorter.addOrder(4, 1); -topoSorter.addOrder(2, 3); -topoSorter.addOrder(3, 1); -console.log(topoSorter.sortAndGetOrderedItems()); +var topoSorter = new TopologicalSorter() +topoSorter.addOrder(5, 2) +topoSorter.addOrder(5, 0) +topoSorter.addOrder(4, 0) +topoSorter.addOrder(4, 1) +topoSorter.addOrder(2, 3) +topoSorter.addOrder(3, 1) +console.log(topoSorter.sortAndGetOrderedItems()) diff --git a/Sorts/bubblesort.js b/Sorts/bubblesort.js index df2980b4e9..72c65dda1e 100644 --- a/Sorts/bubblesort.js +++ b/Sorts/bubblesort.js @@ -1,66 +1,65 @@ -/*Bubble Sort is a algorithm to sort an array. It +/* Bubble Sort is a algorithm to sort an array. It * compares adjacent element and swaps thier position -* The big O on bubble sort in worst and best case is O(N^2). +* The big O on bubble sort in worst and best case is O(N^2). * Not efficient. */ -function bubbleSort(items) { - let length = items.length; - for (let i = (length - 1); i > 0; i--) { - //Number of passes - for (let j = (length - i); j > 0; j--) { - //Compare the adjacent positions - if (items[j] < items[j - 1]) { - //Swap the numbers - let tmp = items[j]; - items[j] = items[j - 1]; - items[j - 1] = tmp; - } - } + +function bubbleSort (items) { + const length = items.length + for (let i = (length - 1); i > 0; i--) { + // Number of passes + for (let j = (length - i); j > 0; j--) { + // Compare the adjacent positions + if (items[j] < items[j - 1]) { + // Swap the numbers + const tmp = items[j] + items[j] = items[j - 1] + items[j - 1] = tmp + } } + } } -//Implementation of bubbleSort +// Implementation of bubbleSort -var ar=[5,6,7,8,1,2,12,14]; -//Array before Sort -console.log("-----before sorting-----") -console.log(ar); -bubbleSort(ar); -//Array after sort -console.log("-----after sorting-----") -console.log(ar); +var ar = [5, 6, 7, 8, 1, 2, 12, 14] +// Array before Sort +console.log('-----before sorting-----') +console.log(ar) +bubbleSort(ar) +// Array after sort +console.log('-----after sorting-----') +console.log(ar) -/*alternative implementation of bubble sort algorithm. +/* alternative implementation of bubble sort algorithm. Using a while loop instead. For educational purposses only */ /* *In bubble sort, we keep iterating while something was swapped in -*the previous inner-loop iteration. By swapped I mean, in the -*inner loop iteration, we check each number if the number proceeding +*the previous inner-loop iteration. By swapped I mean, in the +*inner loop iteration, we check each number if the number proceeding *it is greater than itself, if so we swap them. */ -function bubbleSort(arr){ - let swapped = true; - while(swapped){ - swapped = false; - for(let i = 0; i < arr.length-1; i++){ - if(arr[i] > arr[i + 1]){ - let temp = arr[i]; - arr[i] = arr[i + 1]; - arr[i + 1] = temp; - swapped = true; - } - } +function alternativeBubbleSort (arr) { + let swapped = true + while (swapped) { + swapped = false + for (let i = 0; i < arr.length - 1; i++) { + if (arr[i] > arr[i + 1]) { + const temp = arr[i] + arr[i] = arr[i + 1] + arr[i + 1] = temp + swapped = true + } } - return arr; + } + return arr } -//test -console.log("-----before sorting-----") -var array = [10,5,3,8,2,6,4,7,9,1]; -console.log(array); -console.log("-----after sorting-----") -console.log(bubbleSort(array)); - - +// test +console.log('-----before sorting-----') +var array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1] +console.log(array) +console.log('-----after sorting-----') +console.log(alternativeBubbleSort(array)) diff --git a/Sorts/bucketSort.js b/Sorts/bucketSort.js index 5c8346cd3b..1a7fe77130 100644 --- a/Sorts/bucketSort.js +++ b/Sorts/bucketSort.js @@ -52,9 +52,11 @@ function bucketSort (list, size) { } return sorted } + +// Testing const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14] // Array before Sort console.log(arrOrignal) -arrSorted = bucketSort(arrOrignal) +const arrSorted = bucketSort(arrOrignal) // Array after sort console.log(arrSorted) diff --git a/Sorts/combSort.js b/Sorts/combSort.js index 2bfa83b589..3241c59f82 100644 --- a/Sorts/combSort.js +++ b/Sorts/combSort.js @@ -14,6 +14,7 @@ elements goes down (for each iteration of outer loop) in steps of a "shrink factor" k: [ n/k, n/k2, n/k3, ..., 1 ]. */ + function combSort (list) { if (list.length === 0) { return list @@ -45,6 +46,6 @@ function combSort (list) { const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14] // Array before Sort console.log(arrOrignal) -arrSorted = combSort(arrOrignal) +const arrSorted = combSort(arrOrignal) // Array after sort console.log(arrSorted) diff --git a/Sorts/cycleSort.js b/Sorts/cycleSort.js index 9353b8b611..5853712974 100644 --- a/Sorts/cycleSort.js +++ b/Sorts/cycleSort.js @@ -5,6 +5,7 @@ number of writes to the original array, unlike any other in-place sorting algorithm. It is based on the idea that the permutation to be sorted can be factored into cycles, which can individually be rotated to give a sorted result. */ + function cycleSort (list) { let writes = 0 for (let cycleStart = 0; cycleStart < list.length; cycleStart++) { @@ -18,11 +19,11 @@ function cycleSort (list) { } } // if its the same continue - if (position == cycleStart) { + if (position === cycleStart) { continue } - while (value == list[position]) { + while (value === list[position]) { position++ } @@ -32,14 +33,14 @@ function cycleSort (list) { writes++ // rotate the rest - while (position != cycleStart) { + while (position !== cycleStart) { position = cycleStart for (let i = cycleStart + 1; i < list.length; i++) { if (list[i] < value) { position++ } } - while (value == list[position]) { + while (value === list[position]) { position++ } const oldValueCycle = list[position] diff --git a/Sorts/heapSort.js b/Sorts/heapSort.js index ce63769a5a..13baeddbbf 100644 --- a/Sorts/heapSort.js +++ b/Sorts/heapSort.js @@ -5,6 +5,7 @@ * key (the value) of node P is greater than the key of node C" * Source: https://en.wikipedia.org/wiki/Heap_(data_structure) */ + Array.prototype.heapify = function (index, heapSize) { let largest = index const leftIndex = 2 * index + 1 diff --git a/Sorts/selectionSort.js b/Sorts/selectionSort.js index 3c1d1fd2e0..a46b93ac46 100644 --- a/Sorts/selectionSort.js +++ b/Sorts/selectionSort.js @@ -7,6 +7,7 @@ *In every iteration of selection sort, the minimum element (considering ascending order) *from the unsorted subarray is picked and moved to the sorted subarray. */ + function selectionSort (items) { var length = items.length for (var i = 0; i < length - 1; i++) { @@ -17,7 +18,7 @@ function selectionSort (items) { min = j // Change the current min number position if a smaller num is found } } - if (min != i) { + if (min !== i) { // After each pass, if the current min num != initial min num, exchange the position. // Swap the numbers var tmp = items[i]