打印TopK问题:升序

void PrintTopK(int* a,int k,int n)
{
	int* kMinHeap = (int*)malloc(sizeof(int) * k);
	assert(kMinHeap); 
	for (int i = 0; i < k; ++i)
	{
		kMinHeap[i] = a[i];
	}
	for (int i = (k - 1 - 1) / 2; i >= 0; i--)
	{
		adjustDown(kMinHeap, k, i);
	}

	for (int j = k; j < n; j++)
	{
		if (a[j] > kMinHeap[0])
		{
			kMinHeap[0] = a[j];
			adjustDown(kMinHeap, k, 0);
		}
	}

	for (int i = 0; i < k; i++)
	{
		cout << kMinHeap[i] << " ";
	}
}

你可能感兴趣的:(算法,java,数据结构)