Created
January 11, 2014 16:15
-
-
Save strongme/8372866 to your computer and use it in GitHub Desktop.
Insertion sort - swap
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
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