-
-
Save swapnilshrikhande/e298ad2d471204e083bb to your computer and use it in GitHub Desktop.
bubblesort.js
This file contains 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 (sortingArray) { | |
var goto = sortingArray.length - 1; | |
for(var key in sortingArray) { | |
for(var i = 0; i <= goto - 1; i++) { | |
if(sortingArray[i] > sortingArray[i + 1]) { | |
// Swap | |
var temp = sortingArray[i]; | |
sortingArray[i] = sortingArray[i +1]; | |
sortingArray[i + 1] = temp; | |
} | |
} | |
goto--; | |
} | |
return sortingArray; | |
} | |
var unsorted = [456, 345, 234, 543, 465, 654]; | |
console.log('Unsorted:', unsorted); | |
console.log('Bubble Sorted:', bubbleSort(unsorted)); | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment