⒈ starting with the first element, the element can be thought to have been sorted
⒉ takes the next element and scans the sequence of elements that have been sorted from backward forward
⒊ if the element (sorted) is greater than the new element, move the element to the next position
⒋ Repeat step 3 until the sorted element is found to be less than or equal to the position of the new element
⒌ inserting new elements into the next position
⒍ Repeat steps 2~5
#include <iostream>
#include <array>
using namespace Std;
Template<class t>
void Insertion_sort (t&, int);
int main ()
{
Array<int, 10> arr={1,3,2,5,4,6,8,7,9,0};
Insertion_sort (arr, arr.size ()); Input arrays and array sizes, because array is a class, so we cannot take arr as the array header, we refer directly to the array class
for (int i = 0; i <; i++)
{
cout << Arr[i] << Endl;
}
Cin.get ();
return 0;
}
Template<class t>
void Insertion_sort (t& arr, int count)
{
int p = 0;
for (int i = 1; i < count; i++)
{
Auto number = Arr[i];
for (p = i; p > 0 && number < arr[p-1]; p--)
{
ARR[P] = arr[p-1];
}
ARR[P] = number;
}
}
Inserting a sort C + + implementation