Bubble Sort in JavaScript
Olga Lapovsky
Posted on November 17, 2020
📼 Explanation of Bubble Sort :
💻 CODE :
function bubbleSort(array) {
let isSorted = false;
let arrayLength = array.length - 1;
let temp;
while (!isSorted) {
isSorted = true;
for (let i = 0; i < arrayLength; i++) {
if (array[i] > array[i + 1]) {
temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
isSorted = false;
}
}
arrayLength--;
}
}
Time Complexity:
- Best: Ω(N)
- Average: Θ(N²)
- Worst: Ο(N²)
Space Complexity:
- Ο(1)
💖 💪 🙅 🚩
Olga Lapovsky
Posted on November 17, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
privacy Caught in the Crunch My Journey from Snacks to 2 Million Exposed Users Privacy
November 30, 2024
devchallenge Submission for the DevCycle Feature Flag Challenge: Feature Flag Funhouse
November 30, 2024