[Technical dry goods] The best sorting and data structure in history

Foreword

I have been working for a while, and sometimes I will joking with my colleagues: " If you let me write a quick order now, I'm afraid I can't write it ."

If you do not touch the algorithm for a while, it is really easy to forget. Do not believe? You now think about whether you can write a heap sort by yourself.

Anyone who has experienced school recruitment knows that algorithms and data structures are inevitable.

In the written test, the most important thing is the test questions. For large companies like Pinduoduo and Toutiao, a few algorithm questions come up. If you do n’t have an AC, there are no interview opportunities.

Algorithm questions are also asked during interviews (on-site interviews or video interviews). The difficulty is definitely not as difficult as in the written exam. We can imagine a scene where half of the interview is going on. The interviewer asks you to reverse the binary tree and ask yourself now, will you still do it?

It's not far off. If you are still in college, you can start with the sorting and various basic data structures . It took me a week to make the eight basic sorting and linked list / binary tree / stack / queue into a beautiful PDF .

This PDF reading experience is definitely better than the public account and articles on major blog platforms. The PDF content is pure hand-to-hand , if you don't understand, you can ask me.

The following is a brief introduction to the eight basic sorting and basic data structures. The idea of ​​each sorting and the basic explanation and source code are in PDF.

 

Bubble Sort

Idea: The two are exchanged, the big one is placed at the back, and the maximum value is already at the end of the array after the first sort. Because the two are exchanged, it needs n-1 sorting (such as 10 numbers, 9 sorting)

Code implementation points: two for loops, the outer loop controls the number of sorting passes, and the inner loop controls the number of comparisons . After each trip, the number of comparisons should be reduced by 1.

 

 

Select sort

Idea: Find the largest element in the array and exchange it with the last element of the array. When there is only one number, there is no need to select, so it needs n-1 sorting

Code implementation points: two for loops, the outer loop controls the number of sorted passes, the inner loop finds the maximum number of current passes, and then exchanges with the last element of the current pass array

 

Insert sort

Idea: Insert an element into an existing ordered array. It is unknown whether there is ordered data at the beginning, so the first element of the element is regarded as ordered . Compare with an ordered array, if it is larger , it will be put directly, if it is smaller, it will move the position of the array element, and find a suitable position to insert . When there is only one number, there is no need to insert, so n-1 sorting is required

Code implementation: a for loop is embedded with a while loop implementation, the outer for loop controls the number of sorted trips, and the while loop finds a suitable insertion position (and the insertion position cannot be less than 0)

 

Quick sort

Prerequisites for learning quick sort: need to understand recursion

Idea: Find an element (node) in the array, place it smaller on the left of the node, and place it larger on the right of the node. A trip down, smaller than the node on the left, larger than the node on the right. Keep performing this operation ...

Code implementation: The pivot is taken in the middle, and L and R are used to denote the minimum and maximum positions of the array. The comparison is continued until a number smaller (larger) than the pivot is found, and then exchanged, continuously reducing the range. Recursion L to the element (j) before the pivot. One element (i) to R element after the recursive pivot

 

Merge sort

The premise of learning merge sort: need to understand recursion

Idea: Combine two sorted arrays into an ordered array. Separate the elements as an ordered array and compare and merge them . Continue to split and merge until there is only one element

Code implementation: In the first sorting process, it is essentially two elements (considered as two existing ordered arrays) to merge. Continue to perform such operations, and finally the array is ordered, split the left, right, merge ...

 

 

 

Heap sort

The premise of learning heap sorting: need to understand binary tree

Idea: Heap sort uses a feature of a complete binary tree. The root node is larger than the left and right children. Completing a heap construction operation is essentially comparing the size of the root node and the left and right children. The big one is switched to the root On the nodes, until the largest node is at the top of the tree . Then exchange with the last element of the array

Code implementation: As long as the left or right subtree is greater than the current root node, replace it. After the replacement, the following subtree will change, so it is also necessary to compare until each node realizes the condition of parent> child

 

 

 

Hill Sort

Idea: Hill sorting is essentially an enhanced version of insertion sorting. Hill sorting divides the array into n groups for insertion sorting, until the array is macroscopically ordered, and then there is no need to move so many times when performing insertion sorting. ~

Code thinking: Hill increment is generally gap = gap / 2, but there is more such a for loop than the normal version of insertion sort.

 

Cardinality sorting (bucket sorting)

Idea: Cardinality sorting (bucket sorting): cut the numbers into one, ten, one hundred, one thousand and put them into different buckets, and put them back in the bucket order once, until the number of the largest digits is finished ~ Then The array is ordered

Code implementation: Find the maximum value of the array first, and then use the maximum value / 10 as the condition of the loop (as long as> 0, then there are still digits). Distribute single digits, ten digits, ... to the bucket, and recycle each time it is allocated

 

 

 

Recursive

Recursion is used very, very much in the algorithm. The quick sort and merge sort of the sorting algorithm need to use recursion (at least it is most convenient to implement recursion).

If you want to use recursion, you must know two conditions: recursive exit (condition to terminate recursion) and recursive expression (law)

Tip: In recursion, the problem is often divided into two parts (1 and the whole idea) , which allows us to quickly find recursive expressions (laws)

 

Hanrota achieves:

 

 

 

Basic data structure

Linked lists, queues, binary trees, and stacks are all very basic data structures. There will be corresponding algorithm questions for each data structure, for example:

  • LeetCode No206: Reverse linked list
  • LeetCode No20: Check the string [] {]} {] {} (Whether such a string is valid (aligned)
  • LeetCode No104: Maximum depth of the tree
  • LeetCode No102: Sequence traversal tree

The school recruits do not ask for data structures such as dictionary trees, red-black trees, and graphs, but the problems of linked list, queue, binary tree, and stack data structures (LeetCode is simple) should still be able to be made.

At last

Finally, I want to explain that the code of the sorting algorithm / data structure may not be the optimal solution, and the implementation of the code is written in a way that is easier to understand. Almost every sentence has a corresponding comment, which should be understandable.

It has been working for a while now. Why do you want to write the most basic algorithms and data structures? The reasons are as follows:

  • I am a person who has a quest for typography . If I follow my classmates early on, I may find that my GitHub and article navigation read.me will change frequently. The current GitHub navigation is not what I like (too long), and the earlier article, to be honest, typesetting is not enough, I decided to start a wave again.
  • My article will be distributed on several platforms, but no one will be able to read it after the article is finished, and the map bed is likely to hang because of the platform's anti-theft chain. Because many readers asked me: " Can you convert your article to PDF ?"
  • I have written many series of articles, and these articles will hardly change much, so they are very suitable for " persistent ".

For the reasons above, I decided to summarize my series of articles into a PDF / HTML / WORD document. To be honest, it took me a lot of time to create such a document .

Document acquisition method: forwarding + attention, backstage private letter editor [documentation] can be obtained for free!

 

 

Document acquisition method: forwarding + attention, backstage private letter editor [documentation] can be obtained for free!

 

 

 

 

Published 238 original articles · Like 68 · Visits 30,000+

Guess you like

Origin blog.csdn.net/qq_45401061/article/details/104907907