Created
May 3, 2019 14:10
-
-
Save bhuizi/18aefadfe01b3b47b033047427cb44c2 to your computer and use it in GitHub Desktop.
algorithms in javascript
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
2. Divide and Conquer | |
function diffSum (arr){ | |
if(arr.length === 0) return 0; | |
return arr[0] + sum(arr.slice(1)) | |
} | |
sum([1, 2, 3]) | |
3. Select Sort | |
const itemsToSort = [3, 2, 4, 1, 6] | |
function findLargetstValue(list){ | |
let lrg = list[0]; | |
let indexOfLarge = 0 | |
for( let i = 1; i <= list.length; i++) { | |
if(lrg < list[i]) { | |
lrg = list[i] | |
indexOfLarge = i | |
} | |
} | |
return indexOfLarge | |
} | |
function newSelectionSort(list){ | |
let newList = [] | |
let lrgItem | |
while(list.length) { | |
lrgItem = findLargetstValue(list) | |
newList.push(list[lrgItem]) | |
list.splice(lrgItem, 1) | |
} | |
return newList | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment