Created
July 17, 2012 20:30
-
-
Save renatoargh/3131843 to your computer and use it in GitHub Desktop.
Bubblesort Implementation in Java Script
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 swap(array, i, j){ | |
| var aux = array[i]; | |
| array[i] = array[j]; | |
| array[j] = aux; | |
| } | |
| function bubbleSort(array, ascending){ | |
| for(i = 0; i < array.length; i++){ | |
| for(j = array.length - 1; j >= i + 1; j--){ | |
| if((array[j] < array[j - 1]) === ascending){ | |
| swap(array, j, j - 1); | |
| } | |
| } | |
| } | |
| } | |
| var array = [5, 4, 3, 2, 1, 0]; | |
| console.log("ANTES: " + array); | |
| bubbleSort(array, true); | |
| console.log("DEPOIS: " + array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment