[Data Structure--Hand Sorting Algorithm Part 4] Heap sorting, one article will take you to understand heap sorting

Table of contents

1. Application of heap -- heap sorting

1.1 Analysis of the idea of ​​heap sorting

2. Build a heap

2.1 Up-adjusting heap building: O(N*logN)

2.1.1 Adjust the code upwards

2.1.2 Upward adjustment of the heap building code

2.2 Adjusting the heap downwards: O(N)

2.2.1 Adjust the code down

2.2.2 Adjust the heap building code downward

3. Heap sort implementation code

4. Heap sort test


1. Application of heap -- heap sorting

The heap is a complete binary tree, and the complete binary tree uses arrays to store data optimally.

Heap sorting is to use the idea of ​​heap to sort. It is divided into two steps:
1. Build a heap

Ascending order: build a large pile

Descending order: build a small heap

2. Use the idea of ​​​​heap deletion to sort

Downward adjustment is used in both heap building and heap deletion, so heap sorting can be completed by mastering downward adjustment.

1.1 Analysis of the idea of ​​heap sorting

In this article, we use a small heap to explain, and the sorting of the small heap is in descending order.

Students who don’t know much about heap can take a look at the article of heap: click here to jump

1. We first adjust the elements in the array downwards to build a small heap;

2. The top element of the small heap must be the smallest in the array, so we exchange the top element (the first element of the array) with the last element of the array, and regard the missing element at the end of the array as an element in the array (size-- ) , Then start from the top of the heap to adjust and rebuild the small heap, and repeat it to achieve descending sorting (ascending order can be achieved by changing the small heap to a large heap).

2. Build a heap

We can use upward adjustment to build heaps, or use downward adjustment to build heaps. How should we choose?

Then I must choose whoever has the lowest time complexity. Next, let’s analyze the time complexity of the two heaps:

2.1 Up-adjusting heap building: O(N*logN)

2.1.1 Adjust the code upwards

void AdjustUp(HPDataType* a, int child)
{
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (a[child] < a[parent])//这里控制大小堆
		{
			Swap(&a[child], &a[parent]);

			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}

2.1.2 Upward adjustment of the heap building code

//建堆 -- 向上调整,时间复杂度:O(N*log(N))
for (int i = 0; i < size; i++)
{
	AdjustUp(a, i);
}

Let's draw a picture to analyze the time complexity of upward adjustment heap building:

The heap is a complete binary tree, and the full binary tree is also a complete binary tree. Therefore, we take the full binary tree as an example to deduce that the time complexity of upward adjustment and building the heap is O(N*logN).

2.2 Adjusting the heap downwards: O(N)

2.2.1 Adjust the code down

void AdjustDown(HPDataType* a, int size, int parent)
{
	int child = parent * 2 + 1;
	while (child < size)//当child大于了数组大小就跳出循环
	{
		//找出左右孩子中小/大的那个(假设法)
		if (child + 1 < size && a[child + 1] < a[child])
		{
			child++;
		}

		if (a[child] < a[parent])
		{
			Swap(&a[parent], &a[child]);

			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

2.2.2 Adjust the heap building code downward

for (int i = (size - 1 - 1) / 2; i >= 0; i--)
{
    AdjustDown(a, size, i);
}

Let's draw a picture to analyze the time complexity of downward adjustment heap building:

Adjust the heap building time complexity downward: O(N).

After such an analysis, we can know that the best choice is to adjust the positive stack downward.

3. Heap sort implementation code

//堆排序时间复杂度O(N + N*logN)
void HeapSort(int* a, int size)
{
	//升序 -- 建大堆
	//降序 -- 建小堆

	//建堆 -- 向上调整,时间复杂度:O(N*log(N))
	//for (int i = 0; i < size; i++)
	//{
	//	AdjustUp(a, i);
	//}

	//建堆 -- 向下调整,时间复杂度:O(N)
	//倒着调整
	//叶子节点不需要处理
	//倒数第一个非叶子节点:最后一个节点的父亲开始调整
	for (int i = (size - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(a, size, i);
	}

	//O(N*log(N))
	int end = size - 1;
	while (end)
	{
		//1.先交换
		Swap(&a[0], &a[end]);
		//2.再调整,选出当前大小的数组中最小数
		AdjustDown(a, end, 0);

		end--;
	}
}

4. Heap sort test

We built a small heap, so the final row is in descending order.

*** End of article ***

Guess you like

Origin blog.csdn.net/Ljy_cx_21_4_3/article/details/131065900