Arduino – sorting array integers with a bubble sort algorithm

As it happens, I needed to sort an array of integers in ascending order and also find the minimum and maximum number from the array on my Arduino. I decided to knock up a quick bubble sorting function which you can find below.

Bubble sorting is probably one of the easiest and quickest (to write, not actually sort!) algorithms to produce in C. It works by iterating through the array of values comparing pairs and swapping them around if need be, it then performs the same action again until no swapping is needed. You can read more on the Bubble Sorting algorithm by clicking here.

The above Arduino sketch has debug Serial output which should look like the following one uploaded:

bubble

This code isn’t limited to the Arduino, there are no special function requirements needed and should work in any C based application. The only thing that you may want to change is finding the size of the array instead of having it static however I did not need this as I knew how many elements I needed.