void SortAll(Heap* heap,int* Array,int n,int k){ if(!heap){ return; } int i; int j=0; for(i=k+1;i<n;i++){ Array[j++]=DeleteMax(heap); Insert(heap,Array[i]); } while(GetMax(heap)!=-1){ Array[j++]=DeleteMax(heap); } }
void ModifiedMaxHeap::Insert(int newValue){ int length = _vector.size(); if(length <max_size){ _vector.push_back(newValue); BubbleUp(length); }else if(newValue < GetMax()){ DeleteMax(newValue); } }
void Insert(ElementType X, PriorityQueue H) { int i; if (X.rmsd > FindMax(H).rmsd) return; if (IsFull(H)) DeleteMax(H); for (i = ++H->Size; H->Elements[ i / 2 ].rmsd < X.rmsd; i /= 2) H->Elements[ i ] = H->Elements[ i / 2 ]; H->Elements[ i ] = X; }
int main(){ int N; scanf("%d", &N); Heap H = CreateHeap(N); ElementType * data = (ElementType *)malloc(sizeof(ElementType)*N); for(int i = 0; i<N; i++){ scanf("%d", data+i); Insert(H, data[i]); Show(H); } printf("Insert Finished\n"); int M = H->Size; while(!IsEmpty(H)){ printf("Max is: %d ", DeleteMax(H)); printf("Heap is:"); Show(H); } }
void ArraySorter::CreateHeap(int* arr, int n) { // Percolate down for each parent node int start = (n-1)/2; while(start >= 0) { PercolateDown(arr, start, n-1); start--; } // Remove max from heap (and place at end of array) int end = n-1; int max = 0; for(; end > 0; end--) { max = DeleteMax(arr, end); arr[end] = max; } }
int main () { int heapSize = 12; int intArray[4] = {4, 6, 2, 8}; heapHndl testHeap, builtHeap; testHeap = NewHeap (heapSize); if (IsEmpty (testHeap)) { printf ("working\n"); } Insert (testHeap, 9); Insert (testHeap, 3); Insert (testHeap, 5); Insert (testHeap, 2); Insert (testHeap, 32); Insert (testHeap, 6); Insert (testHeap, 1); Insert (testHeap, 43); Insert (testHeap, 23); Insert (testHeap, 42); Insert (testHeap, 65); Insert (testHeap, 57); if (IsFull (testHeap)) { printf ("heap currently full\n"); } DeleteMax (testHeap); builtHeap = BuildHeap (5, intArray, 4); printf ("%d\n", MaxValue (testHeap)); printf ("%d\n", MaxValue (builtHeap)); FreeHeap (testHeap); FreeHeap (builtHeap); return 0; }