-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK_Min.cpp
More file actions
63 lines (60 loc) · 984 Bytes
/
K_Min.cpp
File metadata and controls
63 lines (60 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
using namespace std;
void Heap_Modify(long a[],int index,int length)
{
int largest=index;
int left=2*index;
int right=2*index+1;
if(left<length&&a[left]>a[index])
largest=left;
if(right<length&&a[right]>a[largest])
largest=right;
if(largest!=index)
{
long tmp=a[largest];
a[largest]=a[index];
a[index]=tmp;
Heap_Modify(a,largest,length);
}
}
void Build_Max_Heap(long a[],int length)
{
for(int i=length/2;i>=0;--i)
Heap_Modify(a,i,length);
}
void K_Min(long a[],int k)
{
FILE *fp;
fp=fopen("data.txt","r");
if(NULL==fp)
{
fprintf(stderr,"fopen() error:%s\n",strerror(errno));
return;
}
int i=0;
while((i<k)&&fscanf(fp,"%ld",&a[i]) !=EOF)
{
i++;
}
Build_Max_Heap(a,k);
long tmp=0;
while(fscanf(fp,"%ld",&tmp)!=EOF)
{
if(tmp<a[0])
{
a[0]=tmp;
Heap_Modify(a,0,k);
}
}
fclose(fp);
}
int main()
{
long a[10];
K_Min(a,4);
for(int i=0;i<4;++i)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}