Skip to content

Instantly share code, notes, and snippets.

@armenr
Created March 28, 2017 01:23
Show Gist options
  • Save armenr/c48af8b10358552c4e0038963de3896f to your computer and use it in GitHub Desktop.
Save armenr/c48af8b10358552c4e0038963de3896f to your computer and use it in GitHub Desktop.
recursiveBubbleSort.js
var bubbleSort = function(array) {
// a method to swap values @ a given pair of index positions
this.shifter = function(input, idxNext, idxElement) {
console.log('shifter engaged');
var temp = input[idxNext];
input[idxNext] = input[idxElement];
input[idxElement] = temp;
return input;
}
// a recursive method to sort some stuff
this.recursor = function(array) {
array.forEach(function(element, index) {
var madeSwitch = false;
// boolean switch --> base case = false on next recrusion/break out
let current = element;
let next = array[index + 1];
if (next < current) {
let currIndex = index;
let nextIndex = index + 1;
this.shifter(array, nextIndex, currIndex);
madeSwitch = true;
}
if (madeSwitch === true) {this.recursor(array)}
});
}
this.recursor(array);
return array;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment