Algorithms Every Programmer Should Know 2
Tags: webdev, java, python, watercooler
Published: 2021-03-04 04:49:19


# Introduction
Let's continue the series of "Algorithms Every Programmer Should Know". It is the 2nd Part, in the first part, we explore the 4 Searching Algorithm. If you haven't read that please read that and here is the post.

{% post https://dev.to/surajondev/algorithms-every-programmer-should-know-part-1-searching-algorithm-1hd3 %}

Today, we are going to look into 6 sorting algorithms that every programmer should know. So without a further ado let's get started.

---

## Insertion Sort

> Insertion sort iterates, consuming one input element each repetition and grows a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there. It repeats until no input elements remain.

![Insertion Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18na5tww41yb7zl53sm0.gif)

It takes one input at a time and compares it with the previous element in the list to place the selected element at the right place. For the first element, it compares with the next element. It iterated again and again until the last element of the list is put in the right place. After the last element, the list is sorted.

**When to use**
- **When the list is small**
- **When the list is almost sorted only a few elements is needed to sort**

---

## Selection Sort

> The algorithm divides the input list into two parts: a sorted sublist of items which is built up from left to right at the front (left) of the list and a sublist of the remaining unsorted items that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

![Selection Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3a6tnurdnn4we9hhdx1.gif)

Initially, the list is divided into two-part. One sorted which is at leftmost and other unsorted lists which is at the rightmost side. Also at the start sorted list is empty and all the element is in the unsorted list. Then we pick the smallest element from the unsorted list and put it into the sorted list. Then the length of the sorted list increases and the unsorted list decreases. The process continues until the unsorted list is empty.

**When to use**
- **To check whether the given list is already sorted or not**
- **When memory is small as it uses less swap**

---

## Heap Sort

> Heapsort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element from it and inserting it into the sorted region. Unlike selection sort, heapsort does not waste time with a linear-time scan of the unsorted region; rather, heap sort maintains the unsorted region in a heap data structure to more quickly find the largest element in each step.

![Heap Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rruxqp47ujoh9pikuk31.gif)

In heap sort, the input value is stored into a heap memory with the root of the largest value from the tree. Then the largest values are stored into an array of the list. The process goes on until the heap memory is empty and the output is the sorted array.

**When to use**
- **When the list is huge**
- **When to find the smallest or larger item in the list**

---

## Merge Sort

> Conceptually, a merge sort works as follows:
  - Divide the unsorted list into n sublists, each containing one element (a list of one element is considered sorted).
  - Repeatedly merge sublists to produce new sorted sublists until there is only one sublist remaining. This will be the sorted list.

![Merge Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/juts4riek84qvn24mv31.gif)

The list is divided into sublists, each list containing one element. Then each element is compared to its neighboring element and merged into another sublist according to order. This process continues to each sublist. Now each sublist has 2 elements, Now each sublist is compared to the neighbor's sublist. Each sublist having 2 elements. The list is merged into another sublist according to order. Now each sublist will have 4 elements. This process continues till there is only one sublist remaining. this sublist will be in sorted order.

**When to use**
- **When the list is linked list**
- **When the list is huge**

---

## Quick Sort

> Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. For this reason, it is sometimes called partition-exchange sort.[

![Quick SOrt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpcesk4beai7gra2wkgw.gif)

In quicksort, we pick an element from the list called a pivot(Mostly it is either the first or the last element of the list). Then we reorder the array in such a manner that all the elements that are less than the pivot are placed before the pivot and elements greater than pivot values are placed after the pivot value.

We repeat this step in both the sublists i.e. before and after the pivot value. This continues until the list is sorted.

**When to use**
- **When the list is small**
- **It is preferred for array**

---

## Counting Sort

> In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value and using arithmetic on those counts to determine the positions of each key value in the output sequence.

![Counting sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2m69b5046vkatwrk8npf.gif)

After taking input, counting sort count number of times the element is repeated in the list. Thus having 2 distinct values one for element and the other for the count. Then doing the arithmetic calculation on it and deciding the position of each element in the list.

For arithmetic calculation, read GeeksforGeeks article on [counting sort](https://www.geeksforgeeks.org/counting-sort/)

**When to use**
- **When the range of input values isn't greater than the number of values to be sorted.**
- **For a small list of array**

---

# Last Note
I hope this article helped you in learning algorithms. Thank You for reading the blog post.

### 🔗 Connect with me:

[**Twitter**](https://twitter.com/surajondev) [**GitHub**](https://github.com/surajondev)