void heapSort(long array[], long length) { long i; //i从最后一个父节点开始调整 for (i = length / 2 - 1; i >= 0; i--) { //形成最大堆,第一个元素为最大数。 maxHeap(array, i, length); } //将第一个元素放置到最后,再将前面的元素重新调整,得到最大堆,将此时最大的数放置到倒数第二 //位置,如此反复。 for (long i = length - 1; i > 0; i--) { std::swap(array[0], array[i]); maxHeap(array, 0, i); } }
void createMax(int arreglo[], int n) { for(int k = n/2; k >= 1; k--) { maxHeap(arreglo, k, n); } }
vector<double> medianSlidingWindow(vector<int>& nums, int k) { vector<double> result; if (k == 0 || nums.size() < k) return result; Heap minHeap(true), maxHeap(false); int i = 0; for (; i != k; ++i) { if (maxHeap.empty() || nums[i] > maxHeap.top().val) minHeap.insert(HeapNode(i, nums[i])); else maxHeap.insert(HeapNode(i, nums[i])); if (maxHeap.sIze() < minHeap.sIze()) { maxHeap.insert(minHeap.top()); minHeap.pop(); } else if (maxHeap.sIze() > minHeap.sIze() + 1) { minHeap.insert(maxHeap.top()); maxHeap.pop(); } } for (; i != nums.size(); ++i) { if (k & 1) result.push_back(maxHeap.top().val); else result.push_back(((double)maxHeap.top().val + minHeap.top().val) / 2.0); maxHeap.remove(i - k); minHeap.remove(i - k); if (maxHeap.empty() || nums[i] > maxHeap.top().val) minHeap.insert(HeapNode(i, nums[i])); else maxHeap.insert(HeapNode(i, nums[i])); if (maxHeap.sIze() < minHeap.sIze()) { maxHeap.insert(minHeap.top()); minHeap.pop(); } else if (maxHeap.sIze() > minHeap.sIze() + 1) { minHeap.insert(maxHeap.top()); maxHeap.pop(); } } if (k & 1) result.push_back(maxHeap.top().val); else result.push_back(((double)maxHeap.top().val + minHeap.top().val) / 2.0); return result; }
void heapSort(int arreglo[], int n) { createMax(arreglo,n); int i, temp; for (i = n; i >= 2; i--) { temp = arreglo[i]; arreglo[i] = arreglo[1]; arreglo[1] = temp; maxHeap(arreglo, 1, i - 1); } }
void maxHeap(int T[], int i) { l = left(i); r = right(i); p = parent(i); int max; if (l <= n && T[l] > T[i]) max = l; else max = i; if (r <= n && T[r] > T[max]) max = r; if (max != i){ swap(&T[i], &T[max]); maxHeap(T, max); } }
vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> result; if (k == 0 || nums.size() < k) return result; Heap maxHeap(false); int i = 0; for (; i != k; ++i) maxHeap.insert(i, nums[i]); for (; i != nums.size(); ++i) { result.push_back(maxHeap.top()); maxHeap.remove(i - k); maxHeap.insert(i, nums[i]); } result.push_back(maxHeap.top()); return result; }
void maxHeap(int arreglo[], int i, int n) { int l,r,largest,loc; l=2*i; r=(2*i+1); if((l<=n)&&arreglo[l]>arreglo[i]) largest=l; else largest=i; if((r<=n)&&(arreglo[r]>arreglo[largest])) largest=r; if(largest!=i) { loc=arreglo[i]; arreglo[i]=arreglo[largest]; arreglo[largest]=loc; maxHeap(arreglo, largest,n); } }
int main() { int a[20], b[20], n, i; printf("Enter the number of elements: "); scanf("%d", &n); printf("Enter the elements: "); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; maxHeap(a, i); } printf("\nOriginal Array\tHeap Array\n"); for (i = 1; i <= n; i++) printf("%d\t\t%d\n", b[i], a[i]); return 0; }
void buildMaxHeap(int T[]){ int i; for(i = n/2; i > 0; i--) maxHeap(T, i); }