Skip to content

Instantly share code, notes, and snippets.

@rgwozdz
Created December 20, 2017 16:28
Show Gist options
  • Select an option

  • Save rgwozdz/2adc35161288b39ab58ca00adfea6396 to your computer and use it in GitHub Desktop.

Select an option

Save rgwozdz/2adc35161288b39ab58ca00adfea6396 to your computer and use it in GitHub Desktop.
bubble-sort with notes
function bubbleSort(arr){
// Get Array length
var len = arr.length;
// Create and outer-loop with a decrementing index i
for (var i = len-1; i>=0; i--){
// Create and inner-loop with incrementing index j that has a max index i
// So, the first time the innerloop cycles, it will loop a number of times equal to the array length.
// The next cycle it will be one less, and so forth
for(var j = 1; j<=i; j++){
// Swap adjacent array elements if left element > right element
if(arr[j-1]>arr[j]){
var temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment