Skip to content

Instantly share code, notes, and snippets.

@unbug unbug/bubbleSort
Created May 21, 2016

Embed
What would you like to do?
function bubbleSort(arr){
if(arguments.lenght === 0 || !Array.isArray(arr)){
throw new Error();
}
var hasHadChange;
for(var i=0;i<arr.length-1;i++){
hasHadChange = false;
for(var x=0;x<arr.length-1;x++){
if(arr[x]>arr[x+1]){
hasHadChange = true;
var tmp = arr[x];
arr[x] = arr[x+1];
arr[x+1] = tmp;
}
}
if(!hasHadChange){
return arr;
}
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.