Insertion sort
The basic idea of insertion sort: Each pass inserts an element to be sorted into the appropriate position in the sorted area according to the size of its key value, until all the insertions are completed. The most fundamental difference between different insertion sorting algorithms is based on what rules to find the insertion point of the new element. Direct insertion sorting is to find in order, and Hill sorting reduces the increment and then uses direct insertion sorting.
Direct insertion sort
- The basic idea
Direct insertion sort is the simplest sorting method, which is to insert each element in the disordered area into an ordered area in turn. - Example
{4,5,1,2,8,6,7,3,10,9}
Take the first of the unordered interval, scan the ordered interval from right to left for comparison, the square brackets can be regarded as an ordered interval .
The first time: [4],5,1,2,8,6,7,3,10,9 The
second time: [4,5],1,2,8,6,7,3,10,9
The third time: [1,4,5],2,8,6,7,3,10,9 The
fourth time: [1,2,4,5],8,6,7,3,10,9
The fifth time: [1,2,4,5,8],6,7,3,10,9 The
sixth time: [1,2,4,5,6,8],7,3,10,9
Seventh time: [1,2,4,5,6,7,8],3,10,9
Eighth time: [1,2,3,4,5,6,7,8],10,9
Ninth time: [1,2,3,4,5,6,7,8,10],9
Tenth time: [1,2,3,4,5,6,7,8,9,10] - Algorithm analysis
The space complexity of the direct insertion sorting algorithm is O(1).
In the best case, the unordered sequence to be compared is originally in order, then the number of comparisons is n-1, the shift is 0 times, and the time complexity is O(n).
In the worst case, the disordered sequence to be compared is originally in reverse order, so the number of comparisons is (n+2)(n-1)/2, and the number of moves is (n+4)(n-1) /2, the time complexity is O(n²).
The average complexity of direct insertion sort is O(n²).
Direct insertion sort is stable.
1. Basic idea of selection and sorting:
Selective sorting is a simple and intuitive sorting algorithm. Its basic principle is as follows: for a given set of records, the smallest record is obtained after the first round of comparison, and then the position of the record is exchanged with the position of the first record; Then, a second comparison is performed on other records except the first record, and the smallest record is obtained and exchanged with the second position record; repeat the process until there is only one record to be compared.
2. Complexity analysis:
From the perspective of the simple selection and sorting process, its biggest feature is that the number of exchanges of mobile data is relatively small, which saves the corresponding time. Analyzing its time complexity, it is found that no matter it is the best and worst case, the comparison times are the same. The i-th sorting requires ni key comparisons. At this time , the comparison times are required . For the number of exchanges, when the most In good times, there are 0 exchanges . In the worst case, which is the initial fall, the number of exchanges is n-1 . Based on the final time sequence and the total number of exchanges, the total time complexity is still equal .
Although it is the same as bubble sort , the performance of simple selection sort is better than bubble sort.
3. The sorting process is as follows:
Take the array {49,38,65,97,76,13,27,49} as an example,