非递归堆排序改进,从下标0开始排序

网上堆排序用到的数组,很多都是从1开始进行排序。我做了改进,可以从0开始。

希望能够帮助大家,如果有错误,非常感激能够指出。

#include <stdio.h>

void print_arr3(int arr[], int count)
{
	int i;
	printf("\n_____ current list:\n");
	for(i=0; i<count; i++)
	{
		 printf("%d ",arr[i]);
	}
}

void AdjustTree4(int arr[], int index, int size)
{
	int child;
	int value, temp;
	child = index * 2 + 1;
	value = arr[index];
	while (child < size)
	{
		if ((child + 1) < size && arr[child] < arr[child + 1])
			child = child + 1;

		if (arr[child] > value)
		{
			arr[index] = arr[child];
			index = child;
			child = index * 2 + 1;
		}
		else
			child = size;
	}
	arr[index] = value;
}

void HeapSort4(int arr[], int size)
{
	if (size <= 0)
		return;

	int index = (size-1)/2;
	for (; index>=0; index--)
	{
		AdjustTree4(arr, index, size);
	}

	for (index = size - 1; index>=0; index--)
	{
		int temp;
		temp = arr[0];
		arr[0] = arr[index];
		arr[index] = temp;

		AdjustTree4(arr, 0, index);
	}
}

int main(int argc, char *argv[])
{
	int n;
//	int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 34};
	int arr[] = {1, 2, 3, 4, 28, 15, 78, 72, 56, 7, 8, 34, 32, 23, 21, 56, 65, 34, 21, 11, 3, 4, 6, 23};
	n = sizeof(arr) / sizeof(arr[0]);
	HeapSort4(arr, n);
	print_arr3(arr, n);
    return 0;
}






                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个非递归实现的堆排序的 C++ 程序: ```cpp #include <iostream> #include <stack> using namespace std; void heapify(int arr[], int n, int i) { int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < n && arr[l] > arr[largest]) largest = l; if (r < n && arr[r] > arr[largest]) largest = r; if (largest != i) { swap(arr[i], arr[largest]); heapify(arr, n, largest); } } void heapSort(int arr[], int n) { for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); stack<int> s; for (int i = n - 1; i >= 0; i--) { swap(arr[0], arr[i]); int j = 0; while (true) { int l = 2 * j + 1; int r = 2 * j + 2; int maxIndex = j; if (l < i && arr[l] > arr[maxIndex]) maxIndex = l; if (r < i && arr[r] > arr[maxIndex]) maxIndex = r; if (maxIndex != j) { swap(arr[j], arr[maxIndex]); s.push(j); j = maxIndex; } else if (!s.empty()) { j = s.top(); s.pop(); } else { break; } } } } void printArray(int arr[], int n) { for (int i = 0; i < n; ++i) cout << arr[i] << " "; cout << endl; } int main() { int arr[] = { 12, 11, 13, 5, 6, 7 }; int n = sizeof(arr) / sizeof(arr[0]); heapSort(arr, n); cout << "Sorted array is \n"; printArray(arr, n); } ``` 这个程序中,我们首先定义了两个函数 `heapify()` 和 `heapSort()` 来实现堆排序算法。 `heapify()` 函数用于维护最大堆的性质, `heapSort()` 函数则用于对堆进行排序。 在 `heapSort()` 函数中,我们首先构建一个最大堆,然后将堆顶元素(即最大元素)与堆中最后一个元素进行交换。然后我们使用一个栈 `s` 来记录需要进行 `heapify()` 操作的下标,然后从堆顶开始,依次将每个元素向下移动(通过不断交换元素),直到该元素达到其应该在的位置。当遇到需要交换的情况时,将下标压入栈中,继续向下移动;当元素已经到达正确位置,但栈不为空时,从栈中弹出一个下标继续向下移动;当元素已经到达正确位置且栈为空时,退出循环,进行下一次交换操作。 在 `printArray()` 函数中,我们用于输出排序后的结果。最后在 `main()` 函数中,我们定义了一个整型数组 `arr`,然后调用 `heapSort()` 函数对其进行排序,并输出排序后的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值