Created
March 15, 2016 12:54
-
-
Save nothingrealhappen/e814adedf2369a4ffa7e to your computer and use it in GitHub Desktop.
sort.js
This file contains hidden or 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
'use strict'; | |
var test = [10,4,3,5,6,2,8]; | |
const swap = function(a, b) { | |
if(this[a] > this[b]) this[a] = [this[b], this[b] = this[a]][0]; | |
} | |
const bubbleSort = a => { | |
a.forEach((x, index) => { | |
for (let i = 0; i < a.length -1 - index; i++) swap.call(a, i, i+1); | |
}); | |
return a; | |
}; | |
const quickSort = a => { | |
if (a.length <= 1) return a; | |
const pivot = a.splice(0, 1); | |
const split = a.reduce((p, v) => v < pivot ? [p[0].concat(v), p[1]] : [p[0], p[1].concat(v)], [[], []]); | |
return quickSort(split[0]).concat(pivot).concat(quickSort(split[1])); | |
}; | |
console.log(bubbleSort(test)); | |
console.log(quickSort(test)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment