Hello, all:
I wrote a function to implement a 4-heap with 100 data in it. But it cannot work correctly.
the step is: first, read the data from a file and put them in to an array one by one. Second, use the percolate down (slide) algorithm to adjust the heap, and the relationship between the parents and the children is "the left most child = parent * 4 - 2", so the function is (without the data-read part):
Code:
void
Im4Heap(int si, int i, PriorityQueue H)
{
int temp, num, pointer, child, start, end;
temp = H->Elements[i];
H->Size = si;
for (i; i * 4 - 2 <= H->Size; i = child)
{
start = i * 4 - 2;
end = i * 4 + 1;
child = i * 4 - 2;
num = Smallest(&H->Elements[start], &pointer, start, end);
if (temp > num)
H->Elements[i] = H->Elements[pointer];
else
break;
}
H->Elements[i] = temp;
}
The "Smallest" function is used for finding the smallest value in an array:
Code:
int
Smallest(int array[], int *i, int start, int end)
{
int num;
num = array[start];
for(*i = start; *i <= end; *i++)
{
if(array[*i] < num)
num = array[*i];
}
printf("%d\n", num);
return num;
}
Thank you very much!