There's lots of issues with your code.
First, you declare a global int length variable, but you never assign it any value. So the length is always ZERO.
In BuildHeap() you do:
for (i = floor(length/2); i <= 1; i--) ...
which starts with floor(0/2) that is with i==0. That value is less than 1 so the loop makes the first iteration and decrements i with the i-- expression — and i becomes minus one. Then the loop proceeds until the i value wraps over zero to become positive (and greater than 1). For 32-bit integers it would take about 2.14 billion iterations...
The very first thing your main() does is invoking BuildHeap() which in turn invokes (multiple times) Heapify(). The latter compares array items to decide if they should get swapped – but they were not assigned any values! You never fill the A array with any data...
Did you notice that you compare and swap items which are outside your A array (actually, before it), since BuildHeap() decrements i below zero and passes negatve index values to Heapify()...?
Your Heapify routine performs at most one swap in the array. It doesn't seem sufficient. Suppose the heap is already 3 levels deep, and the current item appears the smallest one in the heap; it should go to the deepest level, but how do you achieve that? A single swap moves the item just one level downwards.
This is how you could do it.
First, decide the maximum size of data you will handle and prepare the array:
#define LENGTH 100
int A[LENGTH + 1];
(You need 'plus one' because A[0] will not be a part of the heap — why?)
Then prepare variables to store an actual size of data:
int dataSize;
int heapSize;
and a routine to fill the array. You can read the user input or load data from a file, or just fill the array by some algorithm.
void FillArrayAscending() {
dataSize = 16;
for(int i = 1; i <= dataSize; i ++)
A[i] = i;
}
void FillArrayDescending() {
dataSize = 25;
for(int i = 1; i <= dataSize; i ++)
A[i] = 50 - i;
}
void FillArrayCrossed() {
dataSize = 20;
for(int i = 1; i <= dataSize; i += 2)
A[i] = 10 + i;
for(int i = 2; i <= dataSize; i += 2)
A[i] = 30 - i;
}
Then you can 'heapify'. To do that you sift the item down the heap – you iterate swapping until the item reaches its place:
void SiftDown(int i) {
while(i < heapSize) {
int largest = i;
if(2*i <= heapSize && A[i] < A[2*i])
largest = 2*i;
if(2*i + 1 <= heapSize && A[largest] < A[2*i + 1])
largest = 2*i + 1;
if(largest == i)
break; // the item reached its place
A[0] = A[largest];
A[largest] = A[i];
A[i] = A[0];
i = largest; // new position to sift down...
}
}
i indicates the item to be sifted down, largest denotes a swap destination, A[0] can be used as a temporary storage (why?).
Now you can build a heap in the array:
void BuildHeap() {
heapSize = dataSize;
for(int i = heapSize/2; i >= 1; i --)
SiftDown(i);
}
and retrieve items from a heap in a descending order:
void ExtractHeap() {
while(heapSize > 1) {
A[0] = A[1]; // take the max item from a heap
A[1] = A[heapSize]; // replace it with the last one
A[heapSize] = A[0]; // add the max one to the sorted part
heapSize --;
SiftDown(1); // re-heapify
}
}
Now you can do the whole processing:
int main(int, char**)
{
FillArrayAscending(); // or Descending or...
BuildHeap();
ExtractHeap();
// and here you can print the array contents
// for indices 1 .. dataSize
// to verify it is actually sorted
}
heapSizeaconst int, and use local variables to do the work.lengthever change? Also, you don't need to dofloor(i/2)like in Javascript when you use integers: integer division truncates, soi / 2is enough.