Member-only story
Sorting Algorithms: Bubble Sort
Sorting Algorithms?
So, you’re probably wondering, “What exactly are sorting algorithms?”. Well, a sorting algorithm is simply used to rearrange a given array or some list of elements in an ascending or descending order. With the use of a comparison operator that decides the new order of the elements in the data structure.
An example would be if we had an array of unsorted numbers, we wanted our algorithm to sort them in ascending order.
[3,5,7,1,2] => [1,2,3,5,7]
Bubble Sort
Now, A Bubble Sort algorithm is one of the simplest sorting algorithms to understand. Bubble sort works by repeatedly swapping the adjacent elements if they are in the wrong order. Let’s say we are ascending from smallest to largest in an array:
What Exactly Is Happening Here?
What our bubble sort algorithm is doing here is, through every single iteration, it is continuously comparing each pair of integers, determining where to place those two integers in regards…