Created
June 7, 2023 14:34
-
-
Save waterrmalann/b4a9ed307007d1ae0287a37636a4da2e to your computer and use it in GitHub Desktop.
A simple, optimized implementation of Bubble Sort algorithm in JavaScript.
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
// A simple, optimized bubble sort implementation in JavaScript. | |
function bubbleSort(arr) { | |
let len = arr.length; // keep track of the length. | |
let swapped = false; // keep track of if any swaps were made. | |
for (let i = 0; i < len - 1; i++) { | |
// Loop through all the elements of the array. | |
// We did `len - 1` because otherwise an unnecessary pass will be made at the end. | |
// We assume that no swap is taking place this pass. | |
swapped = false; | |
for (let j = 0; j < len - i - 1; j++) { | |
// Loop from start to `len - i - 1` | |
// This will ensure the loop runs one item less everytime, | |
// a nice little optimization. | |
if (arr[j] > arr[j + 1]) { | |
// If the current element is greater than the next element, | |
// we swap those two elements and set `swapped` to true. | |
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; | |
swapped = true; | |
} | |
} | |
if (!swapped) { | |
// If we haven't swapped anything in this pass, | |
// we can short-circuit and break the loop early. | |
break; | |
} | |
} | |
// We finally return the sorted array. | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment