[C ++] - Hill complex sorting and sorting merge sort complete code [+] illustrates

1, Hill Sort: named after its inventor Donald Shell, Shell sort is an improved version of insertion sort

Sequencing method: First, the large data set into several groups, respectively, insertion sort, this time, the smaller insert acts sorted data ratio, the more efficient is inserted

Illustration: Array example: 57831246 (FIG invasion deleted)

(1) first calculated gap = n / 2 = 4, we mean that the array is divided into four groups, the group number where the beacon interval 4, as shown below:
Here Insert Picture Description
(2) each of these four groups the number of insertion sort, the entire group is only partially ordered at this time, as shown in FIG arr:
Here Insert Picture Description
(3) the updated value of the gap again, gap = gap / 2, the second question in the present value of the calculated gap 2, also is divided into two groups, the group numbers within the subject at intervals of 2, as shown below:
Here Insert Picture Description
(4) at this time can be seen, has a generally orderly, but not so obvious, is updated again gap gap = gap / 2 calculating 1 in this problem too, the whole re-insertion sort, the sort is complete.
Here Insert Picture Description
(Test code and in the back)

2, merge sort

The basic idea: Suppose a set of a total of n elements, a layer is divided into two large groups of n / 2 elements; the second layer is divided into four groups, each n / 4 th element, the third layer is divided into eight groups, each group n / 8 ... elements up until only one element each , as shown (FIG invasion deleted) FIG:
! [Insert Picture description here] (https://img-blog.csdnimg.cn/20200329211024316.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1ZpY2t5X0Ny,size_16,color_FFFFFF,t_
will sort the merged group after the completion points, inside each group are sorted into one large turn group, large group again to sort the merger, the greater the synthetic group ... until a synthesis group

Here Insert Picture Description
We set to two lengths of Example 4:
Here Insert Picture Description
(1) First define three auxiliary pointer p, p1, p2 result of creating a large storage array merge. FIG follows:
Here Insert Picture Description
(2) Since a <2, 1 into the new array, p1, and a p moved rearward, as shown below:
Here Insert Picture Description
(3) Since 2 <3, 2 into the new array, p2, and p a backward movement position
Here Insert Picture Description
(4) 3 <7,3 into the new array, p1, and a rearward movement p. FIG follows:
Here Insert Picture Description
(5) 5 <7,5 into the new array, p1, and a rearward movement p. FIG follows:
Here Insert Picture Description
(6) 6 <7,6 into the new array, p1, and a rearward movement p. FIG follows:
Here Insert Picture Description
(7) has come to the end of this time p1, as well as from a set of the remaining elements, the remaining elements of the sequence into the tail of a new array of
Here Insert Picture Description
the complete code Hill and merge sort is as follows:

#include <iostream>
#include <stdlib.h>
#include <stack>
#include <algorithm>
#include <assert.h>
using namespace std;

void QuickSort(int* a, int begin, int end)
{
	if (begin < end)
	{
		int left = begin;
		int right = end-1;
		int temp = a[left];
		while (left != right)
		{
			while (left < right&&a[right] >= temp)
				right--;
			if (a[right] < temp)
			{
				swap(a[right], a[left]);
			}
	
			while (left < right&&a[left] <= temp)
				left++;
			if (a[left] > temp)
			{
				swap(a[left], a[right]);
			}
		}
		a[left] = temp;
		QuickSort(a, begin, left);
		QuickSort(a, left + 1, end);
	}
}

//希尔排序
void ShellSort(int* array,int length)
{
	for (int gap = length / 2; gap >= 1; gap /= 2)
	{
		for (int k = gap; k < length; k++)  
		{
			int j = k - gap;  //j是分组的元素
			int ret = array[k];   //ret保存gap的数字
			while (j >= 0 && ret < array[j])
			{
				array[k] = array[j];   //
				k = j;
				j -= gap;
			}
			//说明比ret小
			array[j + gap] = ret;
		}
	}
}


void merge1(int* a, int begin, int mid, int end)
{
	int *data = new int[end-begin+1];
	int p1 = begin, p2 = mid + 1, p = 0;
	while (p1 <= mid && p2 <= end)
	{
		if (a[p1] <= a[p2])
		{
			data[p++] = a[p1++];
		}
		else
			data[p++] = a[p2++];
	}

	//左侧小集合还有剩余,依次放入大集合尾部
	while (p1 <= mid)
	{
		data[p++] = a[p1++];
	}

	//右侧小集合还有剩余,依次放入大集合尾部
	while (p2 <= end)
	{
		data[p++] = a[p2++];
	}
	int size = sizeof(a) / sizeof(a[0]);
	//将大集合的元素全部都复制回原数组
	for (int i = 0; i < size; i++)
	{
		a[i+begin] = data[i];
	}
}
void mergeSort(int *a, int begin, int end)
{
	if (begin < end)
	{
		int mid = (begin + end) / 2;
		mergeSort(a, begin, mid - 1);
		mergeSort(a, mid + 1, end);
		merge1(a, begin, mid, end);
	}
}
void prit(int *a, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}
int main()
{
	//int a[9] = { 49, 38, 65, 97, 76, 13, 27, 49 };
	int a[6] = { 1,5,3,2,4,8 };
	cout << "原数组是:" << endl;
	prit(a, 6);
	cout << "递归快速排序后:" << endl;
	QuickSort(a, 0, 6);
	prit(a, 6);
	cout << "希尔排序的结果是:" << endl;
	ShellSort(a, 6);
	prit(a, 6);
	cout << "归并排序的结果是:" << endl;
	mergeSort(a, 0, 5);
	prit(a, 6);
	system("pause");
	return 0;
}

Test shots:
Here Insert Picture Description
recursive quicksort Comments: recursive quicksort

Published 33 original articles · won praise 13 · views 1033

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/105185302