Data structure: common sorting algorithm (3): binary insertion sort (C++ implementation)

Data structure: common sorting algorithm (3): binary insertion sort

Insertion sort:

1. Idea: At each step, a record to be sorted is inserted into the appropriate position of the previously sorted word sequence according to its sequence code size, until all insertions are sorted.
2. The key question: Find the appropriate insertion position in the previously sorted sequence.
Method: into direct insertion sort , binary insertion sort , Shell sort

Binary insertion sort

1. Basic idea:

Basic idea: The idea of ​​dichotomy insertion sort is the same as that of direct insertion, except that the way to find a suitable insertion position is different. Here is to find a suitable position by dichotomy, which can reduce the number of comparisons.

Binary insertion sort, referred to as binary sort, is when the i-th element is inserted, the previous 0~i-1 elements are halved, and the element in the middle is first compared. If it is smaller, the first half is halved, otherwise The second half is folded in half until left<right, and then all elements between the first position of the i-th element and the target position are moved back, and the i-th element is placed at the target position.

According to the idea of ​​binary search, the records to be checked are divided into an ordered list and an unordered list, and each time the first keyword of the unordered list is compared with the record in the middle of the ordered list. If it is less than the record, insert the element into the subsequent sub-table, otherwise, if it is greater than the record, insert the element into the previous sub-table. Iterate repeatedly until all records are inserted into the table.

2. Example

Sort the array a[6]={5,36,24,10,6,12} by binary insertion sort

img

Image source: https://www.cnblogs.com/zwtgyh/p/10631760.html

Code

#include<iostream>
using namespace std;
/*二分查找函数,返回插入下标*/
template <typename T>
int BinarySearch(T array[], int start, int end, T k)
{
	while (start <= end)
	{
		int middle = (start + end) / 2;
		int middleData = array[middle];
		if (middleData > k)
		{
			end = middle - 1;
		}
		else
			start = middle + 1;
	}
	return start;
}
//二叉查找插入排序
template <typename T>
void InsertSort(T array[], int length)
{
	if (array == nullptr || length < 0)
		return;
	int i, j;
	for (i = 1; i < length; i++)
	{
		if (array[i]<array[i - 1])
		{
			int temp = array[i];
			int insertIndex = BinarySearch(array, 0, i, array[i]);//使用二分查找在有序序列中进行查找,获取插入下标
			for (j = i - 1; j >= insertIndex; j--) //移动元素
			{
				array[j + 1] = array[j];
			}
			array[insertIndex] = temp;    //插入元素
		}
	}
}
//主函数
int main() {
	//int *a = Random();
	int a[6] = { 5, 36, 24, 10, 6, 12 };
	InsertSort(a, 6);
	for (int i = 0; i < 6; i++)
	{cout << a[i] << " ";}	
	cout << endl;
	return 0;
}

3. Summary:

1. Summary: Worst case: every time it is inserted at the beginning of the ordered sequence, the elements of the entire ordered sequence need to be moved back, and the time complexity is O(n2)

2. The best case: the array to be sorted is in positive order, and the position of each element is its insertion position. At this time, the time complexity is only the comparison. 3. The time complexity is O(log2n)

4. Average situation: O(n2)

In terms of space complexity, binary insertion is also sorted in-place, and the space complexity is (O(1)).

Guess you like

Origin blog.csdn.net/qq_43801020/article/details/108108187