To Expand The #Selftaught Movement Across Southeast Asia!
Source: w3resource

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:

  1. Go through the list of numbers
  2. Only sort out the numbers that have not been sorted yet
  3. 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]!

Notice that the biggest number in the list appears at the end!
In the 2nd Pass, the second biggest element would bubble to the end of the list!
For the 3rd pass, we will only compared the numbers from 0 to len(array) - the number of items already sorted
The very last pass which only compares the final two numbers

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

source : w3resource

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!

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

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.

You've successfully subscribed to The Hacker Collective
Welcome back! You've successfully signed in.
Great! You've successfully signed up.
Success! Your account is fully activated, you now have access to all content.