Selective sorting and its application

Keywords: less

Simple selection sorting
One minimum at a time
Time complexity o(n^2), space complexity o(1)
void selectsort(int a[], int n)
{
	int min;
	for (int i = 0; i < n; i++)
	{
		min = i;//
		for (int j = i + 1; j < n; j++)
			if (a[j] < a[min])min = j;//Find minimum
		if (min != i)swap(a[i], a[min]);//Put the minimum value first
	}
}
Building a big root pile
Select the subtree with [n/2~1] as the root in turn to determine whether the value of the node is greater than the value of its left and right nodes, and exchange if it is greater. (exchange may destroy the following heap)
Using a[0] to store array length
void buildmaxheap(int a[], int len)
{
	for (int i = len / 2; i > 0; i--)
		adjustdown(a, i, len);
}

Adjust the data k downward < select the maximum value of the subtree whose root is this node, and exchange it so that the exchanged node is also the maximum value of its subtree >

Time complexity o(n), in the worst case, the following heap will also be exchanged after each exchange with a child node
The total time i s s=2^i-1(h-i);i is the current layer, h=logn is the height, h-i is the number of times to compare o n the subtree, the total is I from h to 1, and the time complexity is o(n);
void adjustdown(int a[], int k, int len)
{
	a[0] = a[k];//If the property of the large root heap is changed by the exchange node, it is caused by a[k]
	for (int i = 2 * k; i < len; i *= 2)//The child of a node is 2i
	{
		if (i < len&&a[i] < a[i + 1])
			i++;
		if (a[0] > a[i])break;
		else
		{
			a[k] = a[i];
			k = i;
		}
	}//Find the insertion position of the node
	a[k] = a[0];//Put the value of the filtered node in the final location
}
Heap sorting algorithm
Output the top element of the heap to the end of the queue each time, and then stack the remaining i-1 elements until only one is left

Time complexity o(nlogn), space complexity o(1)

void heapsort(int a[], int len)
{
	buildmaxheap(a, len);
	for (int i = len; i > 1; i--)
	{
		swap(a[i], a[1]);//Exchange the largest element a[1] with the last element
		adjustdown(a, 1,i-1);//Readjust to a large pile	
		//Because each adjustment is only compared with one side subtree at most, the number of basic statement executions is directly proportional to
		//The height of the tree, i.e. logn;
	}
}
When inserting a node, the large root heap structure needs to be adjusted at the end of the heap
k is the node that needs to be adjusted upward. Judge the size relationship between the node and its parents
void adjustup(int a[], int k)
{
	a[0] = a[k];
	int i = k / 2;//Its ancestor node
	while (i > 0 && a[i] < a[0])//As long as its ancestor node is smaller than this node, it will enter the loop
	{
		a[k] = a[i];//Small down
		k = i;
		i /= 2;
	}//Insertion position found at end of loop
	a[k] = a[0];
}

Judge whether it is a small pile

Because it is a complete binary tree, the first n/2 is the branch node, and the second is the leaf node
Scan all branch nodes and return an error when the keyword of child node is less than that of root node
bool isminheap(int a[], int n)
{
	if (n % 2 == 0)//An even number indicates that there is a single branch node
	{
		if (a[n] < a[n/2])//The last leaf node belongs to a single branch node
			return 0;
		for (int i = n / 2 - 1; i >= 1; i--)
			if (a[2 * i] < a[i] || a[2 * i + 1] < a[i])
				return 0;
	}
	else
	{
		for (int i = n / 2; i >= 1; i--)
			if (a[2 * i] < a[i] || a[2 * i + 1] < a[i])
				return 0;
	}
	return 1;
}

Posted by Axcelcius on Fri, 15 Nov 2019 09:18:52 -0800