Sorting is the act of rearranging elements in a sequence in order, either in numerical or lexicographical order, and either ascending or descending.
A number of basic algorithms run in O(n2) and should not be used in interviews. In algorithm interviews, you're unlikely to need to implement any of the sorting algorithms from scratch. Instead you would need to sort the input using your language's default sorting function so that you can use binary searches on them.
On a sorted array of elements, by leveraging on its sorted property, searching can be done on them in faster than O(n) time by using a binary search. Binary search compares the target value with the middle element of the array, which informs the algorithm whether the target value lies in the left half or the right half, and this comparison proceeds on the remaining half until the target is found or the remaining half is empty.
While you're unlikely to be asked to implement a sorting algorithm from scratch during an interview, it is good to know the various time complexities of the different sorting algorithms.
- Additional (only if you have time)
- Exponentially Easy Selection Sort, basecs
- Bubbling Up With Bubble Sorts, basecs
- Inching Towards Insertion Sort, basecs
- Making Sense of Merge Sort (Part 1), basecs
- Making Sense of Merge Sort (Part 2), basecs
- Pivoting To Understand Quicksort (Part 1), basecs
- Pivoting To Understand Quicksort (Part 2), basecs
- Counting Linearly With Counting Sort, basecs
- Getting To The Root Of Sorting With Radix Sort, basecs
|Counting sort||O(n + k)||O(k)|
|Radix sort||O(nk)||O(n + k)|
Things to look out for during interviews
Make sure you know the time and space complexity of the language's default sorting algorithm! The time complexity is almost definitely O(nlog(n))). Bonus points if you can name the sort. In Python, it's Timsort.
- Empty sequence
- Sequence with one element
- Sequence with two elements
- Sequence containing duplicate elements.
When a given sequence is in a sorted order (be it ascending or descending), using binary search should be one of the first things that come to your mind.
Sorting an input that has limited range
These are essential questions to practice if you're studying for this topic.
Recommended practice questions
These are recommended questions to practice after you have studied for the topic and have practiced the essential questions.
- Kth Smallest Element in a Sorted Matrix
- Search a 2D Matrix
- Kth Largest Element in an Array
- Find Minimum in Rotated Sorted Array
- Median of Two Sorted Arrays
AlgoMonster aims to help you ace the technical interview in the shortest time possible. By Google engineers, AlgoMonster uses a data-driven approach to teach you the most useful key question patterns and has contents to help you quickly revise basic data structures and algorithms. Best of all, AlgoMonster is not subscription-based - pay a one-time fee and get lifetime access. Join today for a 70% discount →