Direct insertion Sorting is one of the simplest sorting methods, and its basic operation is to insert a record into an ordered table that has been sorted,//To get a new ordered table with a 1 increase in the number of records. The front elements of the current element are all ordered, to be inserted, from the left side of the current element to look forward (from the back to the next),//The element larger than the current element is moved to the right one position, and finally put the current element where it should be on the line. Complexity: O (n^2)--the insertion sort is not suitable for a large amount of data sort application # include <iostream>using namespace Std;template<typename datatype> void output (DataType a[], int n) {for (int i = 0; i < n; i++) cout << a[i] << ""; cout << Endl;} Template<typename datatype>void Insertsort (DataType a[], int n) {DataType key; Int J; int scancnt = 0, swapcnt = 0; for (int i = 1; i < n; i++) {key = a[i];//the array element value corresponding to the current I label---current element j = i-1;//The element designator of the current I, J while (J &G t;= 0 && A[j] > key)//If J is in the Legal range (0~N-1) The value of the element referred to is greater than key {a[j+1] = a[j];//put the element J refers to the j+1 of the element j-- ; Move J Forward, if the current element is longer than the previous small, then J has been changed to 0} a[j + 1] = key;//Set the value of the j+1 element is key output (A, n); }}int Main () {int x[] = {7, 4, 6, 8, 3, 5, 1, 9,-2, 0};cout << "initial state:" << sizeof (x)/sizeof (int) << E Ndl;output (x, sizeof (x)/sizeof (int));insertsort<int> (x, sizeof (x)/sizeof (int)); return 0;}
C + + Insert Sort Demo program