Bubble Sort Algorithm

Basics

Bubble Sort is the simplest sorting algorithm and one of the most common first questions in coding interviews.

The algorithm walks through the array from left to right, comparing each pair of neighbouring elements. If the left element is bigger than the right one, they get swapped.

After one full pass, the largest element has “bubbled up” to the end of the array and is now in its final position. The algorithm then repeats for the remaining unsorted part. If a pass makes no swaps, the array is already sorted and the algorithm can exit early.

The video below explains well how the algorithm works.

Here is the same algorithm as a flowchart.

Bubble Sort algorithm flowchart showing the comparison, swap, and loop logic
Bubble Sort algorithm flowchart

Sample code

Below you will get a link to GitHub repo with Kotlin source code

Download it from github

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.