Skip to content

Instantly share code, notes, and snippets.

@thedineshj
Last active April 21, 2018 07:39
Show Gist options
  • Save thedineshj/cc00ee3e7c41fb2b16cb8d85a51d6035 to your computer and use it in GitHub Desktop.
Save thedineshj/cc00ee3e7c41fb2b16cb8d85a51d6035 to your computer and use it in GitHub Desktop.
JavaSript code for Bubble sort technique(non optimized version)
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