Skip to content

Instantly share code, notes, and snippets.

@bhuizi
Created May 3, 2019 14:10
Show Gist options
  • Save bhuizi/18aefadfe01b3b47b033047427cb44c2 to your computer and use it in GitHub Desktop.
Save bhuizi/18aefadfe01b3b47b033047427cb44c2 to your computer and use it in GitHub Desktop.
algorithms in javascript
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