Skip to content

Instantly share code, notes, and snippets.

@strongme
Created January 11, 2014 16:15
Show Gist options
  • Save strongme/8372866 to your computer and use it in GitHub Desktop.
Save strongme/8372866 to your computer and use it in GitHub Desktop.
Insertion sort - swap
function insertionSort(data) {
for(var i=1;i<data.length;i+=1) {
for(var j=i-1;j>=0&&data[j+1]<data[j];j-=1) {
swap(data,j,j+1);
}
}
return data;
}
function swap(data,a,b) {
var tmp = data[a];
data[a] = data[b];
data[b] = tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment