contents: revamp basic algo content

This commit is contained in:
Yangshun
2022-04-02 14:43:27 +08:00
parent 83d8625ad8
commit ce79ad0d9b
25 changed files with 1093 additions and 327 deletions

View File

@ -1,25 +1,82 @@
---
id: sorting-searching
title: Sorting and searching
toc_max_heading_level: 2
---
## Tips
## Introduction
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(n<sup>2</sup>) 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.
## Time complexity
| Algorithm | Time | Space |
| -------------- | ---------------- | --------- |
| Bubble sort | O(n<sup>2</sup>) | O(1) |
| Insertion sort | O(n<sup>2</sup>) | O(1) |
| Selection sort | O(n<sup>2</sup>) | O(1) |
| Quicksort | O(nlog(n)) | O(log(n)) |
| Mergesort | O(nlog(n)) | O(n) |
| Heapsort | O(nlog(n)) | O(1) |
| Counting sort | O(n + k) | O(k) |
| Radix sort | O(nk) | O(n + k) |
| Algorithm | Big-O |
| ------------- | --------- |
| Binary search | O(log(n)) |
## Learning resources
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.
- Readings
- [Sorting Out The Basics Behind Sorting Algorithms](https://medium.com/basecs/sorting-out-the-basics-behind-sorting-algorithms-b0a032873add), basecs
- [Binary Search](https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/), Khan Academy
- Additional (only if you have time)
- [Exponentially Easy Selection Sort](https://medium.com/basecs/exponentially-easy-selection-sort-d7a34292b049), basecs
- [Bubbling Up With Bubble Sorts](https://medium.com/basecs/bubbling-up-with-bubble-sorts-3df5ac88e592), basecs
- [Inching Towards Insertion Sort](https://medium.com/basecs/inching-towards-insertion-sort-9799274430da), basecs
- [Making Sense of Merge Sort (Part 1)](https://medium.com/basecs/making-sense-of-merge-sort-part-1-49649a143478), basecs
- [Making Sense of Merge Sort (Part 2)](https://medium.com/basecs/making-sense-of-merge-sort-part-2-be8706453209), basecs
- [Pivoting To Understand Quicksort (Part 1)](https://medium.com/basecs/pivoting-to-understand-quicksort-part-1-75178dfb9313), basecs
- [Pivoting To Understand Quicksort (Part 2)](https://medium.com/basecs/pivoting-to-understand-quicksort-part-2-30161aefe1d3), basecs
- [Counting Linearly With Counting Sort](https://medium.com/basecs/counting-linearly-with-counting-sort-cd8516ae09b3), basecs
- [Getting To The Root Of Sorting With Radix Sort](https://medium.com/basecs/getting-to-the-root-of-sorting-with-radix-sort-f8e9240d4224), basecs
## Corner cases
- Empty sequence
- Sequence with one element
- Sequence with two elements
- Sequence containing duplicate elements.
## 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](https://en.wikipedia.org/wiki/Timsort).
## Techniques
### Sorted inputs
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.
## Sample questions
### Sorting an input that has limited range
- Sorting search results on a page given a certain set of criteria.
- Sort a list of numbers in which each number is at a distance `K` from its actual position.
- Given an array of integers, sort the array so that all odd indexes are greater than the even indexes.
- Given users with locations in a list and a logged-in user with locations, find their travel buddies (people who shared more than half of your locations).
- Search for an element in a sorted and rotated array.
- [Source](http://blog.gainlo.co/index.php/2017/01/12/rotated-array-binary-search/)
- Merge two sorted lists together.
- Give 3 distinct algorithms to find the K largest values in a list of N items.
- Find the minimum element in a sorted rotated array in faster than O(n) time.
- Write a function that takes a number as input and outputs the biggest number with the same set of digits.
- [Source](http://blog.gainlo.co/index.php/2017/01/20/arrange-given-numbers-to-form-the-biggest-number-possible/)
[Counting sort](https://en.wikipedia.org/wiki/Counting_sort) is a non-comparison-based sort you can use on numbers where you know the range of values beforehand. Examples: [H-Index](https://leetcode.com/problems/h-index/)
## Recommended questions
- [Binary Search](https://leetcode.com/problems/binary-search/)
- [Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)
- [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)
- [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/)
- [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)
- [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)
- [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)
## Recommended courses