Summary of internal sorting algorithms

Source: Internet
Author: User

The internal sorting algorithm mainly divides into the order of inserting class, the sort of exchange class and the sort of choice class, and their performance difference is mainly embodied in time complexity, space complexity and stability. Various sorting algorithms will be compared and moved between elements, the time complexity is mainly determined by the number of comparisons and the number of moves in the whole sorting process. Space complexity now takes up as much space in the sorting process as it does in the sort record itself.

1. Insert Class Sort

Direct Insert Sort

If the records to be sorted are arranged sequentially, the number of elements in the whole sorting process is n-1 and the number of moves is 0 times. If the records to be sorted are in reverse order, the number of times I is sorted is I, and the frequency of movement is i+1, where I is 2 to n. Therefore, the total number of comparisons for an element during the sorting process is (n+2) (n-1)/2, and the number of moves is (n+4) (n-1)/2.

The time complexity of the direct insertion sorting algorithm is O (n^2), and the spatial complexity is O (1). From the implementation of the direct insertion sorting algorithm, it can be found that the post-position records are not likely to be inserted before the same size records, so the direct insert sorting algorithm is stable.

Binary Insert Sort

binary insertion sorting algorithm compared with the direct insertion sorting algorithm, the number of comparisons per trip is log2^i, so the overall comparison is O (nlog2^n). However, the binary algorithm has not changed the number of moves compared with the direct insertion algorithm.

The time complexity of the binary algorithm is still O (n^2) and the space complexity is O (1). It can be seen from the algorithm implementation that the binary insertion sorting algorithm is stable.

Hill sort

The hill sort uses the feature of the direct insert algorithm to improve the performance when the "order n smaller, sequence is basically ordered" is used. The time complexity of the hill sorting algorithm is O (n^1.5) and the spatial complexity is O (1), which is an unstable sorting algorithm. A counter example is as follows: The sequence to be sorted is {2,4,1, (2)}, set to delta[1]=2, then a trip to the sorted sequence is {1, (2), 2,4}.

2. Sorting of Exchange classes

Bubble sort

When sorting records are sorted in reverse order, each bubbling algorithm makes an i comparison and a 3i exchange (two element exchanges), the range of I is 1 to n-1. So the total number of comparisons is n (n-1)/2, the total number of moves is 3n (n-1)/2. The time complexity of the bubble sorting algorithm is O (n^2), and the spatial Complexity O is (1), which is a stable sorting algorithm.

Quick Sort

The best case for a quick sort is to divide the records to be sorted by two each time, at which time the complexity is O (nlog2^n). In the worst case, the records to be sorted are already sequenced, and the total number of comparisons is n (n-1)/2, at which time the complexity is O (n^2).

The average complexity of fast sorting is O (nlog2^n) and the space complexity is O (log2^n), which is an unstable sorting algorithm. The following examples are: {3, 2, (2)} for the sorted sequence, followed by {(2), 2,3} after a trip.

3. Select Class Sort

Simple selection sorting

Regardless of the initial arrangement of the records to be sorted, the simple selection of the total number of comparisons is always n (n-1)/2, because the number of times it compares per trip is n-i,i from 1 to n-1. In the best case, the simple selection of the sorting algorithm moves at 0, the worst case movement of 3 (n-1).

The time complexity of the simple selection sorting algorithm is O (n^2), and the spatial complexity is O (1), which is an unstable sorting algorithm. The following examples are as follows: the records to be sorted are {6, 8, (6), 2,7}, a trip is recorded as {2, 8, (6), 6,7}, two times sorted after {2, (6), 8,6,7}.

Heap Sort

The time complexity of heap sequencing is O (nlog2^n) and the spatial complexity is O (1), which is an unstable sorting algorithm.

4. Merge sort

The time complexity of merge sort is O (nlog2^n), and the spatial complexity is O (n), which is a stable sorting algorithm.

5. Summary

In summary, the time complexity, spatial complexity, and stability of the internal sequencing algorithm are summarized in the following table:

1. Direct Insert Sort, binary insert sort, bubble sort, and simple select sort are called simple sorts, their time complexity is O (n^2), and the spatial complexity is O (1). For stability, only simple selection of sorting is an unstable sorting algorithm. Simple sorting is only suitable for n smaller cases.

2. The direct insert sort is the best sorting algorithm when the sorted records are basically ordered. It is often used in conjunction with quick sort and merge sorting, because these algorithms divide the entire sorted record into sub-records, which are small and basic in order.

3. The sorting performance is better, such as Hill sort, quick sort, heap sort and merge sort, except the time complexity of hill sort is O (n^1.5), the other sort algorithm is O (NLOGN). While fast sorting worst-case time performance degrades to O (n^2), the fast sorting algorithm is best in terms of average performance. The performance of heap sort and merge sort is stable, when n is large, the performance of merge sort is better than heap sort, but it needs the auxiliary space of O (n).

4. In all sorting algorithms, the unstable algorithm has simple selection sort, hill sort, quick sort and heap sort. In the ranking algorithm with better performance, only the merge sort is stable. Sorting is done by the primary key of the record, without regard to the stability of the sorting method. If the sorting is done by the secondary keyword of the record, the stability of the sorting method should be taken into account.

Summary of internal sorting algorithms

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.