Comparison of common comparison sorting algorithms

Source: Internet
Author: User

    • Comparison of several common sorting algorithms

The basic concepts of sorting and the kinds of algorithms are introduced, and several algorithms for sorting algorithms are presented: bubble sort, select sort, insert sort, merge sort, quick sort, hill sort algorithm and analyze their respective complexity, then in tabular form, clearly and intuitively show the difference of their complexity. On the basis of studying several sort algorithms, this paper discusses a new sort algorithm, and through further exploration, finds the advantages and disadvantages of the new ranking algorithm compared with the previous algorithms.


Sorting algorithm is a common problem in computer programming. In the daily processing of data, we may have hundreds of thousands of requirements in the face of the complexity, so only when the data is properly sorted can it be more in line with the user's requirements. So in the past few decades, programmers have left us with several classic sorting algorithms, all of which are the crystallization of wisdom. This article will lead the reader to explore these interesting sorting algorithms, including some basic concepts of sorting algorithms and several common algorithms, analyze the time complexity of these algorithms, and at the end will introduce our original one sort method, for the reader reference evaluation.


    • Introduction and complexity analysis of several common algorithms

1. Basic Concepts

1.1 Stable sort (stable sort) and non-stable sorting

A stable sort is the relative order in which all equal numbers are retained before they are sorted by some sort method. Conversely, it is an unstable sort.

For example: A group of numbers before the sort is a1,a2,a3,a4,a5, where a2=a4, after some sort of a1,a2,a4,a3,a5,

Then we say that this sort is stable, because the A2 sort before the A4, and it is in front of the A4 after sorting. If it becomes a1,a4,a2,a3,a5, it is not stable.

1.2 In-order (internal sorting) and out-of-order (external sorting)

During the sorting process, all the numbers that need to be sorted are in memory, and the order in which they are stored is adjusted in memory, called inner ordering, in which only the partial number is called into memory, and the memory-adjusted number of storage-order ordering methods in the storage are referred to as the outer ordering.

Time complexity and spatial complexity of the 1.3 algorithm

The time complexity of the algorithm refers to the computational effort required to perform the algorithm. The spatial complexity of an algorithm generally refers to the memory space required to execute the algorithm.


2. Several common algorithms

2.1 Bubble sort (Bubblesort)

The bubble sort method is the simplest sort method. The basic idea of this approach is to think of the elements to be sorted as "bubbles" that are vertically arranged, smaller elements lighter and thus upward. In the bubble sorting algorithm we have to deal with this "bubble" sequence several times. The so-called process, is to check the sequence from the bottom up, and always pay attention to the sequence of two adjacent elements is correct. If the order of two adjacent elements is found to be incorrect, that is, the "light" elements are below, exchanging their positions. Obviously, after processing, the "lightest" element floats to its highest position, and after two times, the "light" element floats to the next high position. In the second pass, you do not have to check because the element at the highest position is already the lightest element. In general, when I pass the processing, I do not have to check the higher position above the elements, because after the previous i-1 the processing, they have been correctly sequenced.

The bubbling sort is stable . The time complexity of the algorithm is O (n ^2).

2.2 Selection sort (Selection sort)

The basic idea of choosing a sort is to treat a sequence of records to be processed n-1 times, and the first I-pass processing is to be l[i. N] The smallest person in the l[i] exchange position. Thus, after I pass the processing, the position of the first I record is already correct. Choosing a sort is not stable . The complexity of the algorithm is O (n ^2).

2.3 Insert Sort (insertion sort)

The basic idea of inserting a sort is that, after i-1-through, l[1..i-1] is in the right order. I-pass processing only l[i] into the appropriate position of l[1..i-1], so that l[1..i] is a sequence of orderly. To achieve this, we can use a sequential comparison method. First compare L[i] and l[i-1], if l[i-1]≤l[i], then L[1..I] has been ordered, the first time the processing is finished, otherwise exchange l[i] and l[i-1] position, continue to compare l[i-1] and l[i-2] until a certain position J (1≤j≤i-1) is found, Make l[j]≤l[j+1]. Figure 1 illustrates the process of inserting a sequence of 4 elements, which requires (a), (b), (c) three insertions. The direct insert sort is stable . Algorithm time complexity is O (n ^2)

2.4 Heap Sorting

Heap sorting is a sort of tree selection, in which the a[n] is considered as the sequential storage structure of a complete binary tree, and the smallest element is selected using the intrinsic relationship between the parent node and the child node in the complete binary tree. heap sequencing is not stable . Algorithm time complexity O (nlog N).

2.5 Merge Sort

With two sequential (ascending) sequences stored in adjacent positions in the same array, it may be set to a[l. M],a[m+1..h], merge them into an ordered sequence, and store them in A[l. H]. Its time complexity is O (nlog2n) in both the best and worst-case scenarios.

2.6 Quick Sort

A quick sort is an essential improvement to the bubbling sort . Its basic idea is that the length of the sequencing sequence can be drastically reduced after a scan. In a bubbling sort, a scan can only ensure that the number of maximum values is moved to the correct position, while the length of the sequence to be sorted may be reduced by only 1. Quick sort by a scan, you can make sure that the number of the left is smaller and the number on the right is larger than it. It then uses the same method to manipulate the left and right sides of the number until there is only one element to the left of the datum point. fast sequencing is not stable . The optimal condition algorithm time complexity O (nlog2n), the worst O (n ^2).

2.7 Hill Sort

In the direct insertion sorting algorithm, inserting one number at a time causes the ordered sequence to increment only 1 nodes, and does not provide any help for inserting the next number. If you compare the number of distant distances (called increments) so that the number moves across multiple elements, a comparison can eliminate multiple element exchanges. D.l.shell realized this idea in 1959 in a sort algorithm named after him. The algorithm first sorts the group of numbers by an increment d into groups, each group of records of the subscript difference D. Sort all the elements in each group, then use a smaller increment to do it, and then sort them in each group. When the increment is reduced to 1 o'clock, the entire number to be sorted is divided into a group, and the sort is completed. The hill sort is not stable . The time complexity of the algorithm is O (N2).


    • Comparison of time complexity of each sorting algorithm

Write down the time complexity of the following algorithms.

(1) bubble sort;

(2) Select sort;

(3) Insert sort;

(4) Quick sorting;

(5) heap sorting;

(6) Merge sort;

Answer:

The time complexity of the bubbling sorting algorithm is O (n^2).

The choice of sorting algorithm complexity is O (n^2).

Insertion sorting algorithm time complexity is O (n^2)

Quick sort quick sort is not stable. The optimal condition algorithm time complexity O (nlog2n), the worst O (n^2).

Heap Sorting algorithm time complexity O (NLOGN).

The time complexity of the merge sort is O (nlog2n).


Time complexity and spatial complexity of commonly used sorting algorithms

Ranking method worst-time analysis Average time complexity degree of stability spatial complexity

Bubble sort O (n2) O (n2) stabilized O (1)

Quick sort O (n2) O (n*log2n) unstable O (log2n) ~o (n)

Select Sort O (n2) O (n2) stabilized O (1)

Binary tree sort o (N2 ) O (n*log2n) with top o (n)

Insert sort O (n2) O (n2) stabilized O (1)

Heap sort O (n*log2n)O (n*log2n) unstable O (1)

Hill sort o o unstable O (1)

1. Complexity of Time

(1) Time frequency an algorithm implementation of the time spent, from the theoretical can not be calculated, must be on the machine to run the test to know. But we can not and do not need to test each algorithm, just know which algorithm spends more time, which algorithm spends less time on it. And the time that an algorithm spends is proportional to the number of executions of the statement in the algorithm, which algorithm takes more time than the number of statements executed. The number of times a statement is executed in an algorithm is called a statement frequency or time frequency. Note as T (N).

(2) Time complexity in the time frequency mentioned earlier, n is called the scale of the problem, and when N is constantly changing, the time frequency t (n) will change constantly. But sometimes we want to know what the pattern is when it changes. To do this, we introduce the concept of time complexity. Under normal circumstances, the number of iterations of the basic operation of the algorithm is a function of the problem size n, denoted by T (n), if there is an auxiliary function f (n), so that when n approaches infinity, the limit value of T (n)/f (n) is not equal to zero constant, then f (n) is the same order of magnitude function of t As T (n) =o (f (n)), called O (f (n)) is the progressive time complexity of the algorithm, which is referred to as the complexity of time.

In various algorithms, if the algorithm is a constant number of execution times, the time complexity is O (1), in addition, the time frequency is not the same, the time complexity may be the same, such as T (n) =n2+3n+4 and T (n) =4n2+2n+1 their frequency is different, but the time complexity of the same, all O ( N2). In order of magnitude increment, the common time complexity is: Constant order O (1), Logarithmic order O (log2n), linear order O (n), linear logarithmic order O (nlog2n), square order O (n2), Cubic O (n3),..., K-order O (NK), exponent-order (2n). With the increasing of the problem scale N, the complexity of the time is increasing and the efficiency of the algorithm is less. 2, spatial complexity is similar to the time complexity, the spatial complexity is the measurement of the storage space required when the algorithm executes in the computer. Note: S (n) =o (f (n)) We are generally talking about the size of the secondary storage unit in addition to the normal memory overhead. The discussion method is similar to the complexity of time, and is not discussed.

(3) The time performance of progressive time complexity evaluation algorithm is mainly to evaluate the time performance of an algorithm by the order of magnitude of time complexity (i.e., the asymptotic time complexity of the algorithm).

2. Similar to the discussion of time complexity, an algorithm's spatial complexity (space complexity) S (n) is defined as the storage space consumed by the algorithm, and it is also a function of the problem size n. Asymptotic spatial complexity is also often referred to as spatial complexity.

Spatial complexity (space complexity) is a measure of the amount of storage space that is temporarily occupied by an algorithm while it is running. The storage space occupied by an algorithm in the computer memory, including the storage space occupied by the storage algorithm itself, the storage space occupied by the input and output data of the algorithm and the storage space occupied by the algorithm in the running process three aspects. The storage space occupied by the input and output data of the algorithm is determined by the problem to be solved, which is passed by the calling function by the parameter table, and it does not change with the algorithm. Storage algorithm itself occupies the storage space and the length of the algorithm written in proportion, to compress the storage space, you must write a shorter algorithm. Algorithm in the running process of temporary occupied storage space varies with the algorithm, some algorithms only need to occupy a small amount of temporary work units, and does not vary with the size of the problem, we call this algorithm "in-place/", is to save the memory of the algorithm, as described in this section of the algorithm is so , some algorithms need to occupy the number of temporary work and solve the problem of the size of N, it increases with the increase of N, when n is large, will occupy more storage units, such as in the Nineth chapter described in the Quick Sort and merge sorting algorithm is the case.

If the spatial complexity of an algorithm is a constant, that is, it can be represented as O (1) when it is not changed by the size of N of the processed data, and when the spatial complexity of an algorithm is proportional to the logarithm of the base N of 2, it can be represented as 0 (10g2n), and when an algorithm's empty I-division complexity is linearly proportional to N, can be represented as 0 (n). If the parameter is an array, it is only necessary to allocate a space for it to store an address pointer transmitted by the argument, that is, a machine word space, and if the formal parameter is a reference, it is only necessary to allocate a space for it to store the address of the corresponding argument variable. To automatically reference the argument variable by the system.


This article from the "Learning Record" blog, reproduced please contact the author!

Comparison of common comparison 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.