Skip to content

Instantly share code, notes, and snippets.

@gmcerveny
Last active January 3, 2016 12:59
Show Gist options
  • Save gmcerveny/8466270 to your computer and use it in GitHub Desktop.
Save gmcerveny/8466270 to your computer and use it in GitHub Desktop.
var numbers = [6, 3, 2, 9];
console.log(numbers);
do {
unsorted = true;
for (var i = 0; i < numbers.length - 1; i += 1) {
var j = i + 1;
if (numbers[i] > numbers[j]){
var hold = numbers[i];
numbers[i] = numbers[j];
numbers[j] = hold;
unsorted = false;
}
}
} while (!unsorted);
console.log(numbers);
var numbers = [1,3,3,5];
var ordered = [numbers.pop()];
console.log(numbers);
while (numbers.length > 0){
var popped = numbers.pop();
for (var i = 0; i < ordered.length; i++) {
if ( popped <= ordered[i]) {
ordered.splice(i, 0, popped);
break;
}
}
}
console.log(ordered);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment