I have this array which has a max heap property. The time complexity of deleteMax is O(logn). If the code below will iterate for only 7 times, what would be the time complexity of the below code (big O)?
int heap_size = 15;
int i, value, heap_array[]; // array is in the form of max heap
....
for(i = 0; i < 7; i++){ // iterates seven times
value = deleteMax(heap_array);
printf("%d ", value);
}
I have two solutions in my mind. First: The time complexity is O(7 * logn) or simply O(logn).
Then the second one is O(1/2 * n * logn) or O(nlogn) since 1/2 is just a constant and I'm assuming that since the number of iteration is 7 which is almost the same as the half the heap_size, I can disregard 1/2. Thus O(nlogn)
Which one would be correct?
n/2
times, that's more interesting. – xiaofeng.li