Interview Kickstart has enabled over 21000 engineers to uplevel.
Getting ready for your next technical interview? As a software developer, you must be well-versed with sorting algorithms, as they’re a frequent feature in coding interviews! These algorithms will help you solve many complex coding problems.
In this article, we will see differences between three sorting algorithms — selection sort, bubble sort, and insertion sort. Knowing these differences will help you choose the right algorithm based on the problem you’re solving.
For each algorithm, we’ll cover:
Then, we’ll compare the three algorithms based on:
We’ve also included a Selection Sort vs. Bubble Sort vs. Insertion Sort Cheat Sheet to help you review key differences between these algorithms at a glance.
Selection sort is a comparison-based sorting algorithm. It divides the input array into two subarrays:
In every iteration, we pick the minimum element (considering we have to sort the array in increasing order) from the unsorted subarray and place it at the end of the sorted subarray. We repeat this entire process until the unsorted array becomes empty.
Following are the steps to sort an array in increasing order using selection sort:
Following is the pseudocode to sort an array in ascending order using selection sort. We have followed 1-based indexing for the array.
Procedure: selection_sort (array A, size N)
for i ← 1 to N - 1
min_index ← i
for j ← i + 1 to N
if ( A[ min_index ] > A[ j ] )
min_index ←j
end if
end for
swap ( A[ i ], A[ min_index ] )
end for
end procedure
Bubble sort is also a comparison-based sorting algorithm. It makes several passes over the input array and, in each pass, swaps adjacent elements that are not in the required order (ascending or descending). It keeps on making passes until the entire array is sorted (makes N - 1 pass in the worst case when N is the size of the array).
Following are the steps to sort an array containing N elements in increasing order using bubble sort.
The first pass is complete — the largest element will be at the end of the array (as it will get consecutively swapped with the next adjacent element). So in the next pass, we don’t need to compare the last two elements anymore. Formally, in the i-th pass, the last (i - 1) elements are correctly placed, so we need to compare elements from 1st position to (N - i + 1)th position only.
Optimizing bubble sort: If, in a pass, we do not make any swaps, then we don’t need to make any further passes.
Following is the pseudocode to sort an array in ascending order using bubble sort. We have followed 1-based indexing for the array.
Procedure: bubble_sort (array A, size N)
for i ← 1 to N - 1
for j ← 1 to N - i
boolean swaps ← false
if ( A[ j ] > A[ j + 1 ] )
swap ( A[ j ], A[ j + 1 ] )
swaps ←true
end if
end for
if (swaps == false)
exit
end if
end for
end procedure
Insertion sort is also a comparison-based sorting algorithm. It also maintains two subarrays: a sorted subarray that initially contains only the first element and an unsorted subarray that contains the rest of the array.
In this method, we pick each element from the unsorted part one by one and insert it in the sorted part at its correct place.
Following are the steps to sort an array containing N elements in increasing order using insertion sort.
Repeat steps 2, 3, and 4 until there is no element left in the unsorted array (precisely N - 1 times, as there are that many elements in the unsorted part initially).
Following is the pseudocode to sort an array in ascending order using insertion sort. We have followed 1-based indexing for the array.
Procedure: insertion_sort (array A, size N)
for i ← 2 to N
key ← A[ i ]
j ← i - 1
while ( j >= 1 and A[ j ] > key)
A[ j + 1 ] ← A[ j ]
j ←j - 1
end while
A[j+1] ← key
end for
end procedure
If you need more information, we have covered each of these algorithms articles in great detail along with code implementation and examples in the following articles: Selection Sort, Bubble Sort, Insertion Sort.
For random datasets, all three algorithms take O(N2) time complexity. The expected (also average) number of inversions in a randomly generated dataset of size N having distinct elements is N * (N - 1) / 4.
You can refer to the following sheet to see the comparison among selection, bubble, and merge sort at a glance.
Question 1: Out of Bubble sort, selection sort, and insertion sort, which sorting algorithm should be used if swapping between two memory locations is expensive?
Answer: Selection sort should be used in such cases. For an array of size N, in the worst case, this algorithm makes N-1 swaps as opposed to N*(N-1)/2 swaps in bubble sort and selection sort.
Question 2: In which context is insertion sort is better than selection sort?
Answer: Insertion sort is stable and adaptive, while selection sort is not. Also, insertion sort is online, meaning that it can start sorting data even if the data isn’t fully available in the beginning.
Question 3: What is common among bubble sort, selection sort, and insertion sort?
Answer: Bubble sort, selection sort, and insertion sort algorithms are comparison-based. All have the same worst-case and average-case complexity. All of them are in-place as well.
Sorting algorithms interview questions feature in almost every coding interview for software developers. If you’re looking for guidance and help to nail these questions and more, sign up for our free webinar.
As pioneers in the field of technical interview prep, we have trained thousands of software engineers to crack the toughest coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
--------
Article contributed by Divyansh Gupta
Attend our webinar on
"How to nail your next tech interview" and learn