Last active
April 21, 2018 07:39
-
-
Save thedineshj/cc00ee3e7c41fb2b16cb8d85a51d6035 to your computer and use it in GitHub Desktop.
JavaSript code for Bubble sort technique(non optimized version)
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(arr) { | |
var temp; | |
console.log("Given array"); | |
console.log(arr); | |
for (var i = 0; i < arr.length - 1; i++) { | |
// Last i elements are already in order | |
for (var j = 0; j < arr.length - i - 1; j++) { | |
if (arr[j] > arr[j + 1]) { | |
temp = arr[j]; | |
arr[j] = arr[j + 1]; | |
arr[j + 1] = temp; | |
} | |
} | |
console.log("Iteration" + " " + (i + 1)); | |
console.log(arr); | |
} | |
console.log("Sorted array"); | |
console.log(arr); | |
} | |
// Example Usage | |
// var arr = [5, 4, 1, 2, 3]; | |
// bubbleSort(arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment