// Paging Heap sort algorithm for n elements // in a Monitored array, a. #include #include "array.h" #include "pheaps.C" // for paging heaps extern MArray a; void swap (int,int); void heapify (int); void pheap (int n) { int i,j,k,lc,rc; int key; int left, right; // Control order of operand evaluation adjust (n); // for paging heaps only heapify (n); k = n; decr (k); // k--; while (k>0) { swap (k,0); // swap (0,k) generates more faults key = a[i=0]; j = 1; if (k>2 && a[2]>a[1]) j = 2; // j is larger child of 0. while ((jkey)) { a[i] = a[j]; i = j; child (i,lc,rc); // find left child and right child j = lc; if (rcleft) // a[lc]0 && key>a[j]) { a[i] = a[j]; i = j; j = parent (i); if (j<0) j = 0; } a[i] = key; incr (k); // k++ for ordinary heaps } }