The classic Sort algorithm PHP implementation tutorial

Source: Internet
Author: User

The so-called ordering is to make a series of records, according to the size of one or some of the keywords, increment or decrement the arrangement of operations. The sorting algorithm is how to make the records arranged according to the requirements of the method. Sorting algorithms are given considerable attention in many areas, especially in the processing of large amounts of data. A good algorithm can save a lot of resources. Considering the various limitations and specifications of data in various fields, it is a great deal of reasoning and analysis to get a good algorithm that accords with the actual.

The complexity of the algorithm is divided into time complexity and space complexity. Its function: Time complexity refers to the computational effort required to execute the algorithm, while space complexity refers to the memory space required to execute the algorithm.

Time Frequency

The time it takes for an algorithm to execute is theoretically impossible to figure out and must be tested on the machine. 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 each algorithm spends is proportional to the number of executions of the statement in the algorithm, which algorithm takes more time to execute than the number of statements executed. The number of execution times of a statement in an algorithm is called the frequency of the statement or the frequency of time, as T (N).

Complexity of Time

In time frequency, n is called the scale of the problem, and when N is constantly changing, the time frequency t (n) will also change constantly. But sometimes we want to know what the law is when it changes, so we introduce the concept of time complexity. In general, the number of times the basic operation is repeated in 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 a constant that is not equal to zero, then f (n) is the same order of magnitude function of T (N). 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 the calculation of time complexity, first find the basic operation of the algorithm, and then according to the corresponding statements to determine its execution times, and then find T (n) The same order of magnitude (its same order of magnitude has the following: 1,LOG2N, N, nlog2n, n2,n3,2n,n!), after finding, f (n) = The order of magnitude, if T (n)/f (n) the limit can be obtained by a constant C, then the time complexity t (n) = O (f (n)).

complexity of space

the spatial complexity of a program is the amount of memory required to run a program. With the spatial complexity of the program, you can have a pre-estimate of how much memory is needed to run the program. In addition to requiring storage space and storing the instructions, constants, variables, and input data used by the store itself, a program needs some working units to manipulate the data and a secondary space to store some of the information needed for realistic computing. The storage space required for program execution consists of the following two parts:

a), fixed part. The size of this part of the space is independent of the number of input/output data. It mainly includes space occupied by instruction space (i.e. code space), data space (constants, simple variables), etc. This part belongs to the static space. b), variable space. This part of the space mainly includes the dynamic allocation of space, as well as the space required for the recursive stack. The spatial size of this part is related to the algorithm. The storage space required for an algorithm is expressed in F (N). S (n) = O (f (n)) where n is the size of the problem, S (n) represents spatial complexity.

Classification

Sorting algorithms can be divided into internal and external sorting, the internal sorting is the data records in memory to sort, while the external sort refers to the order during the sorting of all objects too much, can not be stored in memory at the same time, must be based on the requirements of the ordering process, continuously, external memory between the movement of the sort. Common internal sorting algorithms are: Insert sort, hill sort, select sort, bubble sort, merge sort, quick sort, heap sort, cardinality sort, etc. Summarize with a picture:

The classic Sort algorithm PHP implementation tutorial

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.