Created
December 20, 2017 16:28
-
-
Save rgwozdz/2adc35161288b39ab58ca00adfea6396 to your computer and use it in GitHub Desktop.
bubble-sort with notes
This file contains hidden or 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 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