Binary insertion sorting

Source: Internet
Author: User

Binary insertion sorting

A brief description of the algorithm concept:
When the I-th element is inserted ~ I-1 elements to half, first with them
The element ratio in the middle. If it is small, the first half is folded. Otherwise, the second half is
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 bipartite method, and only search is supported. 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 will move the Middle pointer to the left. Otherwise, the left pointer will move the Middle pointer to the left. Search repeatedly 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 back the records whose sorting code is greater than R [I], we will inevitably ask ourselves a few questions. In fact it is equivalent to the 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.