AlgoViz

Overview

Bubble Sort is a simple comparison-based sorting algorithm. In this algorithm, each pair of adjacent elements is compared, and the elements are swapped if they are in the wrong order. This process is repeated until the list is sorted.

Time Complexity:
Best case: O(n) - When the array is already sorted
Average case: O(n²) - When the array elements are in random order
Worst case: O(n²) - When the array is in reverse order
Space Complexity: O(1) - It is an in-place sorting algorithm.

No. of comparisons: 0 No. of swaps: 0
for i = 0 to length(list) - 1
  for j = 0 to length(list) - i - 1
    if list[j] > list[j + 1]
      swap(list[j], list[j + 1])
SHORT EXPLANATION
------------------
1. Start with the first element and compare it with the next one.
   - If the current element is greater than the next element, swap them.
2. Move to the next element and repeat the comparison.
3. After each pass, the largest element in the unsorted portion is moved to its correct position.
4. Continue this process until the entire list is sorted.
50 1000
5 50
arrow_upward