Objective: To grasp the classification of sorting algorithm and the comparison of different algorithm complexity, to make sure that xxx is independent of the initial state of the array is divided into several situations:
A, the algorithm complexity is independent of the initial state;
b, the total number of elements compared to the initial state independent;
C, the total number of elements moved is independent of the initial state.
1. Algorithm classification
There are two main categories of 10 common sorting algorithms:
Nonlinear time comparison class ordering : By comparison, the relative order between elements is determined, because its time complexity cannot break through O (Nlogn), so it is called nonlinear time comparison class ordering.
linear time Non-comparative class ordering : The relative order between elements is not determined by comparison, it can break through the lower bound of time based on the comparison sort and run in linear time, so it is called linear time non-comparison class ordering.
Internal sorting and external sorting:
In addition, all the sorting is done in memory , and here is the internal sort refers to the content to be sorted in memory can be done , when the file to be sorted very large, the computer memory can not accommodate the entire file, the file can not use the internal sort , while the external sort means that the content to be sorted cannot be completed in memory at once , it needs to do the internal and external content Exchange , and the sorting method used by the outer sort is the merge sort.
2. Complexity of the algorithm
Basic concepts:
Stable : If A is originally in front of B, and a=b, after sort a is still in front of B.
Instability: If A is originally in front of B, and A=b, then a may appear behind B.
time Complexity : The total number of operations on the sorted data. Reflects the regularity of the number of operations when n changes.
spatial complexity: refers to the measurement of the storage space required by the algorithm when it executes in a computer, and it is also a function of the data size n.
3. The algorithm complexity of the sorting method is independent of the initial state of the array
The algorithm complexity of the following four sorting methods can easily be drawn from the red line in the table regardless of the initial state of the array:
A bunch ( heap sort ) Turtle ( merge sort ) Select ( sort ) Base ( base sort ) Friends
Where the total number of elements compared to the initial state is independent of: Select Sort , base sort .
The total number of elements moved is independent of the initial state: merge sort , base sort .
Summary of JS sorting algorithm: Comparison of eight algorithms