// Binary heap routines - root has minimum // Heap must have a handle array such that handle[i] // gives the heap subscript for id number i. In addition, // there is a parallel array "id" to "priority" that gives // the id number for priority[i] (thus, handle[id[i]]==i . . .). #include #include #include #include #define LEFT(x) (2*(x)) #define RIGHT(x) (2*(x)+1) #define PARENT(x) ((x)/2) float CPUtime() { struct rusage rusage; getrusage(RUSAGE_SELF,&rusage); return rusage.ru_utime.tv_sec+rusage.ru_utime.tv_usec/1000000.0 + rusage.ru_stime.tv_sec+rusage.ru_stime.tv_usec/1000000.0; } void buildMinHeap(int priority[],int id[],int handle[]) { int n=priority[0]; int i,iPriority,iId,j,best,l,r; for (i=n/2;i>0;i--) { iPriority=priority[i]; iId=id[i]; j=i; while (1) { l=LEFT(j); if (l>n) break; r=RIGHT(j); if (r>n || priority[l]priority[i]) { j=i; while (1) { l=LEFT(j); if (l>n) break; r=RIGHT(j); if (r>n || priority[l]n) break; r=RIGHT(j); if (r>n || priority[l]priority[0]) { if (i!=25) printf(" %3d %3d\n",i,handle[i]); } else if (i==maxHandle) printf(" %3d %3d %3d\n",i,id[i],priority[i]); else printf(" %3d %3d %3d %3d\n",i,handle[i],id[i],priority[i]); for (i=2;i<=priority[0];i++) if (priority[i]