////////////////////////////// HeapSort ///////////////////////////////////// // ///////////////////////////////////////////////////////////////////////////// void HeapSort( LPSORTDATA lpData, LPENTITYINSTANCE lpEI[], zULONG ulRecordCnt ) { BuildHeap( lpData, lpEI, ulRecordCnt ); zULONG ulHoldCnt = ulRecordCnt; zULONG ulHoldMax = ulMaxRecords; zULONG *p1; zULONG ul; while ( ulRecordCnt > 1 ) { ulRecordCnt--; m_lMaxRecords--; p1 = CSIMP_GETSORTIDX_PTR( ulRecordCnt + 1, m_pUseIdx->m_pxIdx ); ul = *p1; *p1 = *(m_pUseIdx->m_pxIdx); *(m_pUseIdx->m_pxIdx) = ul; ul = Heapify( 1 ); while ( ul ) { ul = Heapify( ul ); } } ulRecordCnt = ulHoldCnt; m_lMaxRecords = ulHoldMax; }
/** Sort an array in ascending order using Heap Sort. * This runs O(n lg n). * @param array The data to sort. * @param size The number of elements in the array. */ void HeapSort(int *array, int size) { int x; /* Create the heap. O(n lg n) */ for(x = 2; x <= size; x++) { int index = x - 1; while(index > 0) { const int next_index = index / 2; if(array[next_index] < array[index]) { const int temp = array[next_index]; array[next_index] = array[index]; array[index] = temp; index = next_index; } else { break; } } } /* Loop removing the max element and placing it at the end. O(n lg n) */ while(size > 0) { const int temp = array[0]; size -= 1; array[0] = array[size]; array[size] = temp; Heapify(array, size); } }
void buildHeap(struct Heap* Heap) { int n = Heap->size - 1; int i; for (i = (n - 1) / 2; i >= 0; --i) Heapify(Heap, i); }
// O( n ) void CKEventThread::DelTimer( CKTimer* pTimer ) { assert_or_ret( pTimer ); UINT n; for( n = 1; n <= m_nTimers; n++ ) { if( m_ppTimers[n] == pTimer ) { m_ppTimers[n] = m_ppTimers[m_nTimers]; m_nTimers--; Heapify( CKTimer::CurrentTime(), n ); break; } } // Shrink heap if we are using less than 1/4 and more than minimum if( m_nTimers <= m_nTimerAlloc/4 && m_nTimerAlloc > MIN_TIMER_ALLOC ) { UINT nNewAlloc = m_nTimerAlloc/2; CKTimer** ppTimers = new CKTimer*[ nNewAlloc ]; memset( ppTimers, 0xDD, nNewAlloc * sizeof(CKTimer*) ); memcpy( ppTimers, m_ppTimers, (1+m_nTimers) * sizeof(CKTimer*) ); delete[] m_ppTimers; m_nTimerAlloc = nNewAlloc; m_ppTimers = ppTimers; } }
void Heapify(Heap *heap, int root){ if(NULL==heap){ return; } int rChild, lChild, smallest; lChild=(root*2)+1; rChild=lChild+1; smallest=root; if(lChild < heap->length && heap->compare((heap->array[smallest]), (heap->array[lChild]))==1){ smallest=lChild; } if(rChild < heap->length && heap->compare((heap->array[smallest]), (heap->array[rChild]))==1){ smallest=rChild; } if (root!=smallest){ void *temp=heap->array[root]; heap->array[root]=heap->array[smallest]; heap->array[smallest]=temp; Heapify(heap, smallest); } }
void MaxHeap::BuildHeap(std::vector<int>& heap, int size) { for (int i = Parent(size); i >= 0; i--) { Heapify(heap, size, i); } }
void Pop() { Heapify(); std::pop_heap(items_.begin(), items_.end(), comp_); items_.pop_back(); }
void MinHeap::DeleteMin() { int length = _vector.size(); _vector[0] = _vector[length - 1]; _vector.pop_back(); Heapify(0); }
int PopHeapRoot(std::vector<int> &heap, bool is_max_heap) { int result = heap[0]; heap[0] = heap.back(); heap.pop_back(); Heapify(heap, is_max_heap); return result; }
/*Builds a heap out of unordered array*/ int Make_Heap(vector<Knode> *arr) { int steps =0; for(int i = (arr->size()-1)/2; i>=0;i--) steps+=Heapify(arr,i,arr->size()-1); return steps; }
void Sorting::Build(){ unsigned short int* a = list; int n = size; for(int i = n/2; i >= 1; i--){ Heapify(a, i, n); } list=a; }
struct node* extractMin(struct Heap* Heap) { struct node* temp = Heap->array[0]; Heap->array[0] = Heap->array[Heap->size - 1]; --Heap->size; Heapify(Heap, 0); return temp; }
ITEM PQUEUEextractMax(PQ pq) { ITEM item; swap(pq, 0, pq->heapsize-1); item = pq->array[pq->heapsize-1]; pq->heapsize--; Heapify(pq, 0); return item; }
void Sorting::HeapSort(unsigned short int* a, int n){ int i, temp; for (i = n; i >= 2; i--){ temp = a[i]; a[i] = a[1]; a[1] = temp; Heapify(a, 1, i - 1); } }
void *removeRoot(Heap *heap){ if(isEmpty(heap)){ return NULL; } void *root=heap->array[0]; heap->array[0]=heap->array[heap->length-1]; heap->length-=1; Heapify(heap, 0); return root; }
MinHeap::MinHeap(int* array, int length) : _vector(length) { for(int i = 0; i < length; ++i) { _vector[i] = array[i]; } for(int i = length/2 ; i>=0; i--) Heapify(i); }
void Heap_Sort(int n) { /* 对R[1..n]进行堆排序,不妨用R[0]做暂存单元 */ int i; BuildHeap(n); /* 将R[1-n]建成初始堆 */ for(i=n;i>1;i--) { /* 对当前无序区R[1..i]进行堆排序,共做n-1趟。 */ R[0]=R[1]; R[1]=R[i];R[i]=R[0]; /* 将堆顶和堆中最后一个记录交换 */ Heapify(1,i-1); /* 将R[1..i-1]重新调整为堆,仅有R[1]可能违反堆性质 */ } /* end of for */ } /* end of Heap_Sort */
//Builds a Binary Heap from array of size n struct BHeap *HeapBuild (Data_t *array, int n) { int i; struct BHeap *heap = calloc (1, sizeof(struct BHeap)); heap->array = calloc (n, sizeof(Data_t)); heap->size = n; for (i = 0; i < n; i++) *(heap->array +i) = *(array + i); for (i = n/2; i >= 0; i--) Heapify(heap, i); return heap; }
pr_type HeapExtractMin (heap_type *A) { pr_type min; if (A->size<1) printf ("heap underflow.\n"); min=A->data[1].prio; A->data[1]=A->data[A->size]; --A->size; Heapify (A,1); return min; }
void Pop() { if(m_Head->ElementCount == 0) return; --m_Head->ElementCount; m_NodeBuffer[0] = m_NodeBuffer[m_Head->ElementCount]; memset(&m_NodeBuffer[m_Head->ElementCount], 0, sizeof(HeapNode<KeyT, ValueT>)); Heapify(0); }
void HeapSort::sort(std::vector<int>& vector) { Heapify(vector); for (int i = vector.size()-1; i >= 0; --i) { int tmp; tmp = vector[i]; vector[i] = vector[0]; vector[0] = tmp; fixDown(vector, 0, i); } }
void LargestKNumber(const std::vector<int> &vec, int k, std::vector<int> &heap) { heap.insert(heap.end(), vec.begin(), vec.begin() + k); BuildHeap(heap); for (int i = k; i < vec.size(); i++) { if (vec[i] > heap[0]) { heap[0] = heap.back(); heap.pop_back(); Heapify(heap, 0); InsertHeap(heap, vec[i]); } } }
TopoCentLB::HeapNode TopoCentLB::extractMax(HeapNode *heap,int *heapSize){ if(*heapSize < 1) CmiAbort("Empty Heap passed to extractMin!\n"); HeapNode max = heap[0]; heap[0] = heap[*heapSize-1]; heapMapping[heap[0].node]=0; *heapSize = *heapSize - 1; Heapify(heap,0,*heapSize); return max; }
Heap * newHeap(void *array[], size_t length, int (*compare)(const void *a, const void *b)){ int i; Heap *heap=(Heap *)malloc(sizeof(Heap)); heap->length=(int)length; heap->array=array; heap->compare=compare; for(i=(heap->length/2)-1; i>=0; i--){ Heapify(heap, i); } return heap; }
void HeapSort::Sort(vector<Point*> *points) { int end = points->size() - 1; Heapify(points); while (end > 0) { Swap(points, 0, end); end--; SiftDown(points, 0, end); } }
////////////////////////////// BuildHeap //////////////////////////////////// // ///////////////////////////////////////////////////////////////////////////// void BuildHeap( LPSORTDATA lpData, LPENTITYINSTANCE lpEI[], zULONG ulRecordCnt ) // non-recursive { zULONG k; #ifdef DEBUG_ALL OutputDebugString( "\nShowing heap prior to build heap ...\n" ); ShowHeap( this, m_pUseIdx->m_pxIdx ); #endif k = ulRecordCnt / 2; while( k > 0 ) { zULONG ul = Heapify( lpData, lpEI, k-- ); while ( ul ) { ul = Heapify( lpData, lpEI, ul ); } } } // end of: BuildHeap
int Heapsort(vector<Knode> *arr) { Knode temp; int steps =0; for(int i =(arr->size()-1);i>=1; i--) { temp = (*arr)[i]; (*arr)[i] = (*arr)[0]; (*arr)[0] = temp; steps+=Heapify(arr,0,i-1); } return steps; }
long ExtractMin() { long uNode,weight; uNode=heap[1]; weight=dist[heap[1]]; heap[1]=heap[heap_size]; heap_size-=1; Heapify(1); return uNode; }
void MaxHeap::Sort(std::vector<int>& v) { int size = (int)v.size(); if (size > 0) { BuildHeap(v, size); for (int i = size - 1; i > 0; i--) { int t = v[i]; v[i] = v[0]; v[0] = t; size -= 1; Heapify(v, size, 0); } } }
int vtkPolyDataSingleSourceShortestPath::HeapExtractMin() { if (Hsize == 0) return -1; int minv = this->H->GetValue(1); this->p->SetValue(minv, -1); this->H->SetValue(1, this->H->GetValue(Hsize)); this->p->SetValue(this->H->GetValue(1), 1); Hsize--; Heapify(1); return minv; }