Created
March 28, 2017 01:23
-
-
Save armenr/c48af8b10358552c4e0038963de3896f to your computer and use it in GitHub Desktop.
recursiveBubbleSort.js
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
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