Last active
June 6, 2020 06:39
-
-
Save huynhducduy/d5a4fe521006f8d77fbc7179bbb53d5b to your computer and use it in GitHub Desktop.
Algorithms & Data Structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function binarySearch(arr, x) { | |
let start = 0, end = arr.length-1; | |
while (start <= end){ | |
const mid = Math.floor((start + end)/2); | |
if (arr[mid] === x) return mid; | |
else if (x > arr[mid]) | |
start = mid + 1; | |
else | |
end = mid - 1; | |
} | |
return false; | |
} | |
console.log(binarySearch([1, 2, 3, 5, 6, 7, 8, 9], 9)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function debounce (func, delay) { | |
let inDebounce | |
return function() { | |
clearTimeout(inDebounce) | |
inDebounce = setTimeout(func.bind(this, arguments), delay) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(data, next = null) { | |
(this.data = data), (this.next = next); | |
} | |
} | |
class LinkedList { | |
constructor() { | |
this.head = null; | |
} | |
getAt(index) { | |
let counter = 0; | |
let node = this.head; | |
while (node) { | |
if (counter === index) { | |
return node; | |
} | |
counter++; | |
node = node.next; | |
} | |
return null; | |
} | |
insertAtBeginning(data) { | |
let newNode = new Node(data); | |
newNode.next = this.head; | |
this.head = newNode; | |
return this.head; | |
} | |
insertAtEnd(data) { | |
let newNode = new Node(data); | |
if (!this.head) { | |
this.head = newNode; | |
return this.head; | |
} | |
let tail = this.head; | |
while (tail.next !== null) { | |
tail = tail.next; | |
} | |
tail.next = newNode; | |
return this.head; | |
} | |
insertAt(data, index) { | |
if (!this.head) { | |
this.head = new Node(data); | |
return; | |
} | |
if (index === 0) { | |
this.head = new Node(data, this.head); | |
return; | |
} | |
const previous = this.getAt(index - 1); | |
let newNode = new Node(data); | |
newNode.next = previous.next; | |
previous.next = newNode; | |
return this.head; | |
} | |
deleteFirstNode() { | |
if (!this.head) { | |
return; | |
} | |
this.head = this.head.next; | |
return this.head; | |
} | |
deleteLastNode() { | |
if (!this.head) { | |
return null; | |
} | |
if (!this.head.next) { | |
this.head = null; | |
return; | |
} | |
let previous = this.head; | |
let tail = this.head.next; | |
while (tail.next !== null) { | |
previous = tail; | |
tail = tail.next; | |
} | |
previous.next = null; | |
return this.head; | |
} | |
deleteAt(index) { | |
if (!this.head) { | |
this.head = new Node(data); | |
return; | |
} | |
if (index === 0) { | |
this.head = this.head.next; | |
return; | |
} | |
const previous = this.getAt(index - 1); | |
if (!previous || !previous.next) { | |
return; | |
} | |
previous.next = previous.next.next; | |
return this.head; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mergeSort(array) { | |
if (array.length < 2) return array; | |
const mid = Math.floor(array.length/2) | |
const left = array.slice(0,mid); | |
const right = array.slice(mid, array.length) | |
return merge(mergeSort(left),mergeSort(right)) | |
} | |
function merge(left,right) { | |
const arr = []; | |
while (left.length && right.length){ | |
if (left[0] < right [0]) arr.push(left.shift()); | |
else arr.push(right.shift()); | |
} | |
return [...arr, ...left, ...right]; | |
} | |
console.log(mergeSort([10,5,3,8,2,6,4,7,9,1])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function quickSort(arr) { | |
if (arr.length <= 1) return arr; | |
var left = []; | |
var right = []; | |
var pivot = arr.pop(); | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] <= pivot) left.push(arr[i]); | |
else right.push(arr[i]); | |
} | |
return [...quickSort(left), pivot, ...quickSort(right)]; | |
} | |
console.log(quickSort([10,5,3,8,2,6,4,7,9,1])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function throttle(func, limitTime) { | |
let lastTime = 0; | |
return function () { | |
let now = new Date(); | |
if (now - lastTime >= limitTime) { | |
func.apply(this, arguments); | |
lastTime = now; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment