Simple Sorting : Bubble sort

Hi,

Today, I'll begin a very long journey of exploring algorithms that you need to know as a software engineer. I'll do my best to describe each and every one simply by providing you sample code in java and python.

Each subsequent post will describe one and only one algorithm. We start by the most simple of them : the Bubble Sort. The bubble sort is notoriously slow.

Process

  1. We start at the left of the array, we compare values in positions 0 and 1
  2. If the value at index 0 is bigger, swap them
  3. Move on the right
  4. When you reach the end of the first pass, you know that the last value is sorted, start over at the left of the array till N-x (the number of pass)

Java:


Python:


The bubble sort runs in O(N2) time.


Comments

Popular posts from this blog

Spring JPA : Using Specification with Projection

Chip input using Reactive Form