//Minimum heap ImplementationvoidMyglobal::min_heapify (int*a,intAintLeftintRight ) { intChild =0; intTMP =0; intTEMPB =0; intParent =Left ; ASSERT (a!=NULL); for(TMP = a[parent],tempb=b[parent]; Leftson (parent) <= right; Parent =Child ) { Child=Leftson (parent); if(Child! = parent && A[child] > A[child +1]) { child++; } if(A[child] <tmp) {A[parent]=A[child]; B[parent]=B[child]; } Else //satisfies the minimum heap characteristics and exits directly Break; } A[parent]=tmp; B[parent]=TEMPB;}//Create minimum heapvoidMyglobal::build_minheap (int*a,int*b,intsize) { inti =PARENT (size); ASSERT (a!=NULL); for(; I >=0; --i) {min_heapify (A, B, I, Size-1); }}//Main functionint* Myglobal::find_kmin_num_4 (int*a,int* B,intSizeintk) { inti =0; ASSERT (a!=NULL); Build_minheap (A, B, size-1); int* m =New int[K]; for(i =0; I < K; ++i) {printf ("%d\t", b[0]); //Delete a[0], release a[size-1-i]M[i] = b[0]; a[0] = a[size-1-i]; Min_heapify (A, B,0, Size-2-i); } printf ("\ n"); returnm;}
Where a is an array and B represents the subscript for the array. At the end of the printf if output A[0] is the smallest k term of the array, the output B[0] is the subscript of the smallest k of the array. b See need to be added to delete
In addition the largest heap in: http://blog.chinaunix.net/uid-20937170-id-3347493.html
Smallest (Large) heap finds the smallest (large) K-key value of an array