CopyCode The Code is as follows: // insert sort directly
Void directinsertionsort (int * arr, int nlen)
{
Int I, J;
For (I = 1; I <nlen; I ++)
{
Int temp = arr [I];
For (j = I-1; j> = 0; j --)
{
If (temp <arr [J])
Arr [J + 1] = arr [J];
Else
Break;
}
If (J + 1! = I)
Arr [J + 1] = temp; // note that J + 1 is used.
}
}
// Semi-insert sorting
Void binaryinsertionsort (int * arr, int nlen)
{
Int I, J;
Int low, mid, high;
For (I = 1; I <nlen; I ++)
{
Int temp = arr [I];
If (temp <arr [I-1]) // This judgment can not
{
Low = 0;
High = I-1;
While (low <= high) // error: While (low {
Mid = (low + high)/2;
If (temp <arr [Mid])
High = mid-1;
Else
Low = Mid + 1;
}
// After the insert position is half-searched, the insert position is low or high + 1, and low is equal to high + 1.
// For (j = I-1; j> = high + 1; j --)
//{
// Arr [J + 1] = arr [J];
//}
// Arr [high + 1] = temp;
For (j = I-1; j> = low; j --)
{
Arr [J + 1] = arr [J];
}
Arr [low] = temp;
}
}
}