[Programming beads] search, sort, B + tree

Foreword

  "Programming Pearl" is indeed a good book. It interprets algorithms and data structures. The analysis of problems can help programmers change their attitudes toward data structures and algorithms. Find and sort inside;

Find

  Searching, searching, the most important concept is binary search, many data structures actually apply the concept of binary search, such as jump table, red black tree, B + tree

Application: Database

  It is the most important thing to choose the right data structure for an application problem. We know that many times, programmers need to choose between time and space. Choosing the right structure is one of the important ways of both time and space overhead;

  Why is B + tree? People often ask this question about the data structure of the database. In fact, the answer is because the B + tree can meet the following three points, and these three points are just common operations of SQL statements:

  1. Can quickly locate an element based on id;
  2. Support to quickly find all elements in a certain range of an element;
  3. Can quickly find n elements before or after a certain element value;

  So after the problem is defined, we start to find the corresponding data structure. The first point, we think of Hash, but obviously, Hash does not support 2 and 3, so we need to compromise, choose binary search, and binary search for optional data Structure: balanced binary tree, jump table, red and black tree, B + tree;

  The third point, it is natural to think of a doubly linked list, but the linked list does not meet the second point; at this time, the data structure of the jump table is more appropriate:

【Photo Source Network】

  Then improve the jump table, and you have a B + tree. Looking at the figure below, why does a node need multiple elements? In fact, the reason is because of the gap between the speed of the disk and the memory, so it is best to load the data from the disk at one time just a node, so the node stores the best element, and the size of this value is the size of the cache page 16K;

【Photo Source Network】

  In fact, there is another thing worth noting. The data structure used by MongoDB is not a B + tree; it is a B-tree. In fact, it is easy to understand. The search requirements of document databases are usually different. They only need to find the value, and rarely do Range search

Application: 9 Palace keyboard 

  This example is familiar to most post-90s. The programmers of that generation estimated that many people encountered this need; the following figure is a 9-square keyboard, where if the data is a certain word, you can only press 0, 1, 2, 3 , 4, 5, 6, 7, 8, and 9 keyboards, for example, Fan and Dan, both English or Pinyin, all press the same keyboard number, which means that all words have their own Requirements for small partners with the same key: How to quickly find the record with the word name fan among hundreds of millions of records according to the user's key input?

 

 

  There are two kinds of questions used in this question. The first is sorting, and the second is searching. Naturally, sorting is to sort hundreds of millions of records. Searching uses binary search; the point of the question is, what sort?

  Logo : This word was first seen in programming Zhuji, as the same keys are called homonyms, and the positive sequence of homonyms is unique, so this sequence is the representation of homonyms; it gives hundreds of millions of records After all the labels are attached, we can sort again according to the label, so an ordered record appears, assuming that we are using merge sort. I won't say much about the following search. At this point, the problem is solved.

 

Guess you like

Origin www.cnblogs.com/iCanhua/p/12723681.html