Insertion Sort - Direct Insertion Sort, Binary Insertion Sort, Hill Sort

Selection sort is a relatively simple sorting algorithm. Its implementation principle is to select the smallest (largest) element from the data elements to be sorted each time, and store it at the start (end) position of the data until all the data to be sorted is finished.

Direct insertion sort:

When inserting the i-th element, we consider the i elements preceding it to be in order.

At this time, use the sorting of array[i] to compare it with the previous sorting code, and insert it when a suitable position is found, and the elements in the original position are moved backward in order.

 

 

Time Complexity : Worst: Contrary to the desired sequence O(n^2)

                         Optimal: same as required series O(n)

Space Complexity : O(1)

Stability : Unstable

code show as below:

void InsertSort(int *array, int size)
{
	for (int i = 0; i<size; i++)
	{
		int key = array[i];
		int end = i-1;
		while (end >= 0 && key < array[end])   // key <= array[end]不稳定
		{
			array[end+1] = array[end];
			end--;
		}
		array[end+1] = key;
	}
}

Binary insertion sort:

In insertion sort, if the cost of the comparison operation is greater than the cost of data exchange , the role of binary search is obvious at this time. With binary search, we can reduce the number of comparison operations.

Time Complexity : Worst: O(N^2)

                       Optimal: n O(lgN)

Space Complexity : O(1)

Stability: stable

void binarysort(int *array, int size)
{
	for (int i = 0; i < size; i++)
	{
		int left = array[0];
		int right = array[size - 1];
		int mid = 0;
		int key = array[i];
		while (left< right)
		{
			mid = left + (left + right) / 2;
			if (key > array[mid])
				left = mid+1;
			else
				right = mid+1;
		}
		for (int j = i - i; i >= left; j--)
		{
			array[j+1] = array[j];
			array[left] = key;
		}
	}
}

When the amount of data is large, binary insertion sort can greatly reduce the number of data comparisons. But when the data is close to sorted, direct insertion sort is better than binary insertion sort.

Hill sort:

Hill sorting, also known as "shrinking incremental sorting", is a more efficient and improved version of the direct insertion sorting algorithm. Group all the data according to a certain increment, and use direct insertion sort for each group. As the increment decreases, each group contains more and more keywords. When the increment is reduced to 1, the sorting ends.

Compared with direct insertion sorting, Hill sorting is more efficient: direct insertion sorting can only move one data at a time, while Hill sorting can move incremental data each time. The time complexity of sorting is greatly improved.

void shellsort(int *array, int size)
{
 int  gap = size;
 while (gap>1)
   {
    for (int i = 0; i<=size; i++)
     {
      gap = gap / 3 + 1;
      int key = array[i];
      int end = i + gap;
      while (end <= size-1 && key > array[end])
       {
         swap(key, array[end]);
         end = end + gap;
       }
      break;
     }
   }
}




 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325888536&siteId=291194637