In the case of limited memory, the first 100 of the 1 million numbers are calculated.
Problem-solving ideas: The first thought is to divide 1 million numbers into 100 parts, one is 10,000, and then 10,000 to build a minimum heap to find the first 100, 100 is 10,000 so you can find the first 100;
The code is as follows:
#include <windows.h>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace Std;
const int n=10000;
const int k=100;
void Createarray (Vector<int>&array)
{
Srand (Time (0));
Array.reserve (N);
for (size_t i = 0; i < N; i++)
{
Array.push_back (rand ()% 10000);
}
for (size_t j = n-k; J < N; J + +)
{
ARRAY[J] = rand ()%N;
}
}
void Adjustdown (int* A, size_t size, int root)
{
int child = root * 2 + 1;
while (Child < size)
{
if (child + 1 <size && A[child + 1] < A[child])
{
++child;
}
if (A[child] < A[root])
{
Swap (A[child], a[root]);
root = child;
Child = 2 * root + 1;
}
Else
{
Break
}
}
}
void Gettop (Vector<int>&array)
{
int A[k] = {};
for (size_t i = 0; i < K; i++)
{
A[i] = Array[i];
}
for (int i = (K-2)/2; I >= 0; i--)
{
Adjustdown (A, K, i);
}
for (int j = K; J < N; J + +)
{
if (A[0]<array[j])
{
A[0] = Array[j];
Adjustdown (A, K, 0);
}
}
for (size_t i = 0; i < K; i++)
{
cout << A[i] << "";
}
cout << Endl;
}
void Test ()
{
vector<int>array;
Createarray (array);
Gettop (array);
}
This article is from the "Stream Wind" blog, please be sure to keep this source http://xiexiankun.blog.51cto.com/10785425/1775367
1 million number of first 100