link deleteMax(link h) { if (hl->red) h = rotR(h); if (hr == z) { free(h); return z; } if (!hr->red && !hrl->red) h = mvRedR(h); hr = deleteMax(hr); return balance(h); }
void heapSort() { int i; for (i = SIZE / 2;i > 0;i--) percolateUp(i); /* Build heap */ for (i = SIZE;i > 0;i--) Arrays[i] = deleteMax(); }
void STdeleteMax(link head) { if (STcount(head) == 0) printf("Underflow"); exit(1); if (!head->l->red && !head->r->red) head->red = 1; head = deleteMax(head); if (STcount(head) > 0) head->red = 0; }
int main (int argc, char **argv){ printf("Welcome to the heap driver program!\n"); //testing insertion into a heap and testing printheap printf("Inserting several nodesand printing the heap\n"); printf("Result should be 45135, 2534, 325, 50, 211, 25, 3, 294, 1.\n"); HeapRef heap = newHeap(10); insert(heap, 50); insert(heap, 211); insert(heap, 45135); insert(heap, 3); insert(heap, 325); insert(heap, 2344); insert(heap, 25); insert(heap, 2534); insert(heap, 294); insert(heap, 1); printHeap(heap); //testing maxValue printf("\nPrinting max of heap, result should be 45135\n"); printf("%d\n", maxValue(heap)); printf("\n"); //testing deleting the max nodes from a heap printf("Deleting 2 max nodes and reheapifying\n"); printf("Result should be 2344, 325, 211, 294, 50, 1, 25, 3.\n"); deleteMax(heap); deleteMax(heap); printHeap(heap); printf("\n"); //testing heap sort function printf("Testing heap sort. Result should be 1, 3, 25, 42, 50, 211, 294, 325, 543, 2311, 2344, 2534, 45135.\n\n"); int keys [] = {50, 211, 45135, 3, 325, 2344, 25, 2534, 294, 1, 42, 543, 2311}; heapSort(keys, 13); for(int i = 0; i < 13; i++){ printf("%d\n", keys[i]); } printf("The heap driver is now done!\n"); return 0; }
void heapSort(int keys[], int numKeys){ if(numKeys == 0) return; heapHndl H = newHeap(numKeys); for(int i = 0; i < numKeys; i++) insert(H, keys[i]); for(int i = numKeys-1; i > -1; i--){ keys[i] = maxValue(H); deleteMax(H); } freeHeap(&H); }
int findKthMax(int a[], int n, int k){ MaxHeap *heap = buildMaxHeap(a, n); int max; for (int i=0; i<k; i++) { max = deleteMax(heap); } return max; }
int main() { int i; MaxHeap h = create(20); insert(h, 20); insert(h, 21); insert(h, 27); insert(h, 17); insert(h, 37); insert(h, 35); for(i = 1; i < h->size + 1; i++) { printf("%d ", h->elements[i]); } printf("\n-------------------------\n"); printf("%d\n", deleteMax(h)); for(i = 1; i < h->size + 1; i++) { printf("%d ", h->elements[i]); } printf("\n-------------------------\n"); printf("%d\n", deleteMax(h)); for(i = 1; i < h->size + 1; i++) { printf("%d ", h->elements[i]); } return 0; }
// 希尔排序 void heapSort(int *data, int n) { unsigned long count = 0; clock_t start = clock(); buildMaxHeap(data, n); // printArray(data, n); // printf("------------------------------------\n"); int i; for (i = n; i > 1; i--) { // 删除到i=2时就可以了 deleteMax(data, i); count++; // printf("[%d] ", n-i+1); // printArray(data, n); } timeinfo("heapSort", n, count, clock() - start); }
void heapSort(int keys[], int numKeys){ heap h = newHeap(numKeys); for(int iter = 0; iter<numKeys; iter++){ insert(h, keys[iter]); } int tmp[numKeys]; for(int i=0; i<numKeys; i++){ tmp[i] = maxValue(h); deleteMax(h); } for(int i=0; i<numKeys; i++){ keys[numKeys-i-1]=tmp[i]; } delete(h); }
void insertMed(int *H,int n,int& N1,int& N2,int x) // inserts an element in the med heap { int m=findMedian(H,n,N1,N2); if(m==-1){H[n-1]=x;N2++;return;} if(x<=m) { if(N2==N1+1)insertMax(H,N1,x); else { int max=findMax(H,N1);deleteMax(H,N1); insertMin(H+n-N2,N2,max);insertMax(H,N1,x); } } else { if(N2==N1)insertMin(H+n-N2,N2,x); else { int min=findMin(H+n-N2,N2);deleteMin(H+n-N2,N2); insertMax(H,N1,min);insertMin(H+n-N2,N2,x); } } }
void deleteMed(int *H,int n,int& N1,int& N2) // deletes an element from the med heap { if(N1==N2) {deleteMax(H,N1);return;} else {deleteMin(H+n-N2,N2);return;} }
void mergeHeap(int A[],int B[]) { int total=A[0]+B[0]+(-1); printf("%i\n", total); int C[total]; int count=0,x,y,max; max=total; x=deleteMax(A[A[0]],A,1,A[0]); y=deleteMax(B[B[0]],B,1,B[0]); while(count<max) { if(x>y) { if (A[0]>0) { C[total] = x; int a =x; printf("%i\n", x); x=deleteMax(A[A[0]],A,1,A[0]); } } else if(y>x) { if (B[0]>0) { C[total]=y; int b = y; printf("%i\n", y); y=deleteMax(B[B[0]],B,1,B[0]); } } else if (x==y) { printf("%i\n", x); x=deleteMax(A[A[0]],A,1,A[0]); y=deleteMax(B[B[0]],B,1,B[0]); C[total] = x; } total--; // printf("X:%i Y:%i\n", x,y); // system("pause"); count++; } printf("X:%i Y:%i\n", x,y); if (x>y) { C[1] = x; C[0]=y; } else if (y>x) { C[1]=y; C[0]=x; } printf("\n\n"); int i; total=A[0]+B[0]; printf("C:"); for(i=0;i<max;i++) printf("%i\n", C[i]); }