Skip to content

Instantly share code, notes, and snippets.

@evilpie
Created December 12, 2011 19:50
Show Gist options
  • Save evilpie/1468789 to your computer and use it in GitHub Desktop.
Save evilpie/1468789 to your computer and use it in GitHub Desktop.
school
/* insertion sort */
function insertion_sort(a) {
for (var i = 1; i < a.length; i++) {
var key = a[i];
var j = i - 1;
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key;
}
return a;
}
function selection_sort(a) {
for (var i = 0; i < a.length - 1; i++) {
var min = i;
for (var k = i + 1; k < a.length; k++) {
if (a[k] < a[min])
min = k;
}
var temp = a[min];
a[min] = a[i];
a[i] = temp;
}
return a;
}
function bubble_sort(a) {
var n = a.length;
do {
var switched = false;
for (var i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
var temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
switched = true;
}
}
n = n - 1
} while (switched && n > 1);
return a;
}
a = bubble_sort([1, 1, 2, 1, 1, 1])
print(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment