Thread: Heap max and insert sort

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    11

    Question Heap max and insert sort

    Hello again,

    I am working on a heap program :)

    The code will let the user insert numbers and then print them in order, starting with the largest number. In the end, the heap will be empty once the last number is printed.

    My problem is, that the heap is not sorted. The parent is supposed to be greater than the children. People tell me that the swap is not working:

    Code:
    void heapify(heap* h, size_t i)
    { 
        if (largest != i)
        {
            int tmp = h -> elements[i];
            h -> elements[i] = h -> elements[largest];
            h -> elements[largest] = tmp;
            heapify(h, largest);
        }
    }
    I feel pretty stupid for not seeing it but I don't know what I am doing wrong in this section.

    Also, it doesn't print the largest number. That would be this section:
    Code:
    int heap_extract_max(heap* h) 
    {
        int max;
        if (h -> size < 1)
        {
            printf("Heap is empty!\n");
            return(-1);
        }
        else
        {
            max = h -> elements[0];
            h -> elements[0] = h -> elements[h -> size[h -> elements]];
            h -> size[h -> elements] = h -> size - 1;
            heapify(h, 0);
            return max;
        }
    }
    I am happy about any kind of help! :)
    Feel free to ask me about other parts of the code. I didn't want to post the whole code so I tried to minimize it on the problems but as a newbie I could be wrong of course.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Short, Self Contained, Correct Example
    The most useful thing to post is a minimal complete program that still has the problem you're trying to identify.

    As it is, there is a whole bunch of stuff we could only guess at.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    11
    I am honestly not good enough to just write a minimal program with the same error.
    You mean, you need more of the code? Probably even the whole code?
    I know for sure that the problem is in my extract_max function, I thought it was enough to post it :/

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    How can "size" be both an array and a integer?

    You need to at least post the "heap" datatype for anyone to have a chance to help you.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jan 2018
    Posts
    11
    I was reading the website that you linked and I feel like it's impossible for me to do this. I was given lots of code but I only have to implement the few functions. The other functions, which were already given, are functions that I cannot understand yet with my level of knowledge. I can't just delete something in there because I don't know what they are doing.... it's like twenty pages of code so I tried to stick to my own functions.
    Everything would be much easier for me if I could just write my own code from the beginning.

  6. #6
    Registered User
    Join Date
    Jan 2018
    Posts
    11
    You mean this?

    Code:
    struct heap {
        int* elements;
        size_t size;
        size_t capacity;
    };

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The way you make it minimal is
    1. You make a new copy of your code for the purposes of posting a minimal example.
    2. You start deleting lines / functions which don't impact on the specific problem you're facing.

    > I was given lots of code but I only have to implement the few functions.
    Mmm.

    > I can't just delete something in there because I don't know what they are doing....
    Deleting stuff is a good way to find out what breaks when you do it.
    You're likely to need some idea of what the rest of the code does if you're going to implement some code - if only to make sure you don't mess things up.

    > it's like twenty pages of code so I tried to stick to my own functions
    *shrug*
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by ma_ry View Post
    You mean this?

    Code:
    struct heap {
        int* elements;
        size_t size;
        size_t capacity;
    };
    Since, size is not an array why are you using it like it is one?

    Code:
    size[h -> elements]
    Edit: elements is an pointer; often it makes sense to use a pointer like it is an array.

    Edit2: Did you mean to write this code? It makes no sense but it is better than what you have.

    Code:
    h -> elements[size]
    Tim S.
    Last edited by stahta01; 01-28-2018 at 03:48 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-12-2015, 11:54 AM
  2. heap sort
    By lucaspewkas in forum C Programming
    Replies: 6
    Last Post: 05-02-2005, 04:20 AM
  3. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM
  4. heap sort
    By stseitli in forum C Programming
    Replies: 1
    Last Post: 07-09-2002, 09:32 AM
  5. insert sort
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-04-2002, 01:27 AM

Tags for this Thread