No need to rely on logarithmic time sorting algorithms If you are sorting small datasets.
Instead, you are more then safe to fallback on a naive quadratic algorithm like insertSort or selectSort.
In this post, we’ll walk through them.
Bubble Sort
Is a simple sorting algorithm that repeatedly compares adjacents items and swap them if needed.
Although the logic is pretty simple, is way to slow to be used in a real scenario, even compared to insertSert.
It’s ok to use it only in cases where the data that need to be sorted is already almost sorted and may only occasionally present few unsorted items.
The idea behind select sort is to repeatedly select the minimum value from the array pushing it to a new array, this way the newly created array will result in being sorted.
Normally select sort is being used as an in-place comparison algorithm, allowing to save memory. For the sake of variation, we are going to select the Max value this time for the in-place example of selectSort:
Insert Sort
Insert Sort take each element of the array and insert it into a new array in a sorted position:
As we did for select sort we can have an insertion sorting in-place: