Binary insertion sorting
A brief description of algorithm ideas:
When the I-th element is inserted ~ I-1 elements to half, first with them
If the element ratio in the middle is small, the first half will be folded. Otherwise, the second half will be
Until left> right, and then between the first position of the I-th element and the target position
And then place the I-th element on the target position.
There is no sorting in the binary method, and there is only a search. So when you find the location to insert. Moving must start from the last record, move one digit backward, and then move the last 2nd digits until the record at the position to be inserted moves the last one.
Binary insertion sorting is stable, and the average time is O (n2)
Void binsort (ref int [] data1)
1. Binary Search Insert Location
If R [I] <R [m] is true, the right pointer must move one middle pointer to the left. Otherwise, the left pointer must move one middle pointer to the left. Repeat the search until the left pointer is greater than the right pointer.
2. It's confusing to move back. When do I need to move back? What records need to be moved?
Although we clearly know that we need to move the records whose sorting code is greater than R [I], we will inevitably ask ourselves a few questions. In fact it is equivalent to a record that needs to move from the I-1 to the left pointer.
3. insert
The left pointer obtained from 1 is actually the position of the element to be inserted.
4. Algorithms
{
Int left, right, num;
Int middle, J;
For (INT I = 1; I <data1.length; I ++)
{
// Prepare
Left = 0;
Right = I-1;
Num = data1 [I];
// Binary Search Insert Location
While (Right> = left)
{
// Point to the sorted Center
Middle = (left + right)/2;
If (Num <data1 [Middle])
// The inserted element is in the right interval
Right = middle-1;
Else
// The inserted element is in the left interval.
Left = middle + 1;
}
// Record with a descending order code greater than R [I]
For (j = I-1; j> = left; j --)
{
Data1 [J + 1] = data1 [J];
}
// Insert
Data1 [left] = num;
}
// The inserted element is in the left interval.
Left = middle + 1;
}
// Record with a descending order code greater than R [I]
For (j = I-1; j> = left; j --)
{
Data1 [J + 1] = data1 [J];
}
// Insert
Data1 [left] = num;
}
/* Binary insert sort algorithm source program */
# Include <stdio. h>
# Define maxnum100
Typedef int keytype;
Typedef int datatype;
Typedef struct {
Keytype key;/* sort the code segment */
/* Datatype Info; other fields of the record */
} Recordnode;
Typedef struct {
Int N;/* n indicates the number of records in the file. n <maxnum */
Recordnode record [maxnum];
} Sortobject;
Void binsort (sortobject * pvector) {/* sort binary insertion in ascending order */
Int I, j, left, mid, right;
Recordnode temp;
Recordnode * Data = pvector-> record;
For (I = 1; I <pvector-> N; I ++ ){
Temp = data [I];
Left = 0; Right = I-1;/* set the upper and lower threshold values of the sorted range */
While (left <= right ){
Mid = (left + right)/2;/* mid points to the middle position of the sorted interval */
If (temp. Key <data [Mid]. Key)
Right = mid-1;/* The inserted element should be in the left subinterval */
Else left = Mid + 1;/* The inserted element should be in the right subinterval */
}
For (j = I-1; j> = left; j --)
Data [J + 1] = data [J];/* move the record whose sorting code is greater than Ki behind */
If (left! = I) data [left] = temp;
}
}
Sortobject vector = {10, 50,101 };
Int main (){
Int I;
Binsort (& Vector );
For (I = 0; I <vector. N; I ++)
Printf ("% d", vector. Record [I]);
Getchar ();
Return 0;
}