Max Heap Adjustment and Heap Sort
Time limit: 500ms
Memory limit: 256mb
Description:
Please complete the missing code of the max heap ADT, such that the program could output a correct answer.
1. isEmpty
2. isFull
3. heapAdjust
4. heapSort
-------------------------Copy the following code, complete it and submit-------------------------
/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Heap {
int * arr;
int capacity;
int size;
};
struct Heap * createHeap(int capacity) {
struct Heap * heap = (struct Heap *)malloc(sizeof(struct Heap));
heap->arr = (int*)malloc(sizeof(int) * capacity);
heap->capacity = capacity;
heap->size = 1;
return heap;
}
int isFull(struct Heap * heap) {
// write down your code here
}
int isEmpty(struct Heap * heap) {
// write down your code here
}
void printHeap(struct Heap * heap) {
int i;
for (i = 1; i < heap->size - 1; i++) {
printf("%d, ", heap->arr[i]);
}
printf("%d\n", heap->arr[i]);
return;
}
void heapAdjust(struct Heap* heap, int nodeid, int heapsize){
// write down your code here
}
void heapSort(struct Heap* heap, int n){
// write down your code here
}
// DO NOT MODIFY THE CODE BELOW
int main(void)
{
int n;
scanf("%d", &n);
struct Heap * heap = createHeap(n+1);
for(int i = 1; i <= n; i++){
scanf("%d", &heap->arr[i]);
heap->size++;
}
for(int i = n / 2; i >= 1; i--){
heapAdjust(heap, i, n);
}
printHeap(heap);
heapSort(heap, n);
printHeap(heap);
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Input:
The first line contains a non-negative integer N;
Then N lines of heap elements.
Output:
1. The max heap array after heap adjustment from n/2, separated by a comma between every two elements.
2. The sorted array of heap elements by heap sort, separated by a comma between every two elements.
Sample Input 1:
6
6
1
2
4
5
7
Sample Output 1:
7, 5, 6, 4, 1, 2
1, 2, 4, 5, 6, 7
Submit