\
home Home
sort Sorts
Selection Sort Insertion Sort Counting Sort
SORTING VISUALIZER - BUBBLE SORT

  BubbleSort(A[],n)

    1.   For i = 0 to n - 1

    2.     For j = 0 to n - 1 - i

    3.       If A[j]>A[j+1]

    4.         swap A[j] <-> A[j+1]

    5.       End If

    6.     End For

    7.   End For


No of Comparisons:



     

Complexity

Time
Space
Best Case Average Case Worst Case
O(1)
O(n)
O(n2)
O(n2)

Bubble Sort

What is the working mechanism of Bubble Sort, and what is the origin of its name?

The bubble sort algorithm gets its name from the way that bubbles rise to the surface of a liquid - larger bubbles rise faster - larger elements "rise" to their correct positions faster in the list. During each pass through the list, adjacent elements are compared and swapped if necessary, gradually "sorting" the elements as if they were rising to the surface like bubbles.


Is bubble sort a stable, in-place sorting algorithm or an out-of-place sorting algorithm?

Bubble sort is a stable in-place algorithm, meaning that it does not require any extra space and maintains the relative order of duplicates.


How does the number of elements impact the efficiency of Bubble sort?

The efficiency of Bubble sort is impacted by the size of the input list, and as the list size increases, the number of iterations needed for the algorithm to sort the list also increases, which can result in poor performance when sorting large lists, making it unsuitable for large-scale applications.


What are advantages and disadvantages of Bubble Sort?

Advantages:

  • Bubble sort is easy to understand and implement.
  • It does not require any additional memory space.
  • It's adaptability to different types of data.
  • It is a stable sorting algorithm, meaning that elements with the same key value maintain their relative order in the sorted output.

Disadvantages:
  • Bubble sort has a time complexity of O(n^2) which makes it very slow for large data sets.
  • It is not efficient for large data sets, because it requires multiple passes through the data.