Insert sorting (-) directly insert sorting directly into the eight sorting algorithms of PHP:
Insert sorting is one of the simplest sorting algorithms. for sequences with N elements, insert sorting is composed of N-1 sort. It works by constructing an ordered sequence. for unordered data, it scans the sorted sequence from the back to the front and finds the corresponding position for insertion.
Steps for inserting a sorting algorithm:
-
Consider the first element of the first sequence to be sorted as an ordered sequence, and treat the second element to the last element as an unordered sequence.
-
Scan unordered sequences from start to end and insert each scanned element into an appropriate position of the ordered sequence (here you need to pay attention to a problem, if there is an element in the ordered sequence that is equal to the element to be inserted, the element to be inserted will be located after this element. in this way, the insertion sorting is stable. If it is inserted before this element, Insertion sorting in this way is unstable.
For the above steps, we can summarize the Insertion sorting algorithm as follows:
In the L-level sorting, move the element on position L to the right position of the element in front of the L + 1, where the first L element is ordered.
Insert sorting algorithm analysis:
Since each nested loop consumes N iterations, the time complexity of Insertion sorting is O (N ^ 2 );
The insertion sorting described above is the direct insertion sorting in the Insertion sorting algorithm, and the insert position in the ordered sequence is searched one by one. Therefore, the insertion sorting is called directly.
Welcome to our follow-up sorting algorithm. personal projects include: Capital companies (www.ya-jing.cn)