Goseeko blog

# What is Bubble sort?

The bubble sort is a simple sorting method. This sorting approach uses a comparison-based algorithm in which each pair of adjacent elements is compares and exchanged if they are out of order. Because the average and worst case complexity of this technique are O(n2), where n is the number of items, it is not suitable for huge data sets.

Bubble Sort is a simple algorithm for sorting a collection of n elements that is provided in the form of an array with n elements. Bubble Sort evaluates and arranges each element individually based on its values.

If an array must sorted in ascending order, bubble sort will start by comparing the array’s first and second members, swapping both elements if the first element is greater than the second, and so on.

If there are n elements in total, we must repeat the method n-1 times.

It’s termed bubble sort because, like a water bubble rising to the sea surface, the largest element in the provided array bubbles up to the last position or highest index for each full iteration.

Although it is simple to use, bubble sort is largely employe as an educational tool because its performance in the actual world is low. It isn’t design to handle huge data collections. Bubble sort has an average and worst-case complexity of O(n2), where n is the number of elements.

Sorting is done by going through all of the items one by one, comparing them to the next closest element, and swapping them if necessary.

## Algorithm

Assume arr is an array of n elements in the algorithm below. The algorithm’s presumed swap function will swap the values of specified array members.

begin BubbleSort(arr)

for all array elements

if arr[i] > arr[i+1]

swap(arr[i], arr[i+1])

end if

end for

return arr

end BubbleSort

Interested in learning about similar topics? Here are a few hand-picked blogs for you!