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 bubbleSort(arr) { | |
for (var i = 0; i < arr.length - 1; i++) { | |
for(var j = 0; j < arr.length - i - 1; j++ ) { | |
if (arr[j] > arr[j + 1]) { | |
var temp = arr[j]; | |
arr[j] = arr[j + 1]; | |
arr[j + 1] = temp; | |
} | |
} | |
} |
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
var quickSort = function(array, left, right){ | |
var leftIndex = partition(array, left, right); | |
if (left < leftIndex - 1){ | |
quickSort(array, left, leftIndex-1); | |
} | |
if (right > leftIndex){ | |
quickSort(array, leftIndex, right); |
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 (arr) { | |
if (arr.length < 2) { | |
return arr; | |
} | |
var mid = Math.floor(arr.length / 2); | |
var subLeft = mergeSort(arr.slice(0, mid)); | |
var subRight = mergeSort(arr.slice(mid)); | |
return merge(subLeft, subRight); |
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 fizzBuzz(to) { | |
for (var i=1; i <= to; i++) { | |
if (i % 15 == 0) | |
console.log("FizzBuzz"); | |
else if (i % 3 == 0) | |
console.log("Fizz"); | |
else if (i % 5 == 0) | |
console.log("Buzz"); | |
else | |
console.log(i); |
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 checkSum(arr, sum) { | |
let low = 0, | |
high = arr.length - 1; | |
while(low < high) { | |
let s = arr[low] + arr[high]; | |
if (s === sum) return true | |
if (s < sum) { | |
low = low + 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 checkUnorderedSum(arr, sum) { | |
let alreadySeen = []; | |
for (var i = 0; i < arr.length; i++) { | |
let complement = sum - arr[i]; | |
if (alreadySeen.indexOf(arr[i]) > -1) { | |
return true | |
} |
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 findDuplicates(arr) { | |
for (var i = 0; i < arr.length; i++){ | |
if (arr[Math.abs(arr[i])] >= 0) { | |
arr[Math.abs(arr[i])] = -arr[Math.abs(arr[i])] | |
} else { | |
console.log(Math.abs(arr[i] + " ")); | |
} | |
} | |
} |
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 isPrime(value) { | |
for(var i = 2; i < value; i++) { | |
if(value % i === 0) { | |
return false; | |
} | |
} | |
return value > 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
//Using built-in functions | |
function reverseString(str) { | |
return str.split("").reverse().join(""); | |
} | |
//Without built-in functions | |
function reverseString(str) { | |
var newString = ""; | |
for (var i = str.length - 1; i >= 0; i--) { | |
newString += str[i]; |
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
//v1 without built in functions | |
function palindrome(str) { | |
var len = str.length, | |
mid = Math.floor(len/2); | |
for ( var i = 0; i < mid; i++ ) { | |
if (str[i] !== str[len - 1 - i]) { | |
return false; | |
} | |
} |
OlderNewer