Skip to content

Instantly share code, notes, and snippets.

@faridlab
Created June 20, 2014 02:18
Show Gist options
  • Save faridlab/4a9a85df8e02da7e7026 to your computer and use it in GitHub Desktop.
Save faridlab/4a9a85df8e02da7e7026 to your computer and use it in GitHub Desktop.
// insertion sort
var insertionSort = function (str) {
var str = str.split(''),
result = [],
swap = [];
for (var i = 0; i < str.length; i++) {
var chr = str[i],
br = false;
swap = result;
result = [];
while (!br && swap.length > 0) {
if(swap[0] >= chr ) br = true;
else {
result.push(swap[0]);
swap.shift();
}
}
result.push(chr);
result = result.concat(swap);
}
return result.join('');
}
var sort = "insertionsort";
insertionSort(sort); // output : eiinnoorrsstt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment