Table of contents
2. The idea of insertion sort
2.2.1 The idea of single-pass sorting
2.2.2 Single-pass sorting code implementation
4. Insertion sort + print test
1. Common sorting algorithms
2. The idea of insertion sort
2.1 Basic idea
Direct insertion sorting is a simple insertion sorting method. Its basic idea is:
insert the records to be sorted into a sorted sequence one by one according to the size of their key values until all records are inserted. , to get a new ordered sequence.
In practice, when we play poker, we use the idea of insertion sort:
2.2 Direct insertion sort
When inserting the i-th (i>=1) element, the previous array[0], array[1],...,array[i-1] have been sorted, and at this time use the sort code of array[i] and Array[i-1], array[i-2], ... compare the order of the sort codes, find the insertion position and insert array[i], and the order of the elements at the original position is shifted backward.
2.2.1 The idea of single-pass sorting
We mark the bottom of the last element in the array as end, each time the number to be inserted is first placed at the end+1 position, and then a temporary variable tmp is used to save the number, and the comparison starts from end and goes forward. If tmp is smaller than end, let end move back one bit, and then compare tmp with end-1, end-2... When tmp is greater than the element at a certain position, insert tmp into the position subscript + 1 position, the single-pass sorting is completed.
2.2.2 Single-pass sorting code implementation
int tmp = a[end + 1];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + 1] = a[end];
end--;
}
else
{
break;
}
}
a[end + 1] = tmp;
There is a clever way of writing in our code, when a[end] < tmp, jump out of the loop, and then insert tmp into a[end+1] position. This can solve the insertion of a[end] < tmp and the first element <tmp at the same time.
3. Insert sort code
Insertion sorting is to sort the entire array once on the basis of single-pass sorting. We add a layer of loop to the single-pass sorting code to achieve insertion sorting.
for (int i = 1; i < n; i++)
{
int end = i - 1;
int tmp = a[i];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + 1] = a[end];
end--;
}
else
{
break;
}
}
a[end + 1] = tmp;
}
4. Insertion sort + print test
void Print(int* a, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
void InsertSort(int* a, int n)
{
for (int i = 1; i < n; i++)
{
int end = i - 1;
int tmp = a[i];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + 1] = a[end];
end--;
}
else
{
break;
}
}
a[end + 1] = tmp;
}
}
void TestInsert()
{
int a[] = { 9,8,7,6,5,4,3,2,1 };
InsertSort(&a, sizeof(a) / sizeof(int));
Print(&a, sizeof(a) / sizeof(int));
}
int main()
{
TestInsert();
return 0;
}
Show results:
5. Time complexity of insertion sort
5.1 Worst case
The worst case is the reverse order, which is the worst case, and the time complexity is: O(N^2) .
5.2 Best case
The best situation is that the order is in order, we don't need to adjust, we just need to traverse the array once, time complexity: O(N) .
6. Summary of the characteristics of direct insertion sort
1. The closer the element set is to order, the higher the time efficiency of the direct insertion sorting algorithm
2. Time complexity: O(N^2)
3. Space complexity: O(1), it is a stable sorting
algorithm4 . Stability: Stable