[As Functional code Tutorial 10] Data structure sorting algorithm

Source: Internet
Author: User

First, Introduction

For data processing, sorting is one of the most basic operations. In today's computer systems, the time spent on sorting accounts for a large proportion of the system's CPU running time. Data indicate that on some commercial computers, the CPU time on the sort is 20% to 60%. In order to improve the efficiency of the computer, a variety of sorting methods and algorithms are proposed. These algorithms have developed and fully demonstrated some important principles and superb techniques of algorithm design. Therefore, it is very important for computational professionals to master the sorting algorithm.

The introduction of the sorting algorithm

In this course we will introduce six sorts of sorting methods: Insert Sort, select sort, bubble sort, hill sort, quick sort and two-way merge.

<1> Direct Selection sort (Selection sort): Simple selection sort, it compares the number of times must be: N (n-1)/2. Therefore, no matter in the sequence of what the case, it will not have excellent performance (from the 100K positive sequence and the reverse sequence data can be found that it time-consuming difference is not much, the difference is only the data movement time), visible to the order of the data is not sensitive. Although it compares more times, its data exchange quantity is very few. So we'll find that it will generally be faster than bubble sort.

<2> Direct Insert sort (insertion sort): A simple insertion sort that removes a maximum order after each comparison, and therefore is as efficient as bubbling. But it's still a high speed, because in a bubble sort, it's a value exchange, and a value move under the Insert sort, so the direct insertion sort will be better than the bubble sort. The direct insertion method is also a kind of algorithm which is very sensitive to the order of data. In ordered cases, only n-1 comparisons are required, and in the worst case, n (n-1)/2 comparisons will be required.

<3> Bubble sort (Bubble sort): Compares adjacent two data elements by keyword, and if reversed, swaps. For a sequence of data elements to be sorted, the maximum data element is moved to the maximum position after a trip, and other data elements with larger values move to the final position, which is a foaming process. Then repeat the procedure for the following records until there are no exchanges in the procedure, and then sort the records.

<4> Quick Sort: a refinement of the bubble sort that eliminates multiple reverse orders in one swap, reducing the number of scans and data exchanges that are consumed in reverse order. In the optimal case, its order time complexity is O (nlog2n). That is, each time the sequence is divided, it can be evenly divided into two substrings. But at worst, its time complexity will be O (n^2). That is, each time the substring is divided, one string is empty, and the other is m-1 (the 100K positive sequence and reverse order in the program is exactly the same, if the program uses the central data of each fetch sequence as the dividing point, it will be in the positive sequence and inverse time to achieve the optimal). The "quick sort" is slower than "bubble sort" from the results of the 100K positive sequence, which is primarily a method of early-ending sorting in bubble sort. In some books this explains "quick sort," which, in theory, would be the fastest sort algorithm if it could divide the sequence evenly each time, and therefore called it a fast sort. Although it is difficult to evenly divide the sequence, it is still the fastest in the internal sorting algorithm based on keyword comparisons, in terms of average performance.

<5> Hill Sort (Shell sort): Incremental choices will affect the efficiency of hill sorting. But no matter how the increment is chosen, finally make sure that the increment is 1 to make a direct insert sort. However, it is relative to the direct insertion sort, and as a result of each comparison in the child table, it is possible to remove multiple reverse orders from the entire table, thus improving the overall sorting performance. Hill sort is an algorithm based on insert ordering, so it is sensitive to data order.

<6> Merge Sort: Merge sort is a non-local sort that will require as much auxiliary space as the sequence to be sorted. It will have incomparable advantages in using it to merge two ordered sequences of its own. Its time complexity is O (nlog2n) in either the best of cases or at worst. Not sensitive to the order of the data. If the data node data volume is large, that will not be suitable. But can be changed to create index operations, the effect will be very good.

The choice of sorting method

Because different sorting methods adapt to different application environments and requirements, it is important to select the appropriate sorting method.

(1) If n is small, direct insertion or direct selection can be used.

When the record scale is small, the direct insertion sort is better, it will be less than the choice of comparison times;

However, when the record size is large, because the direct selection of the number of mobile records is less than the direct insertion, it is appropriate to select a direct selection of sorting.

Both of these are stable sorting algorithms.

(2) If the initial state of the document in a basic order (correction), you should choose the direct insertion, bubble or random fast sorting appropriate (here is the random reference value of the random, the reason for the rapid sorting analysis); Here the quick sort algorithm will be unstable.

(3) If n is large, then the time complexity of O (nlog2n) Sorting method should be used: quick sort, heap sort or merge sort order.

Fast sorting is considered as the best method at present based on the comparison, when the keyword to be sorted is randomly distributed, the average time of fast ordering is shortest;

Heap sorting does not appear to be the worst-case scenario that can occur with a quick sort. But it needs to build a heap of process. Both of these sorts are unstable.

Merge sort is a stable sort algorithm, but it has a certain amount of data movement, so we may be in combination with the insertion sequence, first get a certain length of the series, and then merge, in the efficiency will be improved.

(4) Special box sorting, Cardinal order

All of them are a stable sort algorithm, but they have some limitations:

1> keywords can be decomposed.

2> records have fewer key digits, if dense is better

3> if it is a number, it is best to unsigned, otherwise you will increase the corresponding mapping complexity, you can first separate their positive and negative sorting.

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.