void heap_sort(int array[], int length) { int i; for(i = length/2; i > 0; i--) max_heapify(array, length, i); for(i = length-1; i > 0; i--) { exchange(&array[0], &array[i]); max_heapify(array, i, 1); } }
void max_heapify(void *array, int i, int32_t nelem, size_t elem_size, int32_t offset, int i_type) { void *largest = array + i * elem_size; // if it's STRUCT,THEN largest + offset is the CMP-VALUE! void *pswap = malloc(elem_size); int new_index; printf("\nARGUMENT CHECK\n array = %08x\ni = %d\nnelem = %d\nelem_size = %d\noffset = %d\ni_type = %d\n", (uint32_t)array,i,nelem,elem_size,offset,i_type); if(pswap == NULL) printf("%s Out Of Memory!\n",__func__); if((LEFT(i) < nelem) && ((valcmp(i_type, largest + offset, array + (LEFT(i)) * elem_size + offset)) < 0)) { largest = array + LEFT(i) * elem_size; new_index = LEFT(i); } if((RIGHT(i) < nelem) && ((valcmp(i_type, largest + offset, array + (RIGHT(i)) * elem_size + offset)) < 0)) { largest = array + RIGHT(i) * elem_size; new_index = RIGHT(i); } if(largest != array + i * elem_size) { memmove(pswap, largest, elem_size); memmove(largest, array + i * elem_size, elem_size); memmove(array + i * elem_size, pswap, elem_size); free(pswap); max_heapify(array, new_index, nelem, elem_size, offset, i_type); } }
void build_max_heap(int a[],int n) { int i; count++; for(i=n/2;i>=1;i--) max_heapify(a,i,n); }
int extract_max (int a[], int *n) { int max = a[0]; a[0] = a[*n-1]; (*n)--; max_heapify (a,*n,0); return max; }
/****************************************************************************** * MaxHeapify, BuildMaxHeap, HeapSort ******************************************************************************/ void max_heapify(int * A, int Asize, int heapsize, int i) { static int depth = 0; for (int k = 0; k <= depth; k++) printf("\t"); printf("A[%d]: %d\n", i, A[i]); depth++; int max; int l = LEFT(i); int r = RIGHT(i); if (l < heapsize && A[l] > A[i]) max = l; else max = i; if (r < heapsize && A[r] > A[max]) max = r; if (max != i) { swap(&A[max], &A[i]); /* ______________________________(A, Asize, heapsize, "after MAX-HEAPIFY::swap\\n%2d<->%2d\\nA[%2d]<->A[%2d]", A[max], A[i], max, i); */ max_heapify(A, Asize, heapsize, max); } depth--; }
/* * refer to pseudocode in page 157. */ int build_max_heap(int *p_heap, int len_heap) { for (int i = len_heap / 2 - 1; i >= 0; i--) { max_heapify(len_heap, p_heap, i); } return 0; }
// 堆排序参考算法导论伪代码 void max_heapify(int array[], int heap_size, int index) { int left_index = 2*index; int right_index = 2*index+1; int largest_index = 0; if (left_index < heap_size && array[left_index] > array[index]) { largest_index = left_index; } else { largest_index = index; } if (right_index < heap_size && array[right_index] > array[largest_index]) { largest_index = right_index; } if (largest_index != index) { swap(&array[index], &array[largest_index]); max_heapify(array, heap_size, largest_index); } }
/* 此函数把一颗二叉树中以node为根的子树变成最大堆。 * 注意: 使用的前提条件是 node节点的左右子树(如果存在的话)都是最大堆。 * 这个函数是整个算法的关键。 */ void max_heapify(int heap[], int heap_size, int node) { // 这里先不考虑整数溢出的问题 // 先把注意力放在主要的功能上 // 如果数据规模够大,int类型必然会溢出 int l_child = LEFT(node); int r_child = RIGHT(node); int max_value = node; if (l_child < heap_size && heap[l_child] > heap[max_value]) { max_value = l_child; } if (r_child < heap_size && heap[r_child] > heap[max_value]) { max_value = r_child; } if (max_value != node) { swap_val(heap + node, heap + max_value); // 之后还要保证被交换的子节点构成的子树仍然是最大堆 // 如果不是这个节点会继续"下沉"到合适的位置 max_heapify(heap, heap_size, max_value); } }
void build_max_heapify(int arr[], int heap_size) { int i; for (i = (heap_size - 1) / 2; i >= 0; i--) max_heapify(arr, i, heap_size); }
void build_max_heap(int heap[]) { int i; heap_size = dim; for(i= dim/2 ; i>0;i--) max_heapify(heap,i); }
// Max_Heapify 함수 void max_heapify(heap_t* heap, int index) { int temp = 0; if ((heap == NULL) || (heap->element == NULL) || (index < 1)) { printf("heap pointer error or index error.\n"); return; } int left = 2*index; int right = 2*index + 1; int largest = index; if (left <= heap->size && heap->element[left] > heap->element[index]) { largest = left; } if (right <= heap->size && heap->element[right] > heap->element[largest]) { largest = right; } if (largest != index) { temp = heap->element[index]; heap->element[index] = heap->element[largest]; heap->element[largest] = temp; max_heapify(heap, largest); } }
void build_max_heap(int A[], int size) { my_swap(&A[0], &A[size-1]); int i = size/2; for (; i >= 0; i--) max_heapify(A, i, size); }
int max_heapify(void *array, int size, int i, int no_elem, compare_pr compare) { int left = left_heap(i); int right = right_heap(i); int heap_size = no_elem - 1; //ie., heap is from array[0..no_elem - 1] int largest_child = i; if(left <= heap_size && compare(array+size*(left), array+size*(i)) > 0) largest_child = left; if(right <= heap_size && compare(array+size*(right), array+size*(largest_child)) > 0) //right is the bigger of one of node, else left is bigger largest_child = right; if(largest_child != i) { swap_void(array, i, largest_child, size); max_heapify(array, size, largest_child, no_elem, compare); } return 0; }
void put_median(int num) { int bal=check(); if(bal>=1) { if(num>=median) { insert_min(num); } else { insert_min(maxheap[1]); maxheap[1]=maxheap[maxheap[0]]; maxheap[0]--; max_heapify(1); insert_max(num); } } else { if(num>median) { insert_min(num); insert_max(minheap[1]); minheap[1]=minheap[minheap[0]]; minheap[0]--; min_heapify(1); } else { insert_max(num); } } return; }
void max_heapify(int index) { int left_child_index,right_child_index,largest,temp; left_child_index = ((2*index)-1); // -1 since array index starts from zero and here 1 is assumed to be starting index right_child_index = (((2*index)+1)-1); if ( left_child_index<=size-1 && ( array[left_child_index] > array[index-1] ) ) largest = left_child_index; else largest = index -1 ; if ( right_child_index<=size-1 && ( array[right_child_index] > array[largest] ) ) largest = right_child_index ; if ( largest != index-1 ) { temp = array[index-1]; array[index-1] = array[largest]; array[largest] = temp; max_heapify(largest+1); } }
void max_heapify(int a[], int i) { int largest; int temp; int l = 2*i; int r = 2*i + 1; if(l <= heapsize && a[l] > a[i]) { largest = l; } else { largest = i; } if(r <= heapsize && a[r] > largest) { largest = r; } if(largest != i) { temp = a[i]; a[i] = largest; largest = temp; max_heapify(a, largest); } }
/* heap sort主函数 */ void heap_sort(int heap[], int heap_size) { if (heap == NULL || heap_size < 2) { return; } //构建最大堆 build_max_heap(heap, heap_size); int i; for (i = heap_size - 1; i > 0; i--) { /* 把当前堆的最大值(heap[0])交换到末尾 * 相当于取出最大值,堆的规模变小。 * 交换后的堆不是最大堆,但是根的两颗子树都是最大堆 * 满足调用max_heapify的要求。 */ swap_val(heap, heap + i); heap_size--; //保持最大堆 max_heapify(heap, heap_size, 0); } }
int max_heapify(int* array, int i, int array_size) { int l = 0; int r = 0; int largest = 0; int temp = 0; l = get_left_index(i); r = get_right_index(i); //printf("array[%d] = %d, array[%d] = %d\n", l, array[l], i, array[i]); if (l < array_size && array[l] > array[i]){ largest = l; } else { largest = i; } if (r < array_size && array[r] > array[largest]){ largest = r; } if (largest != i){ temp = array[i]; array[i] = array[largest]; array[largest] = temp; max_heapify(array, largest, array_size); } return 1; }
void build_max_heap(int *a) { int i; a[0]=HEAP_SIZE; for(i=HEAP_SIZE/2;i>=1;i--) max_heapify(a,i); }
void build_max_heap(heap_t heap) { int i = 0; for (i = heap.heap_size/2; i>=1; i--) { max_heapify(heap, i); } }
//------------------------------------------------------------------------------ int decrease_key_max_heap(int *heap,int n,int i,int nw) { if(nw>heap[i]) return 0; heap[i]=nw; max_heapify(heap,i,n); return 1; }
void max_heapify( heap_t* heap , int pos ) { //예외처리 if( heap == NULL || heap->size < pos || pos == 0) return; int leftPos = 0 , rightPos = 0; //우선 maxPos를 검사 노드의 위치값으로 int maxPos = pos; //왼쪽 자식이 있으면 왼쪽자식과 비교하여 maxPos갱신 if( heap->size > pos * 2 ) { leftPos = pos * 2; if( heap->element[leftPos] > heap->element[maxPos] ) maxPos = leftPos; } //오른쪽 자식이 있으면 오른쪽 자식과 비교하여 maxPos갱신 if( heap->size > pos * 2 + 1 ) { rightPos = pos * 2 + 1; if( heap->element[rightPos] > heap->element[maxPos] ) maxPos = rightPos; } //maxPos가 갱신되었으면 pos와 maxPos에 있는 원소 스왑후에 //maxPos위치에서부터 다시 heapify적용 if( maxPos != pos ) { swap( heap , maxPos , pos ); return max_heapify( heap , maxPos ); } }
void heap_sort(int a[], int n) { build_max_heap(a, n); for (int i = n-1; i > 0; --i) { swap(&a[0], &a[i]); max_heapify(a, i, 0); } }
void build_max_heap(HEAP A) { int i; for (i = *(A.heap_size) / 2; i >= 0; i--) max_heapify(A, i); }
int find_min_k_nrs_by_heap_sort(int *array, int size, int k, int *result) { assert(array && result); if (k <= 0) { return 0; } if (k >= size) { memcpy(result, array, size*sizeof(*array)); return size; } build_max_heap(array, k); for (int i = k; i < size; ++i) { if (array[i] < array[0]) { std::swap(array[i], array[0]); max_heapify(array, 0, k); } } memcpy(result, array, k*sizeof(*array)); return k; }
void build_max_heap() /* O(nlgn),actually is O(n) P78*/ { int i; heap_len = len; for (i = len/2; i>0; i--) max_heapify(a,i); }
int main(){ int i,n,*a; scanf("%d",&n); a=(int*)malloc(n*sizeof(int)); for(i=0;i<n;i++) scanf("%d",&a[i]); size=n; buildHeap(a); for(i=n-1;i>=1;i--){ SWAP(a[0],a[i]); size--; max_heapify(a,0); } printf("Sorted: "); for(i=0;i<n;i++){ printf("%d ",a[i]); } }
/* max heapify for all non-leaf nodes * is building max heap */ void build_max_heap(int *a, int n) { int i; for (i = n/2 - 1; i >= 0; i--) max_heapify(a, n, i); }
void build_max_tree(int *array,int n) { int i; for (i = n/2; i >= 0; i--) { max_heapify(array,i,n); } }
void build_max_heap(double* srcdata,int len){ int idx=0; for(idx=(int)(floor(len/2));idx>=0;idx--){ max_heapify(srcdata,len,idx); } }