void Build_Tree(int n){ int i; Build_Max_Heap(n); for(i=n; i!=0; i--){ Max_Heapify(i, n); } }
Priority_Queue::Priority_Queue(vector<int> Heap) { Number=Heap.size(); for(int i=0;i<Number;i++) Max_Heap.push_back(Heap[i]); Build_Max_Heap(Max_Heap); }
void HeapSort(int A [], int n) { Build_Max_Heap(A, n); for (int i = n - 1; i > 0; --i)//从最后一个元素开始,依次与第一个元素交换,然后保持堆的性质,其中i只需要到第二个元素就可以了 { std::swap(A[0], A[i]); Max_Heapify(A, 0, i); } }
// 堆排序 void Heap_Sort() { Build_Max_Heap(); int i; for (i = length-1; i > 0; --i) { A[0]^=A[i]^=A[0]^=A[i]; --heap_size; Max_Heapify(0); } }
//堆排序算法 void HeapSort(int A[],int len) { int i; Build_Max_Heap(A,len); //不断的将堆顶元素与数组中索引为len的元素交换 for(i=len;i>=2;i--) { swap(&A[1],&A[len]); len--; Max_Heapify(A,1,len); } }
/*堆積排序程序碼*/ void HeapSort(int A[], int heap_size) { Build_Max_Heap(A, heap_size); int temp; for(int i = heap_size - 1; i >= 0; i--) { temp = A[0]; A[0] = A[i]; A[i] = temp; Max_Heapify(A, 0, i); } }
void HeapSort(int data[]) { Build_Max_Heap(data); // aftr calling this function all nodes are sorted in each subtree int i; int length = n; for (i =heap_size; i>=1; i--) { // exchange last and first element to be sorted int temp = data[0]; data[0] = data[i]; data[i] = temp; heap_size--; Max_Heapify(data, 0); } // after this for loop all subtree will be sorted. }