Skip to content

Instantly share code, notes, and snippets.

@nothingrealhappen
Created March 15, 2016 12:54
Show Gist options
  • Save nothingrealhappen/e814adedf2369a4ffa7e to your computer and use it in GitHub Desktop.
Save nothingrealhappen/e814adedf2369a4ffa7e to your computer and use it in GitHub Desktop.
sort.js
'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