# Sorting Algorithms Made Easy

If you're a beginner in Sorting Algorithms, the first few sorting algorithms that you are going to learn would be good ol' Bubble Sort. It's simple to code and the easiest to understand out of all the other sorting algorithms. I'll explain a bit further on how to code this in a while.

Firstly, let's write an algorithm in order to sort these numbers using Bubble Sort. Keep in mind that each pass in bubble sort would 'Bubble' the biggest number to the end of the list.

Algorithm:

- Go through the list of numbers
- Only sort out the numbers that have not been sorted yet
- When going through the list of unsorted numbers, swap numbers accordingly when met with a bigger number.

So here is an illustration of what happens with the numbers [5,4,1,2,8]!

Now that we are done with Bubble Sort, let's take a look at another basic sorting algorithm called Insertion Sort.

Insertion sort makes use of having pointers to keep track of the last few items that we have sorted. In a way, it is similar to Bubble Sort whereby the first few items in the list would be sorted as we traverse the list.

Algorithm:

We can assume that the first item in the list is already sorted so we can start traversing the list from the second item!

- Traverse list from 1 to n (length of list)
- We initialise a variable, inserted_num to store the new inserted element that we are looking at in this case is the array[i]
- We have a pointer, position, to track the current position of sorted numbers, this would be (i-1) to begin with!
- We compare the value of the inserted element with the current element in the sorted number array list
- We will stop when we reach a number that is smaller than the number to be inserted
- Insert the number at its correct position!

By looking at the code, we can see that if the "Inserted Number" was smaller than the previously sorted numbers, the previously sorted numbers would just be shifted forward and the inserted number would be placed in between numbers that have already been sorted.

And there you have it, both sorting algorithms are very simple to implement and will be much easier for to visualise by drawing it out to help you understand what is happening exactly!

Hope you enjoyed this post on Sorting Algorithms! Look out for our future explanations on even faster and more efficient Sorting Algorithms such as Merge Sort and Quick Sort.