Bubble Sort

Vachanc
2 min readMar 24, 2021

Introduction:

Bubble Sort is a widely used sorting algorithm in computer science. Bubble Sort works by comparing pairs of adjacent elements repeatedly and switching their locations if they are in the wrong order. All in all, when it comes to algorithms, understanding Bubble Sort is important.

There are steps we should follow for sorting an array using bubble array:

Start with the first two elements in an unsorted sequence of five elements and sort them in ascending order.

To determine the element is greater, compare the second and third elements and sort them in ascending order.

To determine the element is greater, compare the third and fourth elements and sort them in ascending order.

To determine the element is greater, compare the fourth and fifth elements and sort them in ascending order.

Steps 1–5 should be repeated until no more swaps are needed.

The following image is working of bubble sort :

Characteristics of bubble sort:

The order of large values is always sorted first.

It only takes one iteration to detect whether or not a set has been sorted.

Bubble Sort’s best time complexity is O. (n). O(n2) is the average and worst time complexity.

Bubble Sort has an O(1) space complexity because only a single additional memory space is needed.

Time complexities:

· Worst-Case Complexity(O(n2)): The worst-case scenario happens if we try to sort in ascending order, but the list is in descending order.

· Best Case Complexity(O(n)): There is no need to filter the list if it is already sorted.

· Average Case Complexity(O(n2)): It arises when the array’s elements are jumbled up (neither ascending nor descending).

Bubble sort applications:

In the following conditions, bubble sort is used:

· The complexity of code does not matter.

· It is preferable to use a shortcode.

--

--