4

Function 1

void min_heapify(int arr[],int n, int i){
    int j, temp;
    temp = arr[i];
    j = 2 * i;

    while (j <= n)
    {
        if (j < n && arr[j+1] < arr[j])
            j = j + 1;
        if (temp < arr[j])
            break;
        else if (temp >= arr[j])
        {
            arr[j/2] = arr[j];
            j = 2 * j;
        }
    }

    arr[j/2] = temp;
}

Function 2

void max_heapify(int arr[], int n, int i)    
{
    int largest = i;  // Initialize largest as root
    int l = 2*i + 1;  // left = 2*i + 1
    int r = 2*i + 2;  // right = 2*i + 2

    // If left child is larger than root
    if (l < n && arr[l] < arr[largest])
        largest = l;

    // If right child is larger than largest so far
    if (r < n && arr[r] < arr[largest])
        largest = r;

    // If largest is not root
    if (largest != i)
    {
        swap(arr[i], arr[largest]);

        // Recursively heapify the affected sub-tree
        heapify(arr, n, largest);
    }
}

Problem Details

Here the heapification work the same way to make a min_heap but the problem is, I used heap in this below problem to solve it but unfortunately function 2 which I implemented by watching MIT lecture didn't work for this problem, after looking some time in the web I found the 1st function which worked seamlessly for this problem. I'm just confused are not they the same function? ------

Problem

Yup!! The problem name reflects your task; just add a set of numbers. But you may feel yourselves condescended, to write a C/C++ program just to add a set of numbers. Such a problem will simply question your erudition. So, let’s add some flavor of ingenuity to it.

Addition operation requires cost now, and the cost is the summation of those two to be added. So, to add 1 and 10, you need a cost of 11. If you want to add 1, 2 and 3. There are several ways –

1 + 2 = 3, cost = 3
1 + 3 = 4, cost = 4
2 + 3 = 5, cost = 5
3 + 3 = 6, cost = 6
2 + 4 = 6, cost = 6
1 + 5 = 6, cost = 6
Total = 9
Total = 10
Total = 11

I hope you have understood already your mission, to add a set of integers so that the cost is minimal.

Input

Each test case will start with a positive number, N (2 ≤ N ≤ 5000) followed by N positive integers (all are less than 100000). Input is terminated by a case where the value of N is zero. This case should not be processed.

Output

For each case print the minimum total cost of addition in a single line.

SampleInput

3    
1 2 3    
4    
1 2 3 4    
0    

SampleOutput

9
19
  • 1
    Possibly related: Think hard about what happens with the *second* part of `(j < n && arr[j+1] < arr[j])` does when the first part is true for `j = (n-1)`. If `n` is a *magnitude* (number of elements) that would be bad. (but then again, so would `while (j <= n)`). – WhozCraig Apr 15 '16 at 08:13
  • 1
    Are you making max heapify or min heapify for the second function? – fluter Apr 15 '16 at 08:19
  • Both were min_heapify, ans it's solved now thanks anyway... – matha_kharap Apr 17 '16 at 12:20

2 Answers2

0

There is a problem with the swap function in function2.

C is call by value, so

swap(arr[i], arr[largest]);

cannot swap values in the array.

A swap function needs the addresses of the values to swap:

swap(int *v1, int *v2) {
    int tmp = *v1;
    *v1 = *v2;
    *v2 = tmp;
}

And the call would be:

swap(&arr[i], &arr[largest]);
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • the sorting of integer number is not working correctly for this above problem in function 2. like if I enter 5 numbers 1 2 3 4 5 - (this output is coming from the function 1 as I put a print function inside this function) 1 2 3 5 4, 1 2 3 5 4 , 2 4 3 5 , 3 4 3 5 , 3 4 5 , 4 6 5 , 5 6 , 6 9 , 9 , 15 , Here is the output for the function 2- 1 2 3 5 4 , 1 2 3 5 4 , 2 4 3 5 , 2 4 3 5 , 3 4 3 5 , 4 5 3 , 4 5 3 , 5 7 3 , 5 7 3 , 3 7 , 8 7 , 7 , 15, the output from the function 1 is working as the way it should work, but other one is not. – matha_kharap Apr 15 '16 at 11:18
  • How is swapping working? I don't understand how it could possibly work correctly. – Klas Lindbäck Apr 15 '16 at 11:21
0

Ok I find out the solution there was a mistake in the condition check, in the if condition where we checking that if (left <= n) this was previously (left < n) this why it was not working for that problem. ok thank you.

void min_heapify(int arr[],int n, int i){    
    int lowest = i; // Initialize lowest as root
    int left = 2*i ;
    int right = 2*i + 1;



 // If child is lower than root
    if(left <= n && arr[left] < arr[lowest]){
        lowest = left;
    }

    // If right child is lower than lowest
    if(right <= n && arr[right] < arr[lowest]){
        lowest = right;
    }
    // If lowest is not root
    if(lowest != i){ // also break condition
        swap(arr[i], arr[lowest]);

        //Recursively heapify
        min_heapify(arr, n, lowest);

    }