void HeapSort(int* list,int m) { int i,temp; for(i = list[0]/2; i >= 1; i--) { SiftDown(list,i,list[0]); } for(i = a[0]; i > a[0] - m; i--) { temp = list[i]; list[i] = list[1]; list[1] = temp; SiftDown(list,1,i-1); } }
void HeapSort( double A[], int length, int I[] ) { int i, tmp_int; double temp; /* Store original ordering of indices */ for (int i = 0; i < length; i++) { I[i] = i; } /* Re-order A as a binary heap ("heapify A"). Only need to sift first half of A, where all possible parent nodes reside. SiftDown will float each improperly placed parent down and re-order their subtrees to satisfy the heap property. */ for (i = (length / 2); i >= 0; i--) { SiftDown( A, i, length - 1, I ); } /* A is now a binary heap, with Amax = A[0]. Swap it and the last element and remove the last element from consideration. The only node out of place now is A[0]. Sift it down to make A[0:1:length-2] a heap. Now A[0] is the max. of this new heap. Swap it with the second-to-last element and remove it from consideration. Sift A[0] down to make A[0:1:length-3] a heap. Repeat until fully-sorted! */ for (i = length-1; i >= 1; i--) { temp = A[0]; tmp_int = I[0]; // Swap A[0] = A[i]; I[0] = I[i]; A[i] = temp; I[i] = tmp_int; SiftDown( A, 0, i-1, I ); } }
void extract_min() { Swap(1, heap_.size() - 1); heap_.pop_back(); id_to_point_[point_to_id_[point_to_id_.size() - 1]] = 0; point_to_id_.pop_back(); SiftDown(1); }
void SiftDown( double A[], int root, int bottom, int I[] ) { // Considering A as a binary heap, the given "root" node is a parent to up to two children // at indices 2*root + 1 and 2*root + 2 if they exist. If neither exists, root is a leaf node. int maxChild = root * 2 + 1; // Find the biggest of the two children and set it equal to maxChild if ( maxChild < bottom ) { int otherChild = maxChild + 1; maxChild = ( A[otherChild] > A[maxChild] ) ? otherChild : maxChild; // Reversed for stability } else { if ( maxChild > bottom ) return; // Don't overflow; root is a leaf node and therefore in the right place. } // If we have the correct ordering, we are done ( A[parent] > A[both children] ). if ( A[root] >= A[maxChild] ) return; // Otherwise, swap the parent with the larger of the two children. double temp = A[root]; int tmp_int = I[root]; A[root] = A[maxChild]; I[root] = I[maxChild]; A[maxChild] = temp; I[maxChild] = tmp_int; // Tail queue recursion (propagate the parent down since it may still be out of place). // Will be compiled as a loop with correct compiler switches. SiftDown( A, maxChild, bottom, I ); }
_Node* HeapRemove() { _Node* ret = myHeap[0]; myHeap[0] = myHeap[heap_sz-1]; heap_sz--; SiftDown(); return ret; }
void MaxHeap<T>::RemoveTop() { if(!arr.empty()) { std::swap(arr[0],arr.back()); arr.pop_back(); SiftDown(0,arr.size()); } }
std::vector<T>&& MaxHeap<T>::Sort() { int n = arr.size(); for(int i=0;i<n-1;i++) { std::swap(arr[0],arr[n-i-1]); SiftDown(0,arr.size()-i-1); } return std::move(arr); }
void CTaskHeap::Update(int iNode, SCHCMP *pfCompare) { if (iNode < 0 || m_nCurrent <= iNode) { return; } SiftDown(iNode, pfCompare); SiftUp(iNode, pfCompare); }
void HeapSort::Heapify(vector<Point*> *points) { int start = (points->size() - 2) / 2; while (start >= 0) { SiftDown(points, start, points->size() - 1); start--; } }
void CTaskHeap::Sort(SCHCMP *pfCompare) { int s_nCurrent = m_nCurrent; while (m_nCurrent--) { PTASK_RECORD p = m_pHeap[m_nCurrent]; m_pHeap[m_nCurrent] = m_pHeap[0]; m_pHeap[0] = p; SiftDown(0, pfCompare); } m_nCurrent = s_nCurrent; }
PTASK_RECORD CTaskHeap::Remove(int iNode, SCHCMP *pfCompare) { if (iNode < 0 || m_nCurrent <= iNode) return NULL; PTASK_RECORD pTask = m_pHeap[iNode]; m_nCurrent--; m_pHeap[iNode] = m_pHeap[m_nCurrent]; SiftDown(iNode, pfCompare); SiftUp(iNode, pfCompare); return pTask; }
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); } }
/** * Sort an array of shader variable structures alphabetically. * * @param first the first element of the array to sort * @param count the number of elements in the array */ void GlesSortShaderVariables(ShaderVariable * first, GLsizeiptr count) { GLsizeiptr end; ShaderVariable temp; /* Build the heap structure */ Heapify(first, count); end = count - 1; while (end > 0) { temp = first[0]; first[0] = first[end]; first[end] = temp; SiftDown(first, 0, --end); } }
void MaxHeap<T>::SiftDown(unsigned n, unsigned size) { unsigned l = LeftChild(n); unsigned r = RightChild(n); unsigned max_index; if(r>=size) { if(l>=size) return; else max_index = l; } else { arr[l]>=arr[r] ? max_index = l : max_index = r; } if(arr[n]<arr[max_index]) { std::swap(arr[n],arr[max_index]); SiftDown(max_index,size); } }
int main() { int i, j, x, y; n = N; scanf("%d", &i); x = H[i]; y = H[n]; n = n - 1; if(i == n + 1) { for(j = 1; j <= n; ++j) printf("%d ", H[j]); } else { H[i] = y; if(y >= x) SiftUp(H, i); else SiftDown(H, i); for(j = 1; j <= n; ++j) printf("%d ", H[j]); } return 0; }
/** * Convert the shader variable array into a heap structure. * * @param first pointer to the first element of the array * @param count number of array elements */ static void Heapify (ShaderVariable * first, GLsizeiptr count) { GLsizeiptr start; for (start = count / 2 - 1; start >= 0; --start) SiftDown(first, start, count - 1); }
void MaxHeap<T>::Heapify() { int i = Parent(arr.size()-1); for( ;i>=0;i--) SiftDown(i,arr.size()); }