Spiral 50

0 of 19 lessons completed0 of 50 problems solved

Helper data structures and core patterns
Hash table
Stack
Queue
Heap
Core patterns
Problems
Linked lists, trees and graphs
Linked list
Tree
Binary Search Tree
Trie
Graph
Graph algorithms
Problems
Backtracking, greedy and dynamic programming
Backtracking
Greedy vs dynamic programming
Problems
Fundamentals

Sorting

Sorting puts the elements of an array in order. In interview problems, it is usually used when sorting the array makes the problem simpler.

Also, the techniques used in sorting algorithms often come up in other problems, so it is worth understanding how they work.

Bubble sort. O(n²) time.

Compare every two neighbours with two nested loops and swap them when they are in the wrong order, so every pass moves the greatest remaining element to the end.

4
2
5
1
3
01234
ji
4 > 2, swap
1 / 11
Bubble sort
Python
def bubble_sort(nums: list[int]) -> list[int]:
# Sort nums in place with bubble sort and return it
return nums
print(bubble_sort([5, 2, 4, 1, 3]))

In practice, bubble sort is too slow for large arrays. It is mostly used only as a teaching example.

Merge sort. O(n log n) time.

Split the array in half, sort each half, then merge them. Each half is sorted with the same steps, until an array has one element, which is already sorted.

5
2
8
1
9
3
7
4
split [5, 2, 8, 1, 9, 3, 7, 4] in half
1 / 7

To merge two sorted halves, start an i index at the first element of the left half and a j index at the first element of the right half. Take the smaller of the two elements, add it to the merged array and move its index.

1
2
5
8
i
3
4
7
9
j
1 < 3, take 1
1 / 9
Merge sort
Python
def merge_sort(nums: list[int]) -> list[int]:
# Return the values of nums sorted with merge sort
return nums
print(merge_sort([5, 2, 8, 1, 9, 3, 7, 4]))

The technique of splitting the array recursively and then merging the halves is common in interview problems, often in a less obvious form than plain sorting.

The built-in sort functions in many programming languages use merge sort under the hood, because it is stable and takes O(nlog⁡n)O(n \log n) time even in the worst case.

Quick sort. O(n log n) time on average.

Take any element as the pivot, and partition the array, putting the elements less than the pivot to the left, and the rest to the right.

This way the pivot ends up at the index it has in the sorted array. Then sort both sides of the pivot the same way.

The partition uses two indexes. i iterates through the range, and j is the index where the next element less than the pivot goes, so every element before j is less than the pivot.

3
2
1
5
6
4
012345
ij
take 4 as a pivot, 3 < 4, belongs left
1 / 18
Quick sort
Python
def quick_sort(nums: list[int]) -> list[int]:
# Sort nums in place with quick sort and return it
return nums
print(quick_sort([5, 2, 4, 1, 3]))

The time is O(nlog⁡n)O(n \log n) on average, because a pivot usually splits the range into two parts of similar size. In the worst case it can be O(n2)O(n^2) time.

The partition step is very common in interview problems, even when the problem is not about sorting.

Counting sort. O(n + k) time.

Create a separate array of length kk, the greatest value in the input array, so that every value from 0 to the greatest has its own index.

Count how many times every value appears, then write the values back in order.

nums
3
1
2
3
1
0
012345
i
count
0
0
0
0
0123
count 3
1 / 11
Counting sort
Python
def counting_sort(nums: list[int]) -> list[int]:
# Sort nums in place with counting sort and return it
return nums
print(counting_sort([3, 1, 2, 3, 1, 0]))

This approach is fast when the number of possible values is not much greater than the length of the array.

The technique of counting values in a separate array of a fixed size is very common in interview problems when the number of possible values is small.

Was this lesson helpful?