-
-
Save klandergren/1042657 to your computer and use it in GitHub Desktop.
BubbleSort
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
| // working version | |
| function swap(a_array, i, j) { | |
| var tmp = a_array[i]; | |
| a_array[i] = a_array[j]; | |
| a_array[j] = tmp; | |
| } | |
| function bubble_sort(an_array){ | |
| var n = an_array.length; | |
| var found_inversion = true; | |
| while(found_inversion){ | |
| found_inversion = false; | |
| for (var i=1;i<n;i++){ | |
| if(an_array[i-1] > an_array[i]) { | |
| found_inversion = true; | |
| swap(an_array, i-1, i); | |
| } | |
| } | |
| } | |
| return an_array; | |
| } | |
| console.log(bubble_sort([4,5,2,3,1])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment