Love and hate intertwined algorithm, hate only because of love too deep--Algorithm thought chapter

Source: Internet
Author: User
Tags sorts

I. Preface

Since the realization of the importance of the algorithm, LZ to the algorithm is love and hate, Love is because the algorithm is really interesting to learn,

If you have ever had an affair with mathematics, you will naturally have an affection for the algorithm after contacting the algorithm ...

The reason why hate, is to hate their ability limited, algorithm is too difficult, because too love will hate ...

Student time, the school data structure This course, so far regretted the classroom time to sleep ... Until the school recruit interview, many enterprises

All the algorithm mentioned table, Hui Nu doors ... Was asked to be speechless ... LZ only subconsciously understand where the time has gone?? I'll do anything.

It's so bad ... The four years of college are so meaningless ...

The algorithm is more demanding for logical thinking ability ... A problem can be implemented in many ways, and what we need to do is write a

The pleasing algorithm implementation, regardless of the structure of the code, or performance, we have to do as best as possible ...

Solve the same problem, some people may triumphantly sasa write hundreds of lines of code, and some people are concise and ten lines can achieve the same purpose ...

The algorithm is so attractive to me, because he solves the problem of diversity ...

Ii. The concept of sequencing

During the development of a computer program, a set of data elements or records is sorted by a keyword, and the sequenced sequence can be used to quickly find the relevant record.

The purpose of sorting by concepts is to be used for quick lookups.

How to measure the advantages and disadvantages of sorting algorithms

The pros and cons of a sorting algorithm can be measured according to three indicators:

1): time complexity : Analysis of the number of keywords and the number of records moved;

2): spatial complexity : How much auxiliary memory is needed to analyze the sorting algorithm;

3): stability : If the key values of the two keywords A and b are equal, but the order of A and B after sorting remains unchanged,

It is said that the sorting algorithm is stable, otherwise it is unstable.

Classification of establishments and sorts

1, internal sorting : The entire sorting process does not need to rely on external memory (such as disk, etc.), all sorting operations are done in memory.

2, the external sort : If the data element that participates in the sort is very many, the database is very big, the computer cannot put the whole sort process in memory to complete,

external memory (e.g., disk, etc.) must be used.

  Sub-categories under large classifications:

    1, Internal sorting algorithm classification:

1): Select sort (Direct select sort, heap sort)

2): Swap sort (bubble sort, quick sort)

3): Insert sort (direct insert sort, binary insert sort, shell sort)

4): Merge sort

5): Bucket sort

6): Base sort

    2, the external sorting algorithm classification:

1): Multi-way Merge sort

The original file is decomposed into several parts that can be loaded into memory one at a time, each part is transferred into the memory to complete the sorting,

The next step is to merge and sort multiple ordered sub-files.

That is, the large files in the batch into the memory to sort, and after the order to output the results to the external storage, waiting for

After all the records of the original file have been processed, the results of the above group sorting are finally carried out in two groups.

Merge sort.

Note: With memory capacity allowed, the more records are included in each group, the better, which reduces the number of merges.

The idea of internal sorting algorithm

  1. Direct selection of sorting

    1th Trip: The program locates the record on the 1th data , and compares it with each of the 1th data, if the 1th data is greater than one of the subsequent data,

Exchange them ... And so on, until the last data is compared, the smallest data in this set of data is selected, and it is ranked in 1th place.

    2nd trip: The program will record on the 2nd data , with the 2nd data followed by all the data to compare, if the 2nd data after a large fish, a data,

Exchange them ... And so on, until the last data is compared, the 2nd small data in this set of data is selected, and it is ranked 2nd.

( Note: The 1th data in the 2nd order is not taken into comparison )

Followed by ...

  

  2. Heap sequencing (take the big top heap as an example)

    1th trip : The index 0~n-1 all the data at the top of the heap, you can select the maximum value of this set of data, and then the big top heap of the root node and the group of data the most

After a node exchange, the maximum value of this set of data is ranked at the end.

   2nd trip : The index 0~n-2 all the data to build a large heap, you can select this set of data (0~n-2 data, does not include the 1th trip has been selected maximum value )

Maximum value (Note that the maximum value in the 1th Pass has been chosen and is not involved in this comparison, the maximum value here is the maximum value in the 0~n-2 data ),

The root node of the big Top heap in the 2nd trip is exchanged with the last 2nd node of this set of data (0~n-1 data, which contains the maximum value of the 1th Pass),

The 2nd largest value of this set of data is ranked in the bottom 2nd place.

Followed by ...

Description of the concept explanation:

Large Top heap: a set of data elements in their original order into a complete binary tree, and all nodes in the tree value is greater than the value of its left and right child nodes.

Small top heap: A set of data elements in their original order into a complete binary tree, and all nodes in the tree values are less than equal to the values of their left and right child nodes.

  3. Bubble sort

1th trip : Compare No. 0 and 1, 1 and 2, 2 and 3 in turn. n-2 and n-1 indexed elements, that is, the adjacent two data are compared if the previous data is greater than the next

data, exchanging them. After the 1th trip, the largest element is in the final order. ( the 1th trip has a comparison of n data )

     2nd trip : Compare No. 0 and 1, 1 and 2, 2 and 3 in turn. n-3 and n-2 index elements, if the previous data is larger than the last data, exchange them.

( Note: The data in the 2nd trip comparison does not contain the largest data that has been selected in the 1th pass, i.e. only n-1 data are compared ).

After the 2nd trip, the 2nd-largest element was ranked at the bottom 2nd.

Followed by ...

4. Quick Sort

    1th trip : Any value from the sort sequence is taken as the cutoff value (1th data is generally taken).

Define a pointer I, pointing to the first element on the left (that is, index subscript 0. If the cutoff value takes the 1th data, the index of the pointer I corresponds to the value of the element is the cutoff value),

Used to find the elements in a sequence that are smaller than the cutoff value;

Define a pointer J again, pointing to the first element on the right (that is, index subscript: n-1), which is used to find the element that is larger than the cutoff value in the sequence;

Compare the element and the cutoff value of the index to which the pointer J points.

(1): If it is less than the cutoff value, the element pointed to by the pointer J is assigned to the index pointed at the pointer i;

Then move the pointer I (at this point I point to the index subscript 1), the pointer I point to the index element and the cutoff value to compare,

[1]: If it is less than the cutoff value, then continue to move the pointer I, until I encounter the pointer I point to the element is greater than the cutoff value;

[2]: If greater than the cutoff value, the pointer I points to the index element is assigned to the index pointed to by the pointer J, switch to the pointer J move,

(2): if it is greater than the cutoff value, move the pointer J forward, where the pointer J points to index subscript (n-2),

And so on, until the element that the pointer J points to is less than the cutoff value.

The following, and so on (recursive) ...

      

Note: The pointer i is only responsible for finding the elements of the value of the score, the pointer J is only responsible for finding the value of the elements of the big score;

Once the pointer I points to an element with a large score bounds, the element is assigned to the index at which the pointer J points.

When the pointer i is not moving, the pointer j is operated, moving the pointer forward J;

Once the pointer J points to an element with a small score bounds, the element is assigned to the index to which pointer I points at this point.

At this point the pointer J does not move, the pointer I to operate, move the pointer backward i;

5. Direct Insert Sort

For a set of data with n elements, the direct insert sort is n-1 the insert Operation . The steps are as follows:

    1th Trip : The 2nd data is compared to the previous ordered sequence by one by one, when the ordered sequence in front only has 1 data. If

In turn ...

    n-1 : The nth data is compared with the first ordinal sequence in one by one, comparing with the first n-1 data, if the nth data is smaller than the n-1 data,

Then the first n-1 data is moved backward 1 bits, and then the value of the nth data is inserted in the position of the ordinal number n-1, otherwise, the n-1 data is not moved;

Compared with the n-2 data, if the nth data is smaller than the n-2 data, the first n-2~n-1 data is moved backwards by 1 bits,

The value of the nth data is inserted into the position of the ordinal n-2, otherwise the data of the nth n-2~n-1 is not moved;

In turn ...

Finally, compared with the 1th data data, if the nth data is smaller than the 1th data, the 1~n-1 data is moved backwards by 1 bits.

The value of the nth data is inserted into the position of the ordinal 1, otherwise the 1~n-1 data is not moved.

Note: Before each comparison, the nth data is stored in a variable, preventing the loss of nth data when the overall shift occurs.

6. Binary Insert Sort

The binary sort is optimized for direct insertion ordering.

Directly into the sort: when the n-1 needs to insert the nth element into the sequence of the preceding 1 to n-1 elements, he will always start with the first N-1 element,

Compare each element individually (the order of comparison is: n-1,n-2,......,1) until you find his position ... This obviously didn't make use of the front 1 to n-1

Element is already an ordered sequence of this feature ... The binary insertion sort is a simple optimization for this ... Binary insert the following actions:

Action 1: calculates the midpoint of the 1 to n-1 element (assuming that the element of their middle point is m), and then compares the nth element with M, if the nth element is large,

Search directly within half of the m~n-1, but in the half-range of 1~m. This is the general idea of binary ...

       Action 2: in half the scope of the search, and then follow the steps in Operation 1 again binary search ... Until the insertion position of the nth element is determined ...

7. Shell (Hill) sort

The shell sort is a simple improvement on the direct insert sort: It increases the spacing between the elements in the insertion sort, and inserts the sort in those spaced elements,

So that the data items are moved in a large span. When these data items have been sequenced, the shell sorting algorithm reduces the interval of the data items and then sorts them down in sequence.

The interval between data items when these sorts are made is called an increment. It is customary to use the letter H to denote increments.

To illustrate:

Pending sequence:-30-16 21* 23 9-49 21 30* 30

First trip Sort: h = 9/2 = 4 (where 9 is the number of elements with sorted sequence)

After sorting:-30-49 21* 23 9-16 21 30* 30

Second trip sort: h = 4/2 = 2

After sorting:-30-49 9-16 21* 23 21 30* 30

Third trip Sort: h = 2/2 = 1

After sorting: -49-30-16 9 21* 21 23 30* 30

  8. Merge sort

The merge sort is to treat an unordered sequence of length n as an ordered subsequence of n length 1, first doing 22 merges, and getting the ordered subsequence with a length of N/2 of 2.

Do 22 more merges ... Repeat this process and finally get an ordered sequence of length n ...

The key to the merge sorting algorithm is "merging", with the following specific operations (assuming: A and B are two ordered sequences to be merged):

       Action 1: Define the variable i,i initialization value is 0;

      Action 2: Define the variable j,j initialization value is 0;

      Action 3: Compare the elements in the I index of the a sequence with the elements at the J index in the B sequence and copy the smaller element into a temporary array:;

      Operation 4: If the I index element is small, i++, j + +;

      Action 5: Repeat the above 4 steps, you can copy the data elements in a, b two ordered sequence into a temporary array. Know the arbitrary in A, b

All elements in a sequence are copied into the temporary array. Finally, all the remaining elements in the other ordered sequence are copied to the temporary array,

The merge is complete, and the data in the temporary array is copied back ...

  9, barrel-type sorting

Bucket sorting is no longer a comparison-based sorting method, which requires a backlog of two features to be sorted:

1): All values in the pending sequence are in an enumerable range;

2): This enumerable range of pending sequences should not be too large, otherwise the sort overhead is too large.

I want to add ...

  10. Base Order

Cardinality sorting is not a general sort method, it is more like an application of a sort method, and the cardinality sort must depend on another sort method. The general idea of Cardinal sort is

Sorting the data to be sorted into multiple keywords, that is, the essence of the base sort is multi-keyword sorting.

Example (lowest-priority method):

Sequence to be ordered: 192 221 13 23

1th round comparison digit: 221 192 13 23

2nd round comparison 10-bit: 13 23 221 192

3rd round Comparison hundred: 13 23 192 221

Additional notes: For sorting the keywords that are selected when the cardinality is sorted, which sorting method is more appropriate? The answer is--bucket sort

Depending on the two characteristics of the bucket sort, and for the sub-keywords that are separated by multiple keywords, they must be in the enumerable range of 0-9,

This range is also very small, so the bucket sort is very efficient.

Land, concluding remarks

The algorithm is a wonderful thing, the more you will not like, because it is charming enough, enough to attract the programmer you to explore the ...

Although the halogen boiled on this road is still a rookie, if not an algorithmic engineer, usually will not be exposed to too much algorithmic knowledge, but the curiosity of a strong stew is like algorithm.

Hope to communicate with you to exchange algorithm experience, or a variety of professional knowledge exchange, after all, the technology is not pressure body ... Well, it's here today ...

Father's Day, everyone bo friends don't forget to give father a call, ask a good. That is the example of our life, there is no substitute ... Happy Father's Day!!!

If you think the blog is good, just click " recommend "or" enjoy a cup of boiled milk tea!!!

Love and hate intertwined algorithm, hate only because of love too deep--Algorithm thought chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.