πΌ 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)
Top comments (0)