0% found this document useful (0 votes)
40 views2 pages

5 Essential Sorting Algorithms Explained

The document discusses five essential sorting algorithms: Insertion Sort, Selection Sort, Bubble Sort, Merge Sort, and Quick Sort, explaining their mechanisms and use cases. Each algorithm has its advantages and disadvantages, making them suitable for different data sizes and conditions. The document also provides pseudocode for each algorithm to illustrate their implementation.

Uploaded by

jaysahu0201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

5 Essential Sorting Algorithms Explained

The document discusses five essential sorting algorithms: Insertion Sort, Selection Sort, Bubble Sort, Merge Sort, and Quick Sort, explaining their mechanisms and use cases. Each algorithm has its advantages and disadvantages, making them suitable for different data sizes and conditions. The document also provides pseudocode for each algorithm to illustrate their implementation.

Uploaded by

jaysahu0201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Search Write Sign up Sign in

5 Sorting Algorithms Every


Programmer Should Know
Insertion, selection, bubble, merge, and quick sort

Adwiteeya Reyna · Follow


Published in Better Programming · 10 min read · Oct 24, 2020

421 5

Photo by Roberto Nickson on Unsplash

Ever wonder how the products in an Amazon or any other e-commerce


website get sorted when you apply filters like low-to-high or high-to-low, or
alphabetically? Sorting algorithms play a vital role for such websites where
you have an enormous amount of products listed and you have to make
customer interactions easy.

A sorting algorithm is used to rearrange a given array or list of elements as


per the comparison operator on the element. The comparison operator is
used to decide the new order of elements in the respective data structure.
Mainly there are five basic algorithms used and you can derive multiple
algorithms using these basic algorithms. Each of these algorithms has some
pros and cons and can be chosen effectively depending on the size of data to
be handled.

1. Insertion Sort

2. Selection Sort

3. Bubble Sort

4. Merge Sort

5. Quick Sort

For a complete Data Structures and Algorithm cheatsheet, fork this GitHub repo.

1. Insertion Sort
Insertion sort is a simple sorting algorithm that works similarly to the way
you sort playing cards in your hands. The array is virtually split into a sorted
and an unsorted part. Values from the unsorted part are picked and placed at
the correct position in the sorted part. Insertion sort is fast and best suitable
either when the problem size is small (because it has low overhead) or when
the data is nearly sorted (because it is adaptive).

Example:
elements: 9, 6, 5, 0, 8, 2, 7, 1, 3, 4
i : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Let us loop for i = 1 (second element of the array) to 9 (last
element of the array)
i=1. Since 6 is smaller than 9, move 9 and insert 6 before 9
6, 9, 5, 0, 8, 2, 7, 1, 3, 4
i=2. Since 5 is smaller than 6 and 9, move 5 before 6 and 9
5, 6, 9, 0, 8, 2, 7, 1, 3, 4
i=3. Since 0 is smaller than 5,6 and 9, move 0 before 5,6,9
0, 5, 6, 9, 8, 2, 7, 1, 3, 4
i=4. Since 8 is smaller than 9, move 8 before 9
0, 5, 6, 8, 9, 2, 7, 1, 3, 4

i=5. Since 2 is smaller than 5,6,8 and 9, move 2 before 5,6,8,9


0, 2, 5, 6, 8, 9, 7, 1, 3, 4
i=6. 0, 2, 5, 6, 7, 8, 9, 1, 3, 4
i=7. 0, 1, 2, 5, 6, 7, 8, 9, 3, 4
i=8. 0, 1, 2, 3, 5, 6, 7, 8, 9, 4
i=9. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Insertion Sort

Algorithm:

Insertion-Sort(A)
{
for j=i to [Link]
key = A[i];
// insert A[i] into sorted sequence A[1,2,3,..,i-1]
j= i-1;
while (j>0 and A[j]>key)
A[j+1] = A[j]
j= j-1
A[j+1] = key
}

2. Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from the unsorted part
and putting it at the beginning. The algorithm maintains two subarrays in a
given array:

The subarray which is already sorted

Remaining subarray which is unsorted

In every iteration/pass of selection sort, the minimum element (considering


ascending order) from the unsorted subarray is picked and moved to the
sorted subarray. The selection sort has the property of minimizing the
number of swaps. Therefore, it is the best choice when the cost of swapping
is high.

Example:
arr[]= 23 78 45 8 32 46

Pass 1
// Find the minimum element in arr[0...5] and place it at beginning
8 78 45 23 32 46
Pass 2
// Find the minimum element in arr[1...5] and place it at beginning
of arr[1...5]
8 23 45 78 32 46

Pass 3
// Find the minimum element in arr[2...5] and place it at beginning
of arr[2...5]
8 23 32 78 45 46

Pass 4
// Find the minimum element in arr[3...5] and place it at beginning
of arr[3...5]
8 23 32 45 78 46
Pass 5
// Find the minimum element in arr[4...5] and place it at beginning
of arr[4...5]
8 23 32 45 46 78

Selection Sort

Algorithm:

void SelectionSort (int a[], int n)


{
int i,j, temp, min;
for (i=0; i<n-1; i++)
{
min = i;
for (j=i+1; j<n; j++)
if (a[j] < a[min])
{
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}

3. Bubble Sort
Bubble Sort is the sorting algorithm that works by repeatedly swapping the
adjacent elements if they are in the wrong order. After each iteration or pass,
the largest element reaches the end (in case of ascending order) or the
smallest element reaches the end (in case of descending order). The pass
through the list is repeated until the list is sorted. This algorithm is not
suitable for large data sets as its average and worst-case complexity are of
Ο(n^2) where n is the number of items

Example:
64 34 25 12 22 11 90
Iteration 1:
(64 34 25 12 22 11 90) -> (34 64 25 12 22 11 90), Here, algorithm
compares the first two elements, and swaps since 64 > 34.
(34 64 25 12 22 11 90) -> (34 25 64 12 22 11 90), Swap since 64 > 25
(34 25 64 12 22 11 90) -> (34 25 12 64 22 11 90), Swap since 64 > 12
(34 25 12 64 22 11 90) -> (34 25 12 22 64 11 90), Swap since 64 > 22
(34 25 12 22 64 11 90) -> (34 25 12 22 11 64 90), Swap since 64 > 11
(34 25 12 22 11 64 90) -> (34 25 12 22 11 64 90), Now, since these
elements are already in order (90 > 64), algorithm does not swap
them.

Iteration 2:
(34 25 12 22 11 64 90) -> (25 34 12 22 11 64 90), Swap since 34 > 25
(25 34 12 22 11 64 90) -> (25 12 34 22 11 64 90), Swap since 34 > 12
(25 12 34 22 11 64 90) -> (25 12 22 34 11 64 90), Swap since 34 > 22
(25 12 22 34 11 64 90) -> (25 12 22 11 34 64 90), Swap since 34 > 11
(25 12 22 11 34 64 90) -> (25 12 22 11 34 64 90), Now, since these
elements are already in order (64 > 34), algorithm does not swap
them.

Iteration 3:
(25 12 22 11 34 64 90) -> (12 25 22 11 34 64 90), Swap since 25 > 12
(12 25 22 11 34 64 90) -> (12 22 25 11 34 64 90), Swap since 25 > 22
(12 22 25 11 34 64 90) -> (12 22 11 25 34 64 90), Swap since 25 > 11
(12 22 11 25 34 64 90) -> (12 22 11 25 34 64 90), Now, since these
elements are already in order (34 > 25), algorithm does not swap
them.

Iteration 4:
(12 22 11 25 34 64 90) -> (12 22 11 25 34 64 90)
(12 22 11 25 34 64 90) -> (12 11 22 25 34 64 90), Swap since 22 > 11
(12 11 22 25 34 64 90) -> (12 11 22 25 34 64 90)
Iteration 5:
(12 11 22 25 34 64 90) -> (11 12 22 25 34 64 90), Swap since 12 > 11
(11 12 22 25 34 64 90) -> (11 12 22 25 34 64 90)
Iteration 6:
(11 12 22 25 34 64 90) -> (11 12 22 25 34 64 90)

Now, the array is already sorted, but our algorithm does not know if
it is completed. The algorithm needs one whole pass without any swap
to know it is sorted.
Iteration 7:
(11 12 22 25 34 64 90) -> (11 12 22 25 34 64 90)
(11 12 22 25 34 64 90) -> (11 12 22 25 34 64 90)
(11 12 22 25 34 64 90) -> (11 12 22 25 34 64 90)
(11 12 22 25 34 64 90) -> (11 12 22 25 34 64 90)
(11 12 22 25 34 64 90) -> (11 12 22 25 34 64 90)
(11 12 22 25 34 64 90) -> (11 12 22 25 34 64 90)

Bubble Sort

Algorithm:
Bubble_Sort(int a[], n)
{
int swapped, i, j;
for (i=0; i<n; i++)
{
swapped = 0;
for (j=0; j<n-i-1; j++)
{
if (a[j] > a[j+1])
{
swap (a[j], a[j+1]);
swapped = 1;
}
}
if (swapped == 0)
break;
}
}

4. Merge Sort
Unlike the above three sorting algorithms, this algorithm is based on the
divide-and-conquer technique. It divides the input array into two halves,
calls itself for the two halves, and then merges the two sorted halves. The
heart of the Merge Sort is the merge() function, which is used for merging
two halves. The merge(A, p, q, r) is a key process that assumes that A[p..q]
and A[q+1..r] are sorted and merges the two sorted sub-arrays into one.

Merge Sort is the only option when you need a stable and O(N log N) sort.

merge( ) function
Merge procedure is also known as out-of-place procedure

merge( ) function

Algorithm:

merge(A, p, q, r)
{
n1= q-p+1
n2= r-q
Let L[1:n+1] and R[1:n2+1] be new arrays
for (i=1:n1)
L[i]= A[p+i-1]
for (j=1:n2)
R[j]= A[q+j]
L[n1 + 1]= infinity
R[n2 + 1]= infinity
i=1, j=1
for (k=p:r)
{
if (L[i] <= R[j])
A[k] = L[i]
i= i+1
else
A[k] = R[j]
j= j+1
}
}

Merge sort:
The entire merge sort works in the following manner:

Merge Sort

Algorithm:

Merge_Sort(A, p ,r)
{
if p<r
q= [(p+r)/2]
Merge_Sort(A, p ,q)
Merge_Sort(A, q+1, r)
merge(A, p, q, r)
}

5. Quick Sort
Quick Sort is also a Divide and Conquer algorithm. It picks an element as a
pivot and partitions the given array around the picked pivot such that all the
smaller elements are to the left of the pivot and all the greater elements are
to the right of the pivot. There are many different versions of quickSort that
pick pivot in different ways:

Always pick the first element as a pivot.

Always pick the last element as the pivot (implemented below).

Pick a random element as a pivot.

Pick the median as a pivot.

The key process in quicksort is the partition() method. The target of


partitions is, given an array and an element r of the array as a pivot, put r at
its correct position in a sorted array and put all smaller elements (smaller
than r) before r, and put all greater elements (greater than r) after r. All this
should be done in linear time.

For small inputs, quicksort is the best algorithm as compared to the merge
sort. When you don’t need a stable sort and average-case performance
matters more than worst-case performance, go for quicksort. Let’s see the
partition algorithm and its implementation first.

partition( ) algorithm
We start from the rightmost element and keep track of the index of smaller
(or equal to) elements as r.

If we find an element j which is less than r, then we increment i pointer


and swap the elements of i and j.

If we find an element j which is greater than r, then we simply increment


j pointer.

Quick sort

Algorithm:
partition(A, p, r)
{
x= A[r]
i= p-1
for (j= p:r-1)
{
if (A[j] <= x)
{
i= i+1
exchange A[i] with A[j]
}
}
exchange A[i+1] with A[r]
return i+1
}

quick sort
The entire quick sort works in the following manner:

It checks for condition p<r. If True, it enters the if loop else it comes out
of the loop

Then, the partition algorithm is applied in order to choose the pivot


element and put it in the right place.

After the partition algorithm, the entire array is divided into two halves
such that all the elements smaller than the pivot element are to the left of
it and all the elements greater than the pivot element are to the right of it.

Quick Sort is applied on both halves.

The entire loop continues to break the array into two parts till we find an
element such that p>r.

Algorithm:
Quick_Sort(A, p ,r)
{
if (p<r)
{
q= partition(A, p, r)
Quick_Sort(A, p, q-1)
Quick_Sort(A, q+1, r)
}
}

Quick Sort

Programming Data Structures Algorithms Data Learning To Code

421 5

Written by Adwiteeya Reyna Follow

63 Followers · Writer for Better Programming

Software Developer | Web Developer | Database Developer

More from Adwiteeya Reyna and Better Programming

Adwiteeya Reyna Benoit Ruiz in Better Programming

Data Cube Advice From a Software Engineer


Advance form of 2D tabular data With 8 Years of Experience
Practical tips for those who want to advance
in their careers

4 min read · Jul 13, 2020 22 min read · Mar 21, 2023

55 1 13.3K 248

Sami Maameri in Better Programming Adwiteeya Reyna

Building a Multi-document Reader Data Analysis vs. Data Analytics


and Chatbot With LangChain and… The two commonly used terms in Data
The best part? The chatbot will remember Science
your chat history

17 min read · May 20, 2023 2 min read · Jun 26, 2020

1.3K 12 108

See all from Adwiteeya Reyna See all from Better Programming

Recommended from Medium

Kerry Parker in Towards Data Science Ankit Singh

8 takeaways from Data Science Mastering Sliding Window


interviews Techniques
Applying for jobs can be a time-consuming The sliding window technique is a common
process, in this article I’ve pulled together 8… algorithmic approach used for solving vario…

8 min read · 3 days ago 6 min read · Aug 8, 2023

74 175

Lists

General Coding Knowledge Coding & Development


20 stories · 782 saves 11 stories · 373 saves

Stories to Help You Grow as a ChatGPT


Software Developer 23 stories · 385 saves
19 stories · 703 saves

Gowtham Oleti Hira Ahmad

Apps I Use And Why You Should Navigating the Pitfalls:


Too. Understanding and Mitigating…
Let’s skip past the usual suspects like In the dynamic realm of software engineering,
YouTube, WhatsApp and Instagram. I want t… the pursuit of creating robust, scalable, and…

10 min read · Nov 14, 2023 2 min read · Dec 5, 2023

13.8K 223 301 1

BeyondVerse Arslan Ahmad in Level Up Coding

Sorting Algorithms: From Bubble Don’t Just LeetCode; Follow the


Sort to Quick Sort Coding Patterns Instead
Sorting algorithms are a fundamental part of What if you don’t like to practice 100s of
computer science and programming. They… coding questions before the interview?

12 min read · Nov 5, 2023 5 min read · Sep 16, 2022

Membership
10 7.5K 45
Free
Access the best member-only stories.

Distraction-free reading. No ads. Support independent authors.

See more recommendations Organize your knowledge with lists and Listen to audio narrations.
highlights.
Sign up to discover human stories that Read offline.

deepen your understanding of the world. Tell your story. Find your audience.
Join the Partner Program and earn for
your writing.

Sign up for free

Try for $5/month


Help Status About Careers Blog Privacy Terms Text to speech Teams
Membership

Free
Access the best member-only stories.

Distraction-free reading. No ads. Support independent authors.

Organize your knowledge with lists and Listen to audio narrations.


highlights.
Sign up to discover human stories that Read offline.

deepen your understanding of the world. Tell your story. Find your audience.
Join the Partner Program and earn for
your writing.

Sign up for free

Try for $5/month

You might also like