Sort Algorithms Based on Algorithms

Source: Internet
Author: User

    Context

    Algorithm

    What is an algorithm?

    An algorithm is an accurate and complete description of a solution and a series of clear instructions for solving the problem. An algorithm represents a system method to describe the policy mechanism for solving the problem. A sorting algorithm is a solution for sorting, and a search algorithm is a solution for searching.

    What are algorithms used?

    Increasing computer speed and saving storage space has always become a direction for programmers. Sorting has become one of the considerations of programmers, the proper sorting method directly affects the execution speed of the program and the occupation of the auxiliary storage space, thus affecting the performance of the entire software.

    2000 words are omitted here ......

    Sorting Algorithm

    The so-called sorting is to sort a group of unordered sequences in sequence by keywords. In the data to be sorted, if there are multiple identical keywords and the relative order of the same keywords remains unchanged after sorting, the sorting is stable; otherwise, the sorting is unstable, for example:

    The following examples do not separately describe the Ascending Order (from small to large ).

    Insert sort

    The basic idea of insert sorting is to extract an element from the waiting for sorting, and then extract one element one by one. Compare it with the current element and insert it to a proper position until all the elements are inserted.


    Insert sort directly

    Directly sort elements R1, R2 ...... The Ri-1 has arranged the order, compare the I-th element with R1 to the Ri-1 in sequence, find the appropriate position. Sort the data directly by n-1.

    The following example uses the number 1-7 to sort the data after the order is disordered:

    Hill sorting

    An upgraded version of directly inserted sorting during Hill sorting. I think the upgrade of hill sorting is: Group Elements by step, and then sort the elements in the group, until the complete sorting, the essence is the grouping insertion method.

    Take the 7 digits that have been disrupted as an example (the same color line is a group ):

    Select sort

    The basic idea of sorting is to select the smallest record from the elements with sorting, and sort the records in sequence.


    Select sort directly

    Take the 7 digits that have been disrupted as an example:

    Heap sorting

    The basic idea of heap sorting is to first put all elements in a full binary tree through hierarchical traversal. This binary tree must meet the following requirements: the sub-node value is greater than the parent node value (small top heap ), or the sub-node value is smaller than the parent node value (large top heap). The steps are as follows:

  1. Then we can know that the root node value in this tree is the smallest (small top heap)
  2. The element with the largest number is exchanged with the root node element.
  3. Repeat steps 1 and 2

Take the 7 digits that have been disrupted as an example:

Discharge the correct Big Top heap:

Check the root node location and the maximum number node location, and disconnect the original root node from the heap, as shown below:

Sort the remaining trees in the descending order:

Repeat the preceding steps to obtain:

Traverse by level to obtain the sorting result.

As can be seen from the above, in the forward order, you need to build a large top heap first; in the reverse order, you need a small top heap. One-bit heap sorting requires a large number of comparisons to build the initial heap, so the heap sorting is not suitable for sorting with fewer elements.

Exchange sorting

The basic idea of exchange sorting is to compare them by comparison, and then decide whether to switch the position based on the comparison results. The main exchange sorting methods include the Bubble Method and the quick sorting method.


Bubble Sorting

Just like its name, bubble means that if there are two bubbles, the weight is the same as the value of the element to be sorted, and each bubble moves the lightest element up (Before.

Take the 7 digits that have been disrupted as an example:


Quick sorting

The basis of quick sorting is the division and control method, which breaks down the problem into several subproblems with a small but similar structure as the original problem. Recursive solutions are provided to these subproblems, then, combine the sub-problems into the solution of the original problem.

Take the 7 digits that have been disrupted as an example. To make it clearer, we sort them in reverse order:

Note: After the benchmark is selected, the numbers on the left and the numbers on the right must be compared alternately. In this example, the benchmark is first compared with the number on the right, and then the first on the left, then compare it with the second number on the right ......

Select the benchmark from the first four elements and the last two elements, and then sort by the above steps:

Because the example is relatively simple, the sorting is completed twice. The time of quick sorting is mainly spent on partitioning operations, dividing the interval of K length, a total of K-1 times keyword comparison. It is the fastest internal Algorithm Based on keyword comparison.

Merge Sorting

Merge Sorting is an algorithm that uses merge operations to complete sorting. If two merge operations are used, this is called two Merge Sorting. The process is as follows:

  1. First, we regard each element in the range to be sorted as an ordered table and combine them to generate n/2 (rounded down) with a length of 2 (the length of the last table may be less than 2) this is also called a merge.
  2. Then, we merge n/2 (rounded down) ordered tables to generate an ordered table with a length of 4 than N/4 (rounded down;
  3. In this case, it is usually necessary to cyclically obtain an ordered table with the length of n. If the value is an odd number, it is log2n + 1 (rounded down ).

Take the 7 digits that have been disrupted as an example:

Merge Sorting is a stable sorting. You can use a sequential storage structure or a linked list. Merge Sorting requires an auxiliary amount to store the merge results of two ordered sub-files.

Base sort

The basic idea of this sorting method is slightly different from the above several sorting methods. How can we determine the size of a three-digit number based on the primary school instruction? There are hundreds of digits, hundreds of digits, and even dozens of digits. This method is based on this.

In this sorting method, it is not appropriate to use the "7 digits in disorder" above. Re-select the disordered number set as: {315,707,101,123,489,918,613}. Because it is a forward order, it is compared from a single position.

What is the use of single-digit sorting? Isn't there any association at all? At first, I also had this question, wondering why it was not a hundred-bit ranking, so it was more clear at a glance. Now let's think that even if we start to rank by a hundred bits, we can still put a few hundred bits into a single value. What's more, we don't know how many bits there are in a few unordered series. Similarly, the method of sorting by single digit is equivalent to dividing the same single digit into one group first. If the same single digit is sorted by ten digits, the sorted order must be followed.

 

Sort by hundred bits:

To reduce the number of element moves, queues can be stored and distributed in a chain. Each queue has two pointers pointing to the head and end of the queue respectively.

Although the idea of this method is easy to understand, it is because it is arranged from the beginning of a single position, which is in reverse order from the beginning of daily order, and how much thinking needs to be converted, you can sort data from a high position, and vice versa.

Comparison

The complexity and Stability of various sorting methods are as follows:

The sorting mentioned above is not just a method, but also an idea. For example, Merge Sorting: the value in your current circle is the highest. What if it is merged with other circles? I think the most important points of understanding these sorting algorithms are: the purpose of sorting, sorting objects, and unique ideas of each sort. If we can combine these algorithms into our daily lives, you cannot forget it.

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.