Today, this question was asked by the interviewer to quickly arrange it by handwriting on the spot. The scene is as follows:
Interviewer: Let’s continue chatting. Regarding data structures and algorithms, can you write a quick sort? (While speaking, he turned my resume over and handed me a pen, meaning he asked me to write on the back of my resume)
Rookie Me: What do you mean? Write it here? (Pointing to the resume)
Interviewer: Yeah
Rookie Me: No
Interviewer: Okay, that’s all for today’s interview
Rookie Me: (I’m very angry. It’s a labor-management resume. Do you want to write code on the labor-management resume?) Shadiao
Interviewer: (Looking back, confused)
Thinking about it, I was still too young, but that wouldn't be the case now. Just write, it’s just a piece of paper anyway.
Actually, quick queuing is simple, but I guess many people can’t write it by hand. Is it difficult? There are many people who can write it by hand on the spot in several ways.
I am a newbie, but I can still write by hand. After all, I just deliberately prepared "dictate writing" before the interview.
Next, we will analyze and analyze ---- quick sort.
From encyclopedia:
Quick sort was proposed by C. A. R. Hoare in 1962. Its basic idea is to divide the data to be sorted into two independent parts through one sorting. All the data in one part is smaller than all the data in the other part, and then use this method to quickly separate the two parts of the data. Sorting, the entire sorting process can be performed [recursively], so that the entire data becomes an ordered sequence.
It is quite difficult to understand this concept.
It can be understood this way:
Quick sort is an improved version of bubble sort. The whole process is to tear things apart and patch them up. Complement side by side until all elements reach an ordered state.
Core idea:
First take a number from the sequence as the base number, and then perform size partitioning;
During the partitioning process, compare this number Place all large numbers to its right, and put all numbers less than or equal to it to its left;
Repeat the second step for the left and right intervals until there is only one number in each interval, and the sorting is completed.
The following is a step-by-step disassembly in the form of pictures and texts.
Take the array [4,1,6,2,9,3]
as an example.
First pass:
The current array sequence is [3, 1, 2, 4, 9, 6].
Next step: Sort the left side first
2. The whole process of quick sorting
3. Code implementation
import java.util.Arrays; public class QuickSortDemo { //四个步骤: //1.比较startIndex和endIndex,更喜欢理解为校验 //2.找出基准 //3.左边部分排序 //4.右边排序 public static void quickSort(int[] arr, int startIndex, int endIndex) { if (startIndex < endIndex) { //找出基准 int partition = partition(arr, startIndex, endIndex); //分成两边递归进行 quickSort(arr, startIndex, partition - 1); quickSort(arr, partition + 1, endIndex); } } //找基准 private static int partition(int[] arr, int startIndex, int endIndex) { int pivot = arr[startIndex]; int left = startIndex; int right = endIndex; //等于就没有必要排序 while (left != right) { while (left < right && arr[right] > pivot) { right--; } while (left < right && arr[left] <= pivot) { left++; } //找到left比基准大,right比基准小,进行交换 if (left < right) { swap(arr, left, right); } } //第一轮完成,让left和right重合的位置和基准交换,返回基准的位置 swap(arr, startIndex, left); return left; } //两数交换 public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void main(String[] args) { int[] a = {3, 1, 2, 4, 9, 6}; quickSort(a, 0, a.length - 1); //输出结果 System.out.println(Arrays.toString(a)); } }
[1, 2, 3, 4, 6, 9]
There are several ways to write quick sort. If you are interested, you can look it up yourself. You can also look at how quick sort is introduced in Wikipedia.
Time complexity:
Worst The situation is that the element taken each time is the smallest/largest in the array. This situation is actually bubble sorting (the order of one element is arranged every time)
The time complexity in this case is good Calculated, it is the time complexity of bubble sort: T[n] = n * (n-1) = n^2 n;
The best case is O(nlog2n), derivation The process is as follows:
(Time complexity formula of recursive algorithm: T[n] = aT[n/b] f(n) )
https://img2018.cnblogs.com/blog/1258817/201903/1258817-20190326191158640-601403776.png
So The average time complexity is O(nlog2n)
Space complexity:
The space used by quick sort is O(1), which is a constant level; what really consumes space is the recursive call. Because some data must be maintained for each recursion:
The space complexity in the optimal case is: O(log2n); when the group is divided equally every time
The worst case space complexity is: O(n); it degenerates into bubble sorting
So the average space complexity is O(log2n)
Finally, do you think quick sort is useful at work? ? I have never used it after working for nearly ten years, but I know the idea of quick queue. If I don’t prepare before the interview, I definitely won’t be able to write it anyway. What about you?
The above is the detailed content of Meituan interview: Please handwrite a quick schedule, I was shocked!. For more information, please follow other related articles on the PHP Chinese website!