Some Sorting Algorithms

Jeric Hunter
4 min readMar 1, 2021

Hello all, Today I am going to be going over a few sorting algorithms today with you. The algorithms will be “Bubble Sort”, “Insertion Sort”, and “Selection Sort”. Well I’m sure some of you may be asking why to use them at all so I’ll tell you.

from https://macr.ae/article/sorting-algorithms

What is the point of a sorting algorithm?

A sorting algorithm will put items in a list into an order, such as alphabetical or numerical order. For example, when you are using a phone book (I doubt any of you are using one but anyway) you are looking for the first letter of the name you are searching for. Now imagine if the phone book was not organized at all in any way. Finding the person’s name you are looking for will be very hard and borderline impossible. There are hundreds and hundreds of pages you would just be flipping through and looking at each and every page that is going to take you an absurd amount of time.

However when it is sorted in alphabetical order as soon as you flip to the letter you are looking for it will maybe take you a few minutes to find the name. This is the magic of sorting in the simplest way that I can put it.

Now lets dig in to these sorting algorithms I want to talk about today.

Bubble Sort

Bubble sort is the algorithm I decided to start with because it is usually the one that is first taught to you in Computer Science, this algorithm is very good at demonstrating while being simple and easy to figure out.

To begin this algorithm you want to compare the first two elements of your list. If the first element you are at is larger than the second element, you swap them. For example if I have a list of 2,1,4,3,5. You would be swapping the 2 with the 1 and then swapping the 4 with the 3. You won’t have to make any changes if they are already in order you will want to leave them as is. You will continue this process of swapping until you have reached the last pair of items in your list.

from https://commons.wikimedia.org/wiki/File:Bubble-sort-example-300px.gif

Implementation

Selection Sort

Selection sort is also somewhat simple and performs better than the bubble sort option at times. This algorithm will segment your list into two parts, one part being sorted, the other one being unsorted. We find the smallest element in the unsorted list and then place it at the end of the sorted list. So you are continuously removing the smallest element of the unsorted list and adding it to the end of the sorted list. You will continue this process until the list is fully sorted.

from https://commons.wikimedia.org/wiki/File:Selection-Sort-Animation.gif

Implementation

Insertion Sort

Insertion sort works as an algorithm of sorting playing cards in your hand, you should try it out yourself with some numbered cards. This algorithm is both faster and honestly much more simpler than bubble sort and selection sort.

Just like selection sort this algorithm segments the list into two parts, one that is sorted, and one that is not. This algorithm will iterate over the unsorted list and insert the element you are on into the correct position of the sorted list. This process will be repeated until you cannot input any more elements. You will constantly be swapping two items going backwards in your list to fix the elements until they are in proper position. I feel this is best explained through drawing and code implementation so I would take an extra look at the animated example and the implementation.

from https://commons.wikimedia.org/wiki/File:Insertion-sort-example.gif

Implementation

Thank You

Thank you for reading this article on some sorting algorithms, I am heavily considering going over some more sorting algorithms as they are important to understand if you are studying computer science. I also feel they are important when it comes to getting a job because some companies may ask you to implement one of these sorting algorithms in a coding interview.

Anyway I hope you learned something and if not then I hope I was able to make you look at these sorting algorithms differently. I tried to keep it sorta short and sweet so again, thank you.

--

--