int main() { int A[] = {8, 1, 3, 2, 16, 9, 10, 14, 4, 7}; int length = sizeof(A)/sizeof(int); printf("1. build a max heap\n"); printf("2. build a max heap and sort (heap sort)\n"); printf("3. build a min heap\n"); int input = 0; scanf("%d", &input); printA(A, length); switch(input) { case 1: build_max_heap(A, length); printA(A, length); break; case 2: build_max_heap(A, length); max_heapsort(A, length); printA(A, length); break; case 3: build_min_heap(A, length); printA(A, length); break; } return 0; }
void main(void) { int i; int arr[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}; int *heap_size = malloc(sizeof(int)); *heap_size = sizeof(arr) / sizeof(int) - 1; HEAP A = {arr, heap_size}; build_max_heap(A); heap_increase_key(A, 8, 15); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n%d\n", *(A.heap_size)); heap_extract_max(A); printf("%d\n", *(A.heap_size)); printf("\n"); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n"); A = max_heap_insert(A, 16); printf("%d\n", *(A.heap_size)); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n"); A = heap_delete(A, 3); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n"); }
/****************************************************************************** * MAIN ******************************************************************************/ int main(int argc, char * argv[]) { int A[MAX_NUM_NR]; char * filename_unsorted = get_filename(argc, argv); char * filename_sorted = get_sorted_filename(filename_unsorted); int n = get_A(A, MAX_NUM_NR, filename_unsorted); print_A(A, n, "original:"); heap_sort(A, n); print_A(A, n, "sorted:"); assert(verify_A(A, n, filename_sorted)); /* priority queue */ int heapsize = n; build_max_heap(A, heapsize); ______________________________(A, n, heapsize, "===============\\nafter build_max_heap\\n==============="); printf("heap_maximum: %d\n", heap_maximum(A, heapsize)); printf("heap_extract_max: %d\n", heap_extract_max(A, &heapsize)); print_A(A, heapsize, "after heap_extract_max"); ______________________________(A, n, heapsize, "===============\\nafter heap_extract_max\\n==============="); heap_increase_key(A, heapsize, 3, 88); print_A(A, heapsize, "after heap_increase_key"); ______________________________(A, n, heapsize, "===============\\nafter heap_increase_key\\n==============="); max_heap_insert(A, n, &heapsize, 97); print_A(A, heapsize, "after max_heap_insert"); ______________________________(A, n, heapsize, "===============\\nafter max_heap_insert\\n==============="); return 0; }
/* 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 main() { int data[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int size = 10; //int *data = malloc(sizeof(int)*size); struct heap *h; int new_data = 30; h = build_max_heap(data, size); dump_heap(h); free(h); h = build_min_heap(data, size/2, size); dump_heap(h); printf("extract min=%d\n", min_heap_extract(h)); dump_heap(h); printf("insert %d\n", new_data); min_heap_insert(h, new_data); dump_heap(h); free(h); //free(data); return 0; }
void heapsort(SequenceT* arr, size_t size, SortOrderT order) { if (arr == NULL || size <= 1) { return; } HeapT* h = create_heap_from(arr, size); assert(h != NULL); HeapImplT* heap = (HeapImplT*) h; void (*heapify)(HeapT*, size_t); if (order == DESC) { build_max_heap(h); heapify = max_heapify; } else { build_min_heap(h); heapify = min_heapify; } size_t index = size - 1; for (; index > 0; --index) { swap(&heap->data[index], &heap->data[0]); --heap->size; heapify(h, 0); } memcpy(arr, heap->data, size * sizeof(SequenceT)); destroy_heap(h); }
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 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); } }
/* * refer to pseudocode in page 160. */ int heap_sort(int *p_heap, int len_heap) { build_max_heap(p_heap, len_heap); for (int i = len_heap - 1; i >= 1; i--) { swap(&(p_heap[0]), &(p_heap[i])); max_heapify(i, p_heap, 0); } }
void build_heap(int n, int a[n]) { int i; for(i = (n-1)/2;i >= 0;i--) { build_max_heap(a, i, n-1); } }
void max_heapsort(int *A, int length) { for(int i = length - 1; i > 0; i--) { swap(A, 0, i); length = length - 1; build_max_heap(A, length); } }
void heap_sort(int *input, int max_len) { int len = max_len; while(len > 0) { build_max_heap(input, len); swap(input, input + len - 1); len--; } }
void heapsort(int a[]) { heap_size = MAX; build_max_heap(a); while (heap_size > 1) { swap(a, 0, heap_size -1); --heap_size; max_heapify(a, 0); } }
int findKthLargest(vector<int>& nums, int k) { build_max_heap(nums); for (int i = 0; i < k; i++) { swap(nums[0], nums[heap_size - 1]); heap_size--; max_heapify(nums, 0); } return nums[heap_size]; }
void kn_origin1(int A[], int n, int k) { build_max_heap(A, k); for(int i=k; i<n; i++) { if(A[i]<A[0]) { swap(A, i, 0); max_heapify(A, 0, k); } } }
void heapsort(int a[], size_t size) { build_max_heap(a, size); size_t heap_size = size; for (int i = size-1; i > 0; i--) { swap(a[0], a[i]); heap_size--; max_heapify(a, heap_size, 0); } }
void heapsort(int *a) {int c,i; build_max_heap(a); for(i=10;i>=2;i--) {c=a[1]; a[1]=a[i]; a[i]=c; hh=hh-1; maxheapify(a,1); }}
void heap_sort(int *a) { int i; build_max_heap(a); for(i=HEAP_SIZE;i>=2;i--){ exchange(&a[i],&a[1]); a[0]--; max_heapify(a,1); } }
void heap_sort(int A[], int size) { build_max_heap(A, size); int i = size-1; for (; i >= 1; i--) { my_swap(&A[0], &A[i]); max_heapify(A, 0, i); } }
int* heapSort(int* A, int n) { build_max_heap(A,n); int heapsize = n; for(int i = n-1; i >= 1; i--){ swap(A[0], A[i]); heapsize--; max_heapify(A,0,heapsize); } return A; }
void heap_sort(int *array, int len) { build_max_heap(array, len); for(int i = len - 1; i > 0; i--) { int temp = array [i]; array[i] = array[0]; array[0] = temp; max_heapify(array, i, 0); } }
void heap_sort(double* srcdata,int len){ int idx=0; build_max_heap(srcdata,len); for(idx=len-1;idx>0;idx--){ swap(srcdata,0,idx); len--; max_heapify(srcdata,len,0); } }
void clrs_heapsort(int A[]){ int seq_len = array_length(A); heap H = {A, seq_len, seq_len}; int i; build_max_heap(H); for (i = H.length - 1; i >= 1; i = i - 1) { exchange(&A[0], &A[i]); H.heap_size = H.heap_size - 1; max_heapify(H, 0); } }
void hsort(int a[], int p, int r) { build_max_heap(a, p, r); int j=0; for (int i=r; i>=p+1; i--) { swap(a[i],a[p]); j++; heapify(a, p, r-j, 0); } }
void heapsort(long long int *arr,long long int len) { long long int i; build_max_heap(arr,len); for(i= len-1;i>=1;i--) { swap(&arr[0],&arr[i]); heapsize = heapsize -1; max_heapify(arr,0); } }
void heapsort (int A[], int size){ int i, temp; build_max_heap(A, size); for (i = size; i >= 1; i--){ temp = A[i]; A[i] = A[0]; A[0] = temp; size--; max_heapify(A, 0, size); } return; }
// 堆排序参考算法导论伪代码 void heap_sort(int array[], int length) { build_max_heap(array, length); int heap_size = length; for (int i = length-1; i >= 1; --i) { swap(&array[0], &array[i]); --heap_size; max_heapify(array, heap_size, 0); } }
int main() { int i,n; struct element a[1010]; printf(" Enter initial Number of elements of queue : "); scanf("%d",&n); printf(" Enter n elements ( priority followed by name ) : \n"); for(i=1;i<=n;i++) { scanf("%d %s",&(a[i].priority),a[i].name); } build_max_heap(a,n); printf(" Constructed Priority Queue : \n"); for(i=1;i<=n;i++) { printf("%d %s \n",a[i].priority,a[i].name); } int option=0; while(1){ printf(" Options :\n\t1. InsertWithPriority\n\t2. RemoveTop\n\t3. Display Queue\n\t4. Exit\n Enter Option : "); scanf("%d",&option); if(option==1) { n++; printf(" Enter priority and name : "); struct element temp; scanf("%d %s",&(temp.priority),temp.name); insert_with_priority(a,n,temp); //build_max_heap(a,n); printf(" Inserted to queue \n"); } else if(option==2) { printf(" Removed element : %d %s\n",a[1].priority,a[1].name); a[1].priority=-1; swap(a+1,a+n); n--; max_heapify(a,1,n); } else if(option==3) { printf(" Current Priority Queue : \n"); for(i=1;i<=n;i++) { printf("%d %s \n",a[i].priority,a[i].name); } } else break; } }
void heap_sort(int a[],int n) { int i,temp; build_max_heap(a,n); for(i=n;i>1;i--) { temp=a[1]; a[1]=a[i]; a[i]=temp; count+=3; max_heapify(a,1,i-1); } }
void heap_sort(int *array, int size) { if (1 == size) { return; } build_max_heap(array, size); for (int i = size-1; i > 0; --i) { std::swap(array[0], array[i]); max_heapify(array, 0, i); } }