Heap Sort, Quick Sort, Merge Sort Algorithms Comparison

Quick sort: Each sorting divides the array into a certain element as the dividing point, the value greater than the element is on the right side of the element, the value smaller than the element is on the left side of the element, and a certain element is the center, respectively, the left side and the right side of the element Subarrays are sorted quickly. Each time the position of an element is determined, multiple elements are ordered, so it is called quicksort.

  Heap sort: treat the array as a complete binary tree, the positional relationship between the root node root and the child node node is 2*root=node or 2*root+1=node, build a large root heap and a small root heap (the top record of the heap is greater than (less than) or equal to all child node records), according to the nature, each time the maximum value (minimum value) of the heap can be found out and placed in the specified position, and then the heap is adjusted (remove the sorted elements) until all elements are sorted until.

Merge sort: Using the divide and conquer method, each time the array is divided into two, sub-arrays are generated, the sub-arrays are sorted separately, and the sorted sub-arrays are merged until an ordered and complete array is generated.

Three sorting implementations.

void Pushdown(int *a, int low, int high)
{
	int i = low, j = 2 * i;
	int temp = a[i];
	while (j <= high)
	{
		if (j<high&&a[j]>a[j + 1])
		{
			j++;
		}
		if (temp>a[j])
		{
			a[i] = a[j];
			i = j;
			j = 2 * i;
		}
		else break;
	}
	a[i] = temp;
}
// heap sort
void HeadSort(int *a, int n)
{
	for (int i = n / 2;i >= 1;i--)
	{
		Pushdown(a, i, n);
	}
	for (int i = n;i >= 2;i--)
	{
		swap(a[1], a[i]);
		Pushdown(a, 1, i - 1);
		break;
	}

}

//quick sort  
void quicksort(int *a,int left, int right)
{
	int i, j, temp, t;
	if (left > right)
	{
		return;
	}
	i = left;
	j = right;
	temp = a[left];
	while (i != j)
	{
		while (a[j] >= temp && i < j)
		{
			j--;
		}
		while (a[i] <= temp && i < j)
		{
			i++;
		}
		if (i < j)
		{
			t = a[i];
			a[i] = a[j];
			a[j] = t;
		}
	}
	a[left] = a[i];
	a[i] = temp;
	quicksort(a,left, i - 1);
	quicksort(a,i + 1, right);
}

// merge sort

void merge(int* a, int l, int m, int h) {
	if (l == h)
		return;

	int* b = (int*)malloc(sizeof(int) * (h - l + 1));
	int i = l, j = m + 1;
	int k = 0;

	while (i <= m && j <= h)
		b[k++] = a[i] > a[j] ? a[j++] : a[i++];

	if (i <= m)
		while (i <= m)
			b[k++] = a[i++];
	if (j <= h)
		while (j <= h)
			b[k++] = a[j++];
	//Writeback
	i = l;
	k = 0;

	for (; i <= h;)
		a[i++] = b[k++];
}

void mergeSort(int* a, int l, int h) {
	if (l >= h)
		return;
	int m = (h + l) / 2;
	mergeSort(a, l, m);
	mergeSort(a, m + 1, h);
	merge(a, l, m, h);
}

Quick sort recursively burst stack plus this sentence:

//This sentence is used to solve the problem of recursive stack explosion of C++ quick sort and is added in front of the header file
#pragma comment(linker, "/STACK:1024000000,1024000000")  



The 100,000 data quick row has exploded QWQ.



Guess you like

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