Data Structure | Heap Sort

Data Structure | Heap Sort

If you have not seen the implementation of the heap, you can read the previous chapter firstThe implementation of the heap, and then look at this heap sorting. It’s relatively simple~~

  • Here, heap sorting first builds a heap. When building a heap, should we build a small heap or a large heap?
  • In the heap sorting algorithm, the process of establishing a big top heap is to ensure that the root node of the heap is the largest element in the entire heap.
    When you need to sort in ascending order , you want the largest element to be sorted At the end of the sequence.
  • The basic idea of ​​heap sorting is to first build the sequence to be sorted into a large top heap, then exchange the top element of the heap (the largest element) with the last element of the heap, then reconstruct the large top heap for the remaining elements, and then exchange again The top element of the heap and the last element of the heap, and so on, until the entire sequence is in order.
  • The purpose of establishing a large top heap is to sink the largest element to the end of the sequence after each exchange, gradually forming an ordered sequence. If you wish to sort in ascending order, building a big top heap is consistent with this goal.

Build a lot

void Swap(HPDataType* p1, HPDataType* p2)
{
    
    
	HPDataType tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void AdjustUp_Big(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;
		}
	}
}

have a test:

int a[] = {
    
     4,6,2,1,5,8,2,9 };
int sz = sizeof(a) / sizeof(a[0]);
for (int i = 1; i < sz; i++)
{
    
    
	AdjustUp_Big(a, i);
}

sort

void AdjustDown_Big(HPDataType* a, int size, int parent)
{
    
    
	int child = parent * 2 + 1;
	while (child < size)
	{
    
    
		if (child + 1 < size && a[child + 1] > a[child])
			++child;
		
		if (a[child] > a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);

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

Insert image description here

//end是在最后一个元素的下标-1
int end = sz - 1;
while (end > 0)
{
    
    
	//根和最后一个值进行交换,最后一个数不看做堆里面的
	Swap(&a[0], &a[end]);
	AdjustDown_Big(a, end, 0);
	--end;
}

Results and all codes

void Swap(HPDataType* p1, HPDataType* p2)
{
    
    
	HPDataType tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void AdjustUp_Big(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;
		}
	}
}
void AdjustDown_Big(HPDataType* a, int size, int parent)
{
    
    
	int child = parent * 2 + 1;
	while (child < size)
	{
    
    
		if (child + 1 < size && a[child + 1] > a[child])
			++child;
		
		if (a[child] > a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);

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

void HeapSort()
{
    
    
	//建大堆
	int a[] = {
    
     4,6,2,1,5,8,2,9 };
	int sz = sizeof(a) / sizeof(a[0]);
	/*for (int i = 1; i < sz; i++)
	{
		AdjustUp_Big(a, i);
	}*/
	
	//向下调整建堆,这样效率更高,上面那个也可以
	for (int i = (sz - 1 - 1)/2; i >= 0; --i)
	{
    
    
		AdjustDown_Big(a, sz, i);
	}

	//打印
	printf("排序前:");
	for (int i = 0; i < sz; i++)
	{
    
    
		printf("%d ", a[i]);
	}
	
	printf("\n");

	//排序
	//end是在最后一个元素的下标-1
	int end = sz - 1;
	while (end > 0)
	{
    
    
		//根和最后一个值进行交换,最后一个数不看做堆里面的
		Swap(&a[0], &a[end]);
		AdjustDown_Big(a, end, 0);
		--end;
	}

	//打印
	printf("排序后:");
	for (int i = 0; i < sz; i++)
	{
    
    
		printf("%d ", a[i]);
	}
}

Insert image description here

Guess you like

Origin blog.csdn.net/2201_76004325/article/details/134630041