mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
style: Fixed most styles (according to standardjs)
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
function decimalToBinary (num) {
|
function decimalToBinary (num) {
|
||||||
var bin = []
|
const bin = []
|
||||||
while (num > 0) {
|
while (num > 0) {
|
||||||
bin.unshift(num % 2)
|
bin.unshift(num % 2)
|
||||||
num >>= 1 // basically /= 2 without remainder if any
|
num >>= 1 // basically /= 2 without remainder if any
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
function hexStringToRGB (hexString) {
|
function hexStringToRGB (hexString) {
|
||||||
var r = hexString.substring(0, 2)
|
let r = hexString.substring(0, 2)
|
||||||
var g = hexString.substring(2, 4)
|
let g = hexString.substring(2, 4)
|
||||||
var b = hexString.substring(4, 6)
|
let b = hexString.substring(4, 6)
|
||||||
|
|
||||||
r = parseInt(r, 16)
|
r = parseInt(r, 16)
|
||||||
g = parseInt(g, 16)
|
g = parseInt(g, 16)
|
||||||
b = parseInt(b, 16)
|
b = parseInt(b, 16)
|
||||||
var obj = { r, g, b }
|
const obj = { r, g, b }
|
||||||
|
|
||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
var values = {
|
const values = {
|
||||||
I: 1,
|
I: 1,
|
||||||
V: 5,
|
V: 5,
|
||||||
X: 10,
|
X: 10,
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
// class LinkedList and constructor
|
// class LinkedList and constructor
|
||||||
// Creates a LinkedList
|
// Creates a LinkedList
|
||||||
var LinkedList = (function () {
|
const LinkedList = (function () {
|
||||||
function LinkedList () {
|
function LinkedList () {
|
||||||
// Length of linklist and head is null at start
|
// Length of linklist and head is null at start
|
||||||
this.length = 0
|
this.length = 0
|
||||||
@ -19,7 +19,7 @@ var LinkedList = (function () {
|
|||||||
|
|
||||||
// class node (constructor)
|
// class node (constructor)
|
||||||
// Creating Node with element's value
|
// Creating Node with element's value
|
||||||
var Node = (function () {
|
const Node = (function () {
|
||||||
function Node (element) {
|
function Node (element) {
|
||||||
this.element = element
|
this.element = element
|
||||||
this.next = null
|
this.next = null
|
||||||
@ -39,12 +39,12 @@ var LinkedList = (function () {
|
|||||||
|
|
||||||
// Creates a node and adds it to linklist
|
// Creates a node and adds it to linklist
|
||||||
LinkedList.prototype.add = function (element) {
|
LinkedList.prototype.add = function (element) {
|
||||||
var node = new Node(element)
|
const node = new Node(element)
|
||||||
// Check if its the first element
|
// Check if its the first element
|
||||||
if (this.head === null) {
|
if (this.head === null) {
|
||||||
this.head = node
|
this.head = node
|
||||||
} else {
|
} else {
|
||||||
var currentNode = this.head
|
let currentNode = this.head
|
||||||
|
|
||||||
// Loop till there is node present in the list
|
// Loop till there is node present in the list
|
||||||
while (currentNode.next) {
|
while (currentNode.next) {
|
||||||
@ -60,8 +60,8 @@ var LinkedList = (function () {
|
|||||||
|
|
||||||
// Removes the node with the value as param
|
// Removes the node with the value as param
|
||||||
LinkedList.prototype.remove = function (element) {
|
LinkedList.prototype.remove = function (element) {
|
||||||
var currentNode = this.head
|
let currentNode = this.head
|
||||||
var previousNode
|
let previousNode
|
||||||
|
|
||||||
// Check if the head node is the element to remove
|
// Check if the head node is the element to remove
|
||||||
if (currentNode.element === element) {
|
if (currentNode.element === element) {
|
||||||
@ -88,8 +88,8 @@ var LinkedList = (function () {
|
|||||||
|
|
||||||
// Returns the index of the element passed as param otherwise -1
|
// Returns the index of the element passed as param otherwise -1
|
||||||
LinkedList.prototype.indexOf = function (element) {
|
LinkedList.prototype.indexOf = function (element) {
|
||||||
var currentNode = this.head
|
let currentNode = this.head
|
||||||
var index = -1
|
let index = -1
|
||||||
|
|
||||||
while (currentNode) {
|
while (currentNode) {
|
||||||
index++
|
index++
|
||||||
@ -106,8 +106,8 @@ var LinkedList = (function () {
|
|||||||
|
|
||||||
// Returns the element at an index
|
// Returns the element at an index
|
||||||
LinkedList.prototype.elementAt = function (index) {
|
LinkedList.prototype.elementAt = function (index) {
|
||||||
var currentNode = this.head
|
let currentNode = this.head
|
||||||
var count = 0
|
let count = 0
|
||||||
while (count < index) {
|
while (count < index) {
|
||||||
count++
|
count++
|
||||||
currentNode = currentNode.next
|
currentNode = currentNode.next
|
||||||
@ -118,11 +118,11 @@ var LinkedList = (function () {
|
|||||||
// Adds the element at specified index
|
// Adds the element at specified index
|
||||||
LinkedList.prototype.addAt = function (index, element) {
|
LinkedList.prototype.addAt = function (index, element) {
|
||||||
index--
|
index--
|
||||||
var node = new Node(element)
|
const node = new Node(element)
|
||||||
|
|
||||||
var currentNode = this.head
|
let currentNode = this.head
|
||||||
var previousNode
|
let previousNode
|
||||||
var currentIndex = 0
|
let currentIndex = 0
|
||||||
|
|
||||||
// Check if index is out of bounds of list
|
// Check if index is out of bounds of list
|
||||||
if (index > this.length) {
|
if (index > this.length) {
|
||||||
@ -153,9 +153,9 @@ var LinkedList = (function () {
|
|||||||
// Removes the node at specified index
|
// Removes the node at specified index
|
||||||
LinkedList.prototype.removeAt = function (index) {
|
LinkedList.prototype.removeAt = function (index) {
|
||||||
index--
|
index--
|
||||||
var currentNode = this.head
|
let currentNode = this.head
|
||||||
var previousNode
|
let previousNode
|
||||||
var currentIndex = 0
|
let currentIndex = 0
|
||||||
|
|
||||||
// Check if index is present in list
|
// Check if index is present in list
|
||||||
if (index < 0 || index >= this.length) {
|
if (index < 0 || index >= this.length) {
|
||||||
@ -181,8 +181,8 @@ var LinkedList = (function () {
|
|||||||
|
|
||||||
// Function to view the LinkedList
|
// Function to view the LinkedList
|
||||||
LinkedList.prototype.view = function () {
|
LinkedList.prototype.view = function () {
|
||||||
var currentNode = this.head
|
let currentNode = this.head
|
||||||
var count = 0
|
let count = 0
|
||||||
while (count < this.length) {
|
while (count < this.length) {
|
||||||
count++
|
count++
|
||||||
console.log(currentNode.element)
|
console.log(currentNode.element)
|
||||||
@ -195,7 +195,7 @@ var LinkedList = (function () {
|
|||||||
}())
|
}())
|
||||||
|
|
||||||
// Implementation of LinkedList
|
// Implementation of LinkedList
|
||||||
var linklist = new LinkedList()
|
const linklist = new LinkedList()
|
||||||
linklist.add(2)
|
linklist.add(2)
|
||||||
linklist.add(5)
|
linklist.add(5)
|
||||||
linklist.add(8)
|
linklist.add(8)
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
// Functions: push, pop, peek, view, length
|
// Functions: push, pop, peek, view, length
|
||||||
|
|
||||||
// Creates a stack constructor
|
// Creates a stack constructor
|
||||||
var Stack = (function () {
|
const Stack = (function () {
|
||||||
function Stack () {
|
function Stack () {
|
||||||
// The top of the Stack
|
// The top of the Stack
|
||||||
this.top = 0
|
this.top = 0
|
||||||
@ -29,7 +29,7 @@ var Stack = (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.top--
|
this.top--
|
||||||
var result = this.stack[this.top]
|
const result = this.stack[this.top]
|
||||||
this.stack = this.stack.splice(0, this.top)
|
this.stack = this.stack.splice(0, this.top)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@ -46,14 +46,14 @@ var Stack = (function () {
|
|||||||
|
|
||||||
// To see all the elements in the stack
|
// To see all the elements in the stack
|
||||||
Stack.prototype.view = function () {
|
Stack.prototype.view = function () {
|
||||||
for (var i = 0; i < this.top; i++) { console.log(this.stack[i]) }
|
for (let i = 0; i < this.top; i++) { console.log(this.stack[i]) }
|
||||||
}
|
}
|
||||||
|
|
||||||
return Stack
|
return Stack
|
||||||
}())
|
}())
|
||||||
|
|
||||||
// Implementation
|
// Implementation
|
||||||
var myStack = new Stack()
|
const myStack = new Stack()
|
||||||
|
|
||||||
myStack.push(1)
|
myStack.push(1)
|
||||||
myStack.push(5)
|
myStack.push(5)
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// class Node
|
// class Node
|
||||||
var Node = (function () {
|
const Node = (function () {
|
||||||
// Node in the tree
|
// Node in the tree
|
||||||
function Node (val) {
|
function Node (val) {
|
||||||
this.value = val
|
this.value = val
|
||||||
@ -67,7 +67,7 @@ var Node = (function () {
|
|||||||
}())
|
}())
|
||||||
|
|
||||||
// class Tree
|
// class Tree
|
||||||
var Tree = (function () {
|
const Tree = (function () {
|
||||||
function Tree () {
|
function Tree () {
|
||||||
// Just store the root
|
// Just store the root
|
||||||
this.root = null
|
this.root = null
|
||||||
@ -103,7 +103,7 @@ var Tree = (function () {
|
|||||||
}())
|
}())
|
||||||
|
|
||||||
// Implementation of BST
|
// Implementation of BST
|
||||||
var bst = new Tree()
|
const bst = new Tree()
|
||||||
bst.addValue(6)
|
bst.addValue(6)
|
||||||
bst.addValue(3)
|
bst.addValue(3)
|
||||||
bst.addValue(9)
|
bst.addValue(9)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
var TrieNode = function TrieNode (key, parent) {
|
const TrieNode = function TrieNode (key, parent) {
|
||||||
this.key = key
|
this.key = key
|
||||||
this.count = 0
|
this.count = 0
|
||||||
this.children = Object.create(null)
|
this.children = Object.create(null)
|
||||||
@ -20,7 +20,7 @@ Trie.findAllWords = function (root, word, output) {
|
|||||||
if (root.count > 0) {
|
if (root.count > 0) {
|
||||||
if (typeof output === 'object') { output.push({ word: word, count: root.count }) }
|
if (typeof output === 'object') { output.push({ word: word, count: root.count }) }
|
||||||
}
|
}
|
||||||
var key
|
let key
|
||||||
for (key in root.children) {
|
for (key in root.children) {
|
||||||
word += key
|
word += key
|
||||||
this.findAllWords(root.children[key], word, output)
|
this.findAllWords(root.children[key], word, output)
|
||||||
@ -34,9 +34,9 @@ Trie.prototype.insert = function (word) {
|
|||||||
this.root.count += 1
|
this.root.count += 1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var node = this.root
|
let node = this.root
|
||||||
var len = word.length
|
const len = word.length
|
||||||
var i
|
let i
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
if (node.children[word.charAt(i)] === undefined) { node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node) }
|
if (node.children[word.charAt(i)] === undefined) { node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node) }
|
||||||
node = node.children[word.charAt(i)]
|
node = node.children[word.charAt(i)]
|
||||||
@ -46,9 +46,9 @@ Trie.prototype.insert = function (word) {
|
|||||||
|
|
||||||
Trie.prototype.findPrefix = function (word) {
|
Trie.prototype.findPrefix = function (word) {
|
||||||
if (typeof word !== 'string') return null
|
if (typeof word !== 'string') return null
|
||||||
var node = this.root
|
let node = this.root
|
||||||
var len = word.length
|
const len = word.length
|
||||||
var i
|
let i
|
||||||
// After end of this loop node will be at desired prefix
|
// After end of this loop node will be at desired prefix
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
if (node.children[word.charAt(i)] === undefined) return null // No such prefix exists
|
if (node.children[word.charAt(i)] === undefined) return null // No such prefix exists
|
||||||
@ -69,9 +69,9 @@ Trie.prototype.remove = function (word, count) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var child = this.root
|
let child = this.root
|
||||||
var len = word.length
|
const len = word.length
|
||||||
var i, key
|
let i, key
|
||||||
// child: node which is to be deleted
|
// child: node which is to be deleted
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
key = word.charAt(i)
|
key = word.charAt(i)
|
||||||
@ -93,9 +93,9 @@ Trie.prototype.remove = function (word, count) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Trie.prototype.findAllWords = function (prefix) {
|
Trie.prototype.findAllWords = function (prefix) {
|
||||||
var output = []
|
const output = []
|
||||||
// find the node with provided prefix
|
// find the node with provided prefix
|
||||||
var node = this.findPrefix(prefix)
|
const node = this.findPrefix(prefix)
|
||||||
// No such prefix exists
|
// No such prefix exists
|
||||||
if (node === null) return output
|
if (node === null) return output
|
||||||
Trie.findAllWords(node, prefix, output)
|
Trie.findAllWords(node, prefix, output)
|
||||||
@ -104,7 +104,7 @@ Trie.prototype.findAllWords = function (prefix) {
|
|||||||
|
|
||||||
Trie.prototype.contains = function (word) {
|
Trie.prototype.contains = function (word) {
|
||||||
// find the node with given prefix
|
// find the node with given prefix
|
||||||
var node = this.findPrefix(word)
|
const node = this.findPrefix(word)
|
||||||
// No such word exists
|
// No such word exists
|
||||||
if (node === null || node.count === 0) return false
|
if (node === null || node.count === 0) return false
|
||||||
return true
|
return true
|
||||||
@ -112,7 +112,7 @@ Trie.prototype.contains = function (word) {
|
|||||||
|
|
||||||
Trie.prototype.findOccurences = function (word) {
|
Trie.prototype.findOccurences = function (word) {
|
||||||
// find the node with given prefix
|
// find the node with given prefix
|
||||||
var node = this.findPrefix(word)
|
const node = this.findPrefix(word)
|
||||||
// No such word exists
|
// No such word exists
|
||||||
if (node === null) return 0
|
if (node === null) return 0
|
||||||
return node.count
|
return node.count
|
||||||
@ -120,7 +120,7 @@ Trie.prototype.findOccurences = function (word) {
|
|||||||
|
|
||||||
// To test
|
// To test
|
||||||
(function demo () {
|
(function demo () {
|
||||||
var x = new Trie()
|
const x = new Trie()
|
||||||
x.insert('sheldon')
|
x.insert('sheldon')
|
||||||
x.insert('hello')
|
x.insert('hello')
|
||||||
x.insert('anyword')
|
x.insert('anyword')
|
||||||
|
@ -5,13 +5,13 @@
|
|||||||
The namespace LinearAlgebra contains useful classes and functions for dealing with
|
The namespace LinearAlgebra contains useful classes and functions for dealing with
|
||||||
linear algebra under JavaScript.
|
linear algebra under JavaScript.
|
||||||
*/
|
*/
|
||||||
var LinearAlgebra;
|
let LinearAlgebra;
|
||||||
(function (LinearAlgebra) {
|
(function (LinearAlgebra) {
|
||||||
/*
|
/*
|
||||||
class: Vector
|
class: Vector
|
||||||
This class represents a vector of arbitrary size and operations on it.
|
This class represents a vector of arbitrary size and operations on it.
|
||||||
*/
|
*/
|
||||||
var Vector = /** @class */ (function () {
|
const Vector = /** @class */ (function () {
|
||||||
// constructor
|
// constructor
|
||||||
function Vector (N, comps) {
|
function Vector (N, comps) {
|
||||||
if (comps === undefined) {
|
if (comps === undefined) {
|
||||||
@ -19,7 +19,7 @@ var LinearAlgebra;
|
|||||||
}
|
}
|
||||||
this.components = new Array(N)
|
this.components = new Array(N)
|
||||||
if (comps.length === 0) {
|
if (comps.length === 0) {
|
||||||
for (var i = 0; i < N; i++) {
|
for (let i = 0; i < N; i++) {
|
||||||
this.components[i] = 0.0
|
this.components[i] = 0.0
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -37,8 +37,8 @@ var LinearAlgebra;
|
|||||||
}
|
}
|
||||||
// computes the eulidean length.
|
// computes the eulidean length.
|
||||||
Vector.prototype.eulideanLength = function () {
|
Vector.prototype.eulideanLength = function () {
|
||||||
var sum = 0
|
let sum = 0
|
||||||
for (var i = 0; i < this.components.length; i++) {
|
for (let i = 0; i < this.components.length; i++) {
|
||||||
sum += this.components[i] * this.components[i]
|
sum += this.components[i] * this.components[i]
|
||||||
}
|
}
|
||||||
return Math.sqrt(sum)
|
return Math.sqrt(sum)
|
||||||
@ -59,9 +59,9 @@ var LinearAlgebra;
|
|||||||
// vector addition
|
// vector addition
|
||||||
Vector.prototype.add = function (other) {
|
Vector.prototype.add = function (other) {
|
||||||
if (this.size() === other.size()) {
|
if (this.size() === other.size()) {
|
||||||
var SIZE = this.size()
|
const SIZE = this.size()
|
||||||
var ans = new Vector(SIZE)
|
const ans = new Vector(SIZE)
|
||||||
for (var i = 0; i < SIZE; i++) {
|
for (let i = 0; i < SIZE; i++) {
|
||||||
ans.changeComponent(i, (this.components[i] + other.component(i)))
|
ans.changeComponent(i, (this.components[i] + other.component(i)))
|
||||||
}
|
}
|
||||||
return ans
|
return ans
|
||||||
@ -72,9 +72,9 @@ var LinearAlgebra;
|
|||||||
// vector subtraction
|
// vector subtraction
|
||||||
Vector.prototype.sub = function (other) {
|
Vector.prototype.sub = function (other) {
|
||||||
if (this.size() === other.size()) {
|
if (this.size() === other.size()) {
|
||||||
var SIZE = this.size()
|
const SIZE = this.size()
|
||||||
var ans = new Vector(SIZE)
|
const ans = new Vector(SIZE)
|
||||||
for (var i = 0; i < SIZE; i++) {
|
for (let i = 0; i < SIZE; i++) {
|
||||||
ans.changeComponent(i, (this.components[i] - other.component(i)))
|
ans.changeComponent(i, (this.components[i] - other.component(i)))
|
||||||
}
|
}
|
||||||
return ans
|
return ans
|
||||||
@ -84,10 +84,10 @@ var LinearAlgebra;
|
|||||||
}
|
}
|
||||||
// dot-product
|
// dot-product
|
||||||
Vector.prototype.dot = function (other) {
|
Vector.prototype.dot = function (other) {
|
||||||
var sum = 0
|
let sum = 0
|
||||||
if (other.size() === this.size()) {
|
if (other.size() === this.size()) {
|
||||||
var SIZE = other.size()
|
const SIZE = other.size()
|
||||||
for (var i = 0; i < SIZE; i++) {
|
for (let i = 0; i < SIZE; i++) {
|
||||||
sum += this.components[i] * other.component(i)
|
sum += this.components[i] * other.component(i)
|
||||||
}
|
}
|
||||||
return sum
|
return sum
|
||||||
@ -97,18 +97,18 @@ var LinearAlgebra;
|
|||||||
}
|
}
|
||||||
// scalar multiplication
|
// scalar multiplication
|
||||||
Vector.prototype.scalar = function (s) {
|
Vector.prototype.scalar = function (s) {
|
||||||
var SIZE = this.size()
|
const SIZE = this.size()
|
||||||
var ans = new Vector(SIZE)
|
const ans = new Vector(SIZE)
|
||||||
for (var i = 0; i < SIZE; i++) {
|
for (let i = 0; i < SIZE; i++) {
|
||||||
ans.changeComponent(i, (this.components[i] * s))
|
ans.changeComponent(i, (this.components[i] * s))
|
||||||
}
|
}
|
||||||
return ans
|
return ans
|
||||||
}
|
}
|
||||||
// returns a string representation of this vector.
|
// returns a string representation of this vector.
|
||||||
Vector.prototype.toString = function () {
|
Vector.prototype.toString = function () {
|
||||||
var ans = '('
|
let ans = '('
|
||||||
var SIZE = this.components.length
|
const SIZE = this.components.length
|
||||||
for (var i = 0; i < SIZE; i++) {
|
for (let i = 0; i < SIZE; i++) {
|
||||||
if (i < SIZE - 1) {
|
if (i < SIZE - 1) {
|
||||||
ans += this.components[i] + ','
|
ans += this.components[i] + ','
|
||||||
} else {
|
} else {
|
||||||
@ -121,7 +121,7 @@ var LinearAlgebra;
|
|||||||
// the One is on position 'pos'
|
// the One is on position 'pos'
|
||||||
Vector.prototype.createUnitBasis = function (pos) {
|
Vector.prototype.createUnitBasis = function (pos) {
|
||||||
if (pos >= 0 && pos < this.components.length) {
|
if (pos >= 0 && pos < this.components.length) {
|
||||||
for (var i = 0; i < this.components.length; i++) {
|
for (let i = 0; i < this.components.length; i++) {
|
||||||
if (i === pos) {
|
if (i === pos) {
|
||||||
this.components[i] = 1.0
|
this.components[i] = 1.0
|
||||||
} else {
|
} else {
|
||||||
@ -135,20 +135,20 @@ var LinearAlgebra;
|
|||||||
}
|
}
|
||||||
// normalizes this vector and returns it.
|
// normalizes this vector and returns it.
|
||||||
Vector.prototype.norm = function () {
|
Vector.prototype.norm = function () {
|
||||||
var SIZE = this.size()
|
const SIZE = this.size()
|
||||||
var quotient = 1.0 / this.eulideanLength()
|
const quotient = 1.0 / this.eulideanLength()
|
||||||
for (var i = 0; i < SIZE; i++) {
|
for (let i = 0; i < SIZE; i++) {
|
||||||
this.components[i] = this.components[i] * quotient
|
this.components[i] = this.components[i] * quotient
|
||||||
}
|
}
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
// returns true if the vectors are equal otherwise false.
|
// returns true if the vectors are equal otherwise false.
|
||||||
Vector.prototype.equal = function (other) {
|
Vector.prototype.equal = function (other) {
|
||||||
var ans = true
|
let ans = true
|
||||||
var SIZE = this.size()
|
const SIZE = this.size()
|
||||||
var EPSILON = 0.001
|
const EPSILON = 0.001
|
||||||
if (SIZE === other.size()) {
|
if (SIZE === other.size()) {
|
||||||
for (var i = 0; i < SIZE; i++) {
|
for (let i = 0; i < SIZE; i++) {
|
||||||
if (Math.abs(this.components[i] - other.component(i)) > EPSILON) {
|
if (Math.abs(this.components[i] - other.component(i)) > EPSILON) {
|
||||||
ans = false
|
ans = false
|
||||||
}
|
}
|
||||||
@ -164,8 +164,8 @@ var LinearAlgebra;
|
|||||||
// -------------- global functions ---------------------------------
|
// -------------- global functions ---------------------------------
|
||||||
// returns a unit basis vector of size N with a One on position 'pos'
|
// returns a unit basis vector of size N with a One on position 'pos'
|
||||||
function unitBasisVector (N, pos) {
|
function unitBasisVector (N, pos) {
|
||||||
var ans = new Vector(N)
|
const ans = new Vector(N)
|
||||||
for (var i = 0; i < N; i++) {
|
for (let i = 0; i < N; i++) {
|
||||||
if (i === pos) {
|
if (i === pos) {
|
||||||
ans.changeComponent(i, 1.0)
|
ans.changeComponent(i, 1.0)
|
||||||
} else {
|
} else {
|
||||||
@ -177,8 +177,8 @@ var LinearAlgebra;
|
|||||||
LinearAlgebra.unitBasisVector = unitBasisVector
|
LinearAlgebra.unitBasisVector = unitBasisVector
|
||||||
// returns a random vector with integer components (between 'a' and 'b') of size N.
|
// returns a random vector with integer components (between 'a' and 'b') of size N.
|
||||||
function randomVectorInt (N, a, b) {
|
function randomVectorInt (N, a, b) {
|
||||||
var ans = new Vector(N)
|
const ans = new Vector(N)
|
||||||
for (var i = 0; i < N; i++) {
|
for (let i = 0; i < N; i++) {
|
||||||
ans.changeComponent(i, (Math.floor((Math.random() * b) + a)))
|
ans.changeComponent(i, (Math.floor((Math.random() * b) + a)))
|
||||||
}
|
}
|
||||||
return ans
|
return ans
|
||||||
@ -186,8 +186,8 @@ var LinearAlgebra;
|
|||||||
LinearAlgebra.randomVectorInt = randomVectorInt
|
LinearAlgebra.randomVectorInt = randomVectorInt
|
||||||
// returns a random vector with floating point components (between 'a' and 'b') of size N.
|
// returns a random vector with floating point components (between 'a' and 'b') of size N.
|
||||||
function randomVectorFloat (N, a, b) {
|
function randomVectorFloat (N, a, b) {
|
||||||
var ans = new Vector(N)
|
const ans = new Vector(N)
|
||||||
for (var i = 0; i < N; i++) {
|
for (let i = 0; i < N; i++) {
|
||||||
ans.changeComponent(i, ((Math.random() * b) + a))
|
ans.changeComponent(i, ((Math.random() * b) + a))
|
||||||
}
|
}
|
||||||
return ans
|
return ans
|
||||||
@ -198,7 +198,7 @@ var LinearAlgebra;
|
|||||||
class: Matrix
|
class: Matrix
|
||||||
This class represents a matrix of arbitrary size and operations on it.
|
This class represents a matrix of arbitrary size and operations on it.
|
||||||
*/
|
*/
|
||||||
var Matrix = /** @class */ (function () {
|
const Matrix = /** @class */ (function () {
|
||||||
// constructor for zero-matrix or fix number matrix.
|
// constructor for zero-matrix or fix number matrix.
|
||||||
function Matrix (row, col, comps) {
|
function Matrix (row, col, comps) {
|
||||||
if (comps === undefined) {
|
if (comps === undefined) {
|
||||||
@ -206,9 +206,9 @@ var LinearAlgebra;
|
|||||||
}
|
}
|
||||||
if (comps.length === 0) {
|
if (comps.length === 0) {
|
||||||
this.matrix = []
|
this.matrix = []
|
||||||
var rowVector = []
|
let rowVector = []
|
||||||
for (var i = 0; i < row; i++) {
|
for (let i = 0; i < row; i++) {
|
||||||
for (var j = 0; j < col; j++) {
|
for (let j = 0; j < col; j++) {
|
||||||
rowVector[j] = 0
|
rowVector[j] = 0
|
||||||
}
|
}
|
||||||
this.matrix[i] = rowVector
|
this.matrix[i] = rowVector
|
||||||
@ -238,10 +238,10 @@ var LinearAlgebra;
|
|||||||
}
|
}
|
||||||
// returns a string representation of this matrix.
|
// returns a string representation of this matrix.
|
||||||
Matrix.prototype.toString = function () {
|
Matrix.prototype.toString = function () {
|
||||||
var ans = ''
|
let ans = ''
|
||||||
for (var i = 0; i < this.rows; i++) {
|
for (let i = 0; i < this.rows; i++) {
|
||||||
ans += '|'
|
ans += '|'
|
||||||
for (var j = 0; j < this.cols; j++) {
|
for (let j = 0; j < this.cols; j++) {
|
||||||
if (j < this.cols - 1) {
|
if (j < this.cols - 1) {
|
||||||
ans += this.matrix[i][j] + ','
|
ans += this.matrix[i][j] + ','
|
||||||
} else {
|
} else {
|
||||||
@ -257,7 +257,7 @@ var LinearAlgebra;
|
|||||||
}
|
}
|
||||||
// returns the dimension rows x cols as number array
|
// returns the dimension rows x cols as number array
|
||||||
Matrix.prototype.dimension = function () {
|
Matrix.prototype.dimension = function () {
|
||||||
var ans = []
|
const ans = []
|
||||||
ans[0] = this.rows
|
ans[0] = this.rows
|
||||||
ans[1] = this.cols
|
ans[1] = this.cols
|
||||||
return ans
|
return ans
|
||||||
@ -266,9 +266,9 @@ var LinearAlgebra;
|
|||||||
Matrix.prototype.add = function (other) {
|
Matrix.prototype.add = function (other) {
|
||||||
if (this.rows === other.dimension()[0] &&
|
if (this.rows === other.dimension()[0] &&
|
||||||
this.cols === other.dimension()[1]) {
|
this.cols === other.dimension()[1]) {
|
||||||
var ans = new Matrix(this.rows, this.cols)
|
const ans = new Matrix(this.rows, this.cols)
|
||||||
for (var i = 0; i < this.rows; i++) {
|
for (let i = 0; i < this.rows; i++) {
|
||||||
for (var j = 0; j < this.cols; j++) {
|
for (let j = 0; j < this.cols; j++) {
|
||||||
ans.changeComponent(i, j, (this.matrix[i][j] + other.component(i, j)))
|
ans.changeComponent(i, j, (this.matrix[i][j] + other.component(i, j)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -279,12 +279,12 @@ var LinearAlgebra;
|
|||||||
}
|
}
|
||||||
// returns true if the matrices are equal, otherwise false.
|
// returns true if the matrices are equal, otherwise false.
|
||||||
Matrix.prototype.equal = function (other) {
|
Matrix.prototype.equal = function (other) {
|
||||||
var ans = true
|
let ans = true
|
||||||
var EPSILON = 0.001
|
const EPSILON = 0.001
|
||||||
if (this.rows === other.dimension()[0] &&
|
if (this.rows === other.dimension()[0] &&
|
||||||
this.cols === other.dimension()[1]) {
|
this.cols === other.dimension()[1]) {
|
||||||
for (var i = 0; i < this.rows; i++) {
|
for (let i = 0; i < this.rows; i++) {
|
||||||
for (var j = 0; j < this.cols; j++) {
|
for (let j = 0; j < this.cols; j++) {
|
||||||
if (Math.abs(this.matrix[i][j] - other.component(i, j)) > EPSILON) {
|
if (Math.abs(this.matrix[i][j] - other.component(i, j)) > EPSILON) {
|
||||||
ans = false
|
ans = false
|
||||||
}
|
}
|
||||||
@ -297,9 +297,9 @@ var LinearAlgebra;
|
|||||||
}
|
}
|
||||||
// matrix-scalar multiplication
|
// matrix-scalar multiplication
|
||||||
Matrix.prototype.scalar = function (c) {
|
Matrix.prototype.scalar = function (c) {
|
||||||
var ans = new Matrix(this.rows, this.cols)
|
const ans = new Matrix(this.rows, this.cols)
|
||||||
for (var i = 0; i < this.rows; i++) {
|
for (let i = 0; i < this.rows; i++) {
|
||||||
for (var j = 0; j < this.cols; j++) {
|
for (let j = 0; j < this.cols; j++) {
|
||||||
ans.changeComponent(i, j, (this.matrix[i][j] * c))
|
ans.changeComponent(i, j, (this.matrix[i][j] * c))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ const FibonacciDpWithoutRecursion = (number) => {
|
|||||||
const table = []
|
const table = []
|
||||||
table.push(1)
|
table.push(1)
|
||||||
table.push(1)
|
table.push(1)
|
||||||
for (var i = 2; i < number; ++i) {
|
for (let i = 2; i < number; ++i) {
|
||||||
table.push(table[i - 1] + table[i - 2])
|
table.push(table[i - 1] + table[i - 2])
|
||||||
}
|
}
|
||||||
return table
|
return table
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
* have been searched.
|
* have been searched.
|
||||||
*/
|
*/
|
||||||
function SearchArray (searchNum, ar) {
|
function SearchArray (searchNum, ar) {
|
||||||
var position = Search(ar, searchNum)
|
const position = Search(ar, searchNum)
|
||||||
if (position !== -1) {
|
if (position !== -1) {
|
||||||
console.log('The element was found at ' + (position + 1))
|
console.log('The element was found at ' + (position + 1))
|
||||||
} else {
|
} else {
|
||||||
@ -15,13 +15,13 @@ function SearchArray (searchNum, ar) {
|
|||||||
|
|
||||||
// Search “theArray” for the specified “key” value
|
// Search “theArray” for the specified “key” value
|
||||||
function Search (theArray, key) {
|
function Search (theArray, key) {
|
||||||
for (var n = 0; n < theArray.length; n++) {
|
for (let n = 0; n < theArray.length; n++) {
|
||||||
if (theArray[n] === key) { return n }
|
if (theArray[n] === key) { return n }
|
||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
SearchArray(3, ar)
|
SearchArray(3, ar)
|
||||||
SearchArray(4, ar)
|
SearchArray(4, ar)
|
||||||
SearchArray(11, ar)
|
SearchArray(11, ar)
|
||||||
|
@ -23,7 +23,7 @@ function gnomeSort (items) {
|
|||||||
|
|
||||||
// Implementation of gnomeSort
|
// Implementation of gnomeSort
|
||||||
|
|
||||||
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
const ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
// Array before Sort
|
// Array before Sort
|
||||||
console.log(ar)
|
console.log(ar)
|
||||||
gnomeSort(ar)
|
gnomeSort(ar)
|
||||||
|
@ -50,7 +50,7 @@ function heapSort (items) {
|
|||||||
|
|
||||||
// Implementation of heapSort
|
// Implementation of heapSort
|
||||||
|
|
||||||
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
const ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
// Array before Sort
|
// Array before Sort
|
||||||
console.log(ar)
|
console.log(ar)
|
||||||
heapSort(ar)
|
heapSort(ar)
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
* the correct position and expand sorted part one element at a time.
|
* the correct position and expand sorted part one element at a time.
|
||||||
*/
|
*/
|
||||||
function insertionSort (unsortedList) {
|
function insertionSort (unsortedList) {
|
||||||
var len = unsortedList.length
|
const len = unsortedList.length
|
||||||
for (var i = 1; i < len; i++) {
|
for (let i = 1; i < len; i++) {
|
||||||
var tmp = unsortedList[i] // Copy of the current element.
|
const tmp = unsortedList[i] // Copy of the current element.
|
||||||
/* Check through the sorted part and compare with the number in tmp. If large, shift the number */
|
/* Check through the sorted part and compare with the number in tmp. If large, shift the number */
|
||||||
for (var j = i - 1; j >= 0 && (unsortedList[j] > tmp); j--) {
|
for (var j = i - 1; j >= 0 && (unsortedList[j] > tmp); j--) {
|
||||||
// Shift the number
|
// Shift the number
|
||||||
@ -19,6 +19,6 @@ function insertionSort (unsortedList) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var arr = [5, 3, 1, 2, 4, 8, 3, 8]
|
const arr = [5, 3, 1, 2, 4, 8, 3, 8]
|
||||||
insertionSort(arr)
|
insertionSort(arr)
|
||||||
console.log(arr)
|
console.log(arr)
|
||||||
|
@ -28,12 +28,12 @@ function introsort (array, compare) {
|
|||||||
* 0 if a is equal to b
|
* 0 if a is equal to b
|
||||||
* 1 if a greater than b
|
* 1 if a greater than b
|
||||||
*/
|
*/
|
||||||
var defaultComparator = function (x, y) {
|
const defaultComparator = function (x, y) {
|
||||||
if (x === undefined && y === undefined) return 0
|
if (x === undefined && y === undefined) return 0
|
||||||
if (x === undefined) return 1
|
if (x === undefined) return 1
|
||||||
if (y === undefined) return -1
|
if (y === undefined) return -1
|
||||||
var xString = toString(x)
|
const xString = toString(x)
|
||||||
var yString = toString(y)
|
const yString = toString(y)
|
||||||
if (xString < yString) return -1
|
if (xString < yString) return -1
|
||||||
if (xString > yString) return 1
|
if (xString > yString) return 1
|
||||||
return 0
|
return 0
|
||||||
@ -75,8 +75,8 @@ function introsort (array, compare) {
|
|||||||
* [IIFE](https://en.wikipedia.org/wiki/Immediately_invoked_function_expression)
|
* [IIFE](https://en.wikipedia.org/wiki/Immediately_invoked_function_expression)
|
||||||
*/
|
*/
|
||||||
return (function (array, comparator) {
|
return (function (array, comparator) {
|
||||||
var swap = function (index1, index2) {
|
const swap = function (index1, index2) {
|
||||||
var temp = array[index1]
|
const temp = array[index1]
|
||||||
array[index1] = array[index2]
|
array[index1] = array[index2]
|
||||||
array[index2] = temp
|
array[index2] = temp
|
||||||
}
|
}
|
||||||
@ -85,14 +85,14 @@ function introsort (array, compare) {
|
|||||||
* If the length of array is less than
|
* If the length of array is less than
|
||||||
* this then we simply perform insertion sort
|
* this then we simply perform insertion sort
|
||||||
*/
|
*/
|
||||||
var THRESHOLD = 16
|
const THRESHOLD = 16
|
||||||
/**
|
/**
|
||||||
* @constant TUNEMAXDEPTH
|
* @constant TUNEMAXDEPTH
|
||||||
* Constant usec to increase or decrease value
|
* Constant usec to increase or decrease value
|
||||||
* of maxDepth
|
* of maxDepth
|
||||||
*/
|
*/
|
||||||
var TUNEMAXDEPTH = 1
|
const TUNEMAXDEPTH = 1
|
||||||
var len = array.length
|
const len = array.length
|
||||||
/**
|
/**
|
||||||
* Return if array is only of length 1
|
* Return if array is only of length 1
|
||||||
* Array of size 1 is always sorted
|
* Array of size 1 is always sorted
|
||||||
@ -104,7 +104,7 @@ function introsort (array, compare) {
|
|||||||
* Calculate maxDepth = log2(len)
|
* Calculate maxDepth = log2(len)
|
||||||
* Taken from implementation in stdc++
|
* Taken from implementation in stdc++
|
||||||
*/
|
*/
|
||||||
var maxDepth = Math.floor(Math.log2(len)) * TUNEMAXDEPTH
|
const maxDepth = Math.floor(Math.log2(len)) * TUNEMAXDEPTH
|
||||||
/**
|
/**
|
||||||
* The very first call to quicksort
|
* The very first call to quicksort
|
||||||
* this initiates sort routine
|
* this initiates sort routine
|
||||||
@ -133,7 +133,7 @@ function introsort (array, compare) {
|
|||||||
heapSort(start, last)
|
heapSort(start, last)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var pivot = (last + start) >> 1
|
let pivot = (last + start) >> 1
|
||||||
pivot = partition(start, last, pivot)
|
pivot = partition(start, last, pivot)
|
||||||
quickSort(start, pivot, depth - 1)
|
quickSort(start, pivot, depth - 1)
|
||||||
quickSort(pivot + 1, last, depth - 1)
|
quickSort(pivot + 1, last, depth - 1)
|
||||||
@ -148,8 +148,8 @@ function introsort (array, compare) {
|
|||||||
function partition (start, last, pivot) {
|
function partition (start, last, pivot) {
|
||||||
swap(start, pivot)
|
swap(start, pivot)
|
||||||
pivot = start
|
pivot = start
|
||||||
var lo = start
|
let lo = start
|
||||||
var hi = last
|
let hi = last
|
||||||
while (true) {
|
while (true) {
|
||||||
lo++
|
lo++
|
||||||
while (comparator(array[lo], array[pivot]) <= 0 && lo !== last) {
|
while (comparator(array[lo], array[pivot]) <= 0 && lo !== last) {
|
||||||
@ -175,7 +175,7 @@ function introsort (array, compare) {
|
|||||||
* @param {Number} last one more than last index of array to be sorted
|
* @param {Number} last one more than last index of array to be sorted
|
||||||
*/
|
*/
|
||||||
function insertionSort (start, last) {
|
function insertionSort (start, last) {
|
||||||
var i, j
|
let i, j
|
||||||
for (i = start + 1; i < last; i++) {
|
for (i = start + 1; i < last; i++) {
|
||||||
j = i - 1
|
j = i - 1
|
||||||
while (j >= 0 && comparator(array[j], array[j + 1]) > 0) {
|
while (j >= 0 && comparator(array[j], array[j + 1]) > 0) {
|
||||||
@ -192,7 +192,7 @@ function introsort (array, compare) {
|
|||||||
* @param {Number} last one more than last index of array to be sorted
|
* @param {Number} last one more than last index of array to be sorted
|
||||||
*/
|
*/
|
||||||
function heapSort (start, last) {
|
function heapSort (start, last) {
|
||||||
var x = (last + start) >> 1
|
let x = (last + start) >> 1
|
||||||
while (x - start >= 0) {
|
while (x - start >= 0) {
|
||||||
heapify(x, start, last)
|
heapify(x, start, last)
|
||||||
x--
|
x--
|
||||||
@ -211,8 +211,8 @@ function introsort (array, compare) {
|
|||||||
* @param {Number} last one more than last index of segment that cur belongs to
|
* @param {Number} last one more than last index of segment that cur belongs to
|
||||||
*/
|
*/
|
||||||
function heapify (cur, start, last) {
|
function heapify (cur, start, last) {
|
||||||
var size = last - start
|
const size = last - start
|
||||||
var max, lt, rt
|
let max, lt, rt
|
||||||
cur = cur - start
|
cur = cur - start
|
||||||
while (true) {
|
while (true) {
|
||||||
max = cur
|
max = cur
|
||||||
@ -250,9 +250,9 @@ function introsort (array, compare) {
|
|||||||
(function demo () {
|
(function demo () {
|
||||||
const data = []
|
const data = []
|
||||||
const size = 1000000
|
const size = 1000000
|
||||||
var i = 0
|
let i = 0
|
||||||
var temp
|
let temp
|
||||||
var c = function (a, b) {
|
const c = function (a, b) {
|
||||||
return a - b
|
return a - b
|
||||||
}
|
}
|
||||||
for (i = 0; i < size; i++) {
|
for (i = 0; i < size; i++) {
|
||||||
@ -260,7 +260,7 @@ function introsort (array, compare) {
|
|||||||
data.push(temp)
|
data.push(temp)
|
||||||
}
|
}
|
||||||
introsort(data, c)
|
introsort(data, c)
|
||||||
var faulty = false
|
let faulty = false
|
||||||
for (i = 1; i < size; i++) {
|
for (i = 1; i < size; i++) {
|
||||||
if (data[i] < data[i - 1]) {
|
if (data[i] < data[i - 1]) {
|
||||||
faulty = true
|
faulty = true
|
||||||
@ -283,8 +283,8 @@ function introsort (array, compare) {
|
|||||||
const data = []
|
const data = []
|
||||||
const data2 = []
|
const data2 = []
|
||||||
const size = 1000000
|
const size = 1000000
|
||||||
var i = 0
|
let i = 0
|
||||||
var temp
|
let temp
|
||||||
for (i = 0; i < size; i++) {
|
for (i = 0; i < size; i++) {
|
||||||
temp = Math.random() * Number.MAX_SAFE_INTEGER
|
temp = Math.random() * Number.MAX_SAFE_INTEGER
|
||||||
data.push(temp)
|
data.push(temp)
|
||||||
@ -292,7 +292,7 @@ function introsort (array, compare) {
|
|||||||
}
|
}
|
||||||
introsort(data)
|
introsort(data)
|
||||||
data2.sort()
|
data2.sort()
|
||||||
var faulty = false
|
let faulty = false
|
||||||
for (i = 1; i < size; i++) {
|
for (i = 1; i < size; i++) {
|
||||||
if (data[i] !== data2[i]) {
|
if (data[i] !== data2[i]) {
|
||||||
faulty = true
|
faulty = true
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function merge (list1, list2) {
|
function merge (list1, list2) {
|
||||||
var results = []
|
const results = []
|
||||||
|
|
||||||
while (list1.length && list2.length) {
|
while (list1.length && list2.length) {
|
||||||
if (list1[0] <= list2[0]) {
|
if (list1[0] <= list2[0]) {
|
||||||
@ -55,15 +55,15 @@ function merge (list1, list2) {
|
|||||||
function mergeSort (list) {
|
function mergeSort (list) {
|
||||||
if (list.length < 2) return list
|
if (list.length < 2) return list
|
||||||
|
|
||||||
var listHalf = Math.floor(list.length / 2)
|
const listHalf = Math.floor(list.length / 2)
|
||||||
var subList1 = list.slice(0, listHalf)
|
const subList1 = list.slice(0, listHalf)
|
||||||
var subList2 = list.slice(listHalf, list.length)
|
const subList2 = list.slice(listHalf, list.length)
|
||||||
|
|
||||||
return merge(mergeSort(subList1), mergeSort(subList2))
|
return merge(mergeSort(subList1), mergeSort(subList2))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge Sort Example
|
// Merge Sort Example
|
||||||
var unsortedArray = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
|
const unsortedArray = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
|
||||||
var sortedArray = mergeSort(unsortedArray)
|
const sortedArray = mergeSort(unsortedArray)
|
||||||
|
|
||||||
console.log('Before:', unsortedArray, 'After:', sortedArray)
|
console.log('Before:', unsortedArray, 'After:', sortedArray)
|
||||||
|
@ -17,16 +17,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function quickSort (items) {
|
function quickSort (items) {
|
||||||
var length = items.length
|
const length = items.length
|
||||||
|
|
||||||
if (length <= 1) {
|
if (length <= 1) {
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
var PIVOT = items[0]
|
const PIVOT = items[0]
|
||||||
var GREATER = []
|
const GREATER = []
|
||||||
var LESSER = []
|
const LESSER = []
|
||||||
|
|
||||||
for (var i = 1; i < length; i++) {
|
for (let i = 1; i < length; i++) {
|
||||||
if (items[i] > PIVOT) {
|
if (items[i] > PIVOT) {
|
||||||
GREATER.push(items[i])
|
GREATER.push(items[i])
|
||||||
} else {
|
} else {
|
||||||
@ -34,7 +34,7 @@ function quickSort (items) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var sorted = quickSort(LESSER)
|
let sorted = quickSort(LESSER)
|
||||||
sorted.push(PIVOT)
|
sorted.push(PIVOT)
|
||||||
sorted = sorted.concat(quickSort(GREATER))
|
sorted = sorted.concat(quickSort(GREATER))
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ function quickSort (items) {
|
|||||||
|
|
||||||
// Implementation of quick sort
|
// Implementation of quick sort
|
||||||
|
|
||||||
var ar = [0, 5, 3, 2, 2]
|
let ar = [0, 5, 3, 2, 2]
|
||||||
// Array before Sort
|
// Array before Sort
|
||||||
console.log(ar)
|
console.log(ar)
|
||||||
ar = quickSort(ar)
|
ar = quickSort(ar)
|
||||||
|
@ -10,29 +10,29 @@ function radixSort (items, RADIX) {
|
|||||||
RADIX = 10
|
RADIX = 10
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxLength = false
|
let maxLength = false
|
||||||
var placement = 1
|
let placement = 1
|
||||||
|
|
||||||
while (!maxLength) {
|
while (!maxLength) {
|
||||||
maxLength = true
|
maxLength = true
|
||||||
var buckets = []
|
const buckets = []
|
||||||
|
|
||||||
for (var i = 0; i < RADIX; i++) {
|
for (let i = 0; i < RADIX; i++) {
|
||||||
buckets.push([])
|
buckets.push([])
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var j = 0; j < items.length; j++) {
|
for (let j = 0; j < items.length; j++) {
|
||||||
var tmp = items[j] / placement
|
const tmp = items[j] / placement
|
||||||
buckets[Math.floor(tmp % RADIX)].push(items[j])
|
buckets[Math.floor(tmp % RADIX)].push(items[j])
|
||||||
if (maxLength && tmp > 0) {
|
if (maxLength && tmp > 0) {
|
||||||
maxLength = false
|
maxLength = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var a = 0
|
let a = 0
|
||||||
for (var b = 0; b < RADIX; b++) {
|
for (let b = 0; b < RADIX; b++) {
|
||||||
var buck = buckets[b]
|
const buck = buckets[b]
|
||||||
for (var k = 0; k < buck.length; k++) {
|
for (let k = 0; k < buck.length; k++) {
|
||||||
items[a] = buck[k]
|
items[a] = buck[k]
|
||||||
a++
|
a++
|
||||||
}
|
}
|
||||||
@ -44,7 +44,7 @@ function radixSort (items, RADIX) {
|
|||||||
|
|
||||||
// Implementation of radixSort
|
// Implementation of radixSort
|
||||||
|
|
||||||
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
const ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
// Array before Sort
|
// Array before Sort
|
||||||
console.log(ar)
|
console.log(ar)
|
||||||
radixSort(ar)
|
radixSort(ar)
|
||||||
|
@ -2,7 +2,7 @@ import { selectionSort } from './SelectionSort'
|
|||||||
|
|
||||||
describe('selectionSort', () => {
|
describe('selectionSort', () => {
|
||||||
it('expects to return the array sorted in ascending order', () => {
|
it('expects to return the array sorted in ascending order', () => {
|
||||||
var toSort = [5, 6, 7, 8, 1, 2, 12, 14]
|
const toSort = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
const expected = [1, 2, 5, 6, 7, 8, 12, 14]
|
const expected = [1, 2, 5, 6, 7, 8, 12, 14]
|
||||||
|
|
||||||
expect(selectionSort(toSort)).toEqual(expected)
|
expect(selectionSort(toSort)).toEqual(expected)
|
||||||
|
@ -4,16 +4,16 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function shellSort (items) {
|
function shellSort (items) {
|
||||||
var interval = 1
|
let interval = 1
|
||||||
|
|
||||||
while (interval < items.length / 3) {
|
while (interval < items.length / 3) {
|
||||||
interval = interval * 3 + 1
|
interval = interval * 3 + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
while (interval > 0) {
|
while (interval > 0) {
|
||||||
for (var outer = interval; outer < items.length; outer++) {
|
for (let outer = interval; outer < items.length; outer++) {
|
||||||
var value = items[outer]
|
const value = items[outer]
|
||||||
var inner = outer
|
let inner = outer
|
||||||
|
|
||||||
while (inner > interval - 1 && items[inner - interval] >= value) {
|
while (inner > interval - 1 && items[inner - interval] >= value) {
|
||||||
items[inner] = items[inner - interval]
|
items[inner] = items[inner - interval]
|
||||||
@ -28,7 +28,7 @@ function shellSort (items) {
|
|||||||
|
|
||||||
// Implementation of shellSort
|
// Implementation of shellSort
|
||||||
|
|
||||||
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
const ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
// Array before Sort
|
// Array before Sort
|
||||||
console.log(ar)
|
console.log(ar)
|
||||||
shellSort(ar)
|
shellSort(ar)
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
|
|
||||||
function TopologicalSorter () {
|
function TopologicalSorter () {
|
||||||
var graph = {}
|
const graph = {}
|
||||||
var isVisitedNode
|
let isVisitedNode
|
||||||
var finishTimeCount
|
let finishTimeCount
|
||||||
var finishingTimeList
|
let finishingTimeList
|
||||||
var nextNode
|
let nextNode
|
||||||
|
|
||||||
this.addOrder = function (nodeA, nodeB) {
|
this.addOrder = function (nodeA, nodeB) {
|
||||||
nodeA = String(nodeA)
|
nodeA = String(nodeA)
|
||||||
@ -18,7 +18,7 @@ function TopologicalSorter () {
|
|||||||
finishTimeCount = 0
|
finishTimeCount = 0
|
||||||
finishingTimeList = []
|
finishingTimeList = []
|
||||||
|
|
||||||
for (var node in graph) {
|
for (const node in graph) {
|
||||||
if (Object.prototype.hasOwnProperty.call(graph, node) && !isVisitedNode[node]) {
|
if (Object.prototype.hasOwnProperty.call(graph, node) && !isVisitedNode[node]) {
|
||||||
dfsTraverse(node)
|
dfsTraverse(node)
|
||||||
}
|
}
|
||||||
@ -34,7 +34,7 @@ function TopologicalSorter () {
|
|||||||
function dfsTraverse (node) {
|
function dfsTraverse (node) {
|
||||||
isVisitedNode[node] = true
|
isVisitedNode[node] = true
|
||||||
if (graph[node]) {
|
if (graph[node]) {
|
||||||
for (var i = 0; i < graph[node].length; i++) {
|
for (let i = 0; i < graph[node].length; i++) {
|
||||||
nextNode = graph[node][i]
|
nextNode = graph[node][i]
|
||||||
if (isVisitedNode[nextNode]) continue
|
if (isVisitedNode[nextNode]) continue
|
||||||
dfsTraverse(nextNode)
|
dfsTraverse(nextNode)
|
||||||
@ -49,7 +49,7 @@ function TopologicalSorter () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* TEST */
|
/* TEST */
|
||||||
var topoSorter = new TopologicalSorter()
|
const topoSorter = new TopologicalSorter()
|
||||||
topoSorter.addOrder(5, 2)
|
topoSorter.addOrder(5, 2)
|
||||||
topoSorter.addOrder(5, 0)
|
topoSorter.addOrder(5, 0)
|
||||||
topoSorter.addOrder(4, 0)
|
topoSorter.addOrder(4, 0)
|
||||||
|
@ -18,7 +18,7 @@ Array.prototype.wiggleSort = function () {
|
|||||||
|
|
||||||
// Implementation of wiggle sort
|
// Implementation of wiggle sort
|
||||||
|
|
||||||
var arr = [3, 5, 2, 1, 6, 4]
|
const arr = [3, 5, 2, 1, 6, 4]
|
||||||
// Array before Wiggle Sort
|
// Array before Wiggle Sort
|
||||||
console.log(arr) // [3, 5, 2, 1, 6, 4]
|
console.log(arr) // [3, 5, 2, 1, 6, 4]
|
||||||
|
|
||||||
|
@ -25,13 +25,13 @@ function traverseDFS (root) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function searchDFS (tree, value) {
|
function searchDFS (tree, value) {
|
||||||
var stack = []
|
const stack = []
|
||||||
|
|
||||||
stack.push(tree[0])
|
stack.push(tree[0])
|
||||||
|
|
||||||
while (stack.length !== 0) {
|
while (stack.length !== 0) {
|
||||||
for (let i = 0; i < stack.length; i++) {
|
for (let i = 0; i < stack.length; i++) {
|
||||||
var node = stack.pop()
|
const node = stack.pop()
|
||||||
|
|
||||||
if (node.value === value) {
|
if (node.value === value) {
|
||||||
return node
|
return node
|
||||||
@ -47,7 +47,7 @@ function searchDFS (tree, value) {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
var tree = [
|
const tree = [
|
||||||
{ value: 6, left: 1, right: 2 },
|
{ value: 6, left: 1, right: 2 },
|
||||||
{ value: 5, left: 3, right: 4 },
|
{ value: 5, left: 3, right: 4 },
|
||||||
{ value: 7, left: null, right: 5 },
|
{ value: 7, left: null, right: 5 },
|
||||||
|
Reference in New Issue
Block a user