Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:05:46+00:00 2026-05-30T00:05:46+00:00

I am attempting to implement Heap sort in my program to learn more about

  • 0

I am attempting to implement Heap sort in my program to learn more about sorting algorithms. However I am running in to an issue.

I call Heap sort in main like this:

Main:

heap_sort(h_vector);

Where h_vector is a randomly sized vector with random ordered elements. My heap sort algorithm looks like.

Heap Sort:

void max_heapify(std::vector<int>& v, int i)
{
    int left = i + 1, right = i + 2;
    int largest;

    if( left <= v.size() && v[left] > v[i])
    {
        largest = left;
    }

    else
    {
        largest = i;
    }

    if( right <= v.size() && v[right] > v[largest])
    {
        largest = right;
    }

    if( largest != i)
    {
        std::swap(v[i], v[largest]);
        max_heapify(v,largest);
    }



}

void build_max_heap(std::vector<int>& v)
{
    for( int i = v.size() - 2; i >= 0; --i)
    {
        max_heapify(v, i);
    }

}

void heap_sort(std::vector<int>& v)
{
    build_max_heap(v);
    int x = 0;
    int i = v.size() - 1;
    while( i > x)
    {
        std::swap(v[i],v[x]);
        ++x;
        --i;
    }

}

Whenever I add this sort to my program I get the following error.

Error:

*** glibc detected *** ./a.out: free(): invalid next size (normal): 0x096c82d0 ***

I am not sure what could be causing this. I thought at first that my alogrithm might be going out of bounds of the vector but I have checked a few times and I do not see where. Any ideas? Thanks for your help in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:05:49+00:00Added an answer on May 30, 2026 at 12:05 am

    At the very first invokation of max_heapify(), you invoke it with i = v.size() - 2

    Thus, when you set right = i + 2; you actually set: right = v.size()

    Now, look at this:

     if( right <= v.size() && v[right] > v[largest])
    

    Note that right <= v.size(), and you are now trying to access v[right], which is out of bound.

    Note that the last index of v is v[v.size() -1] – so all your if statements should be right < v.size() [instead <=]

    I assume solving these issues will solve your bug eventually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to implement a poor man's test of whether a process is still running
I'm attempting to implement a custom search dialog in a WPF program. The Parent
I'm attempting to implement a 'Requirements' type system into a program. It seemed pretty
I'm currently attempting to implement having a (signed) applet communicate to a server program
I'm attempting to implement the Tcl dictionary sort in Perl in order to order
I am attempting to implement the Visitor Design Pattern using OCaml's OO constructs and
I'm attempting to implement nested object forms for my site, using Ryan Daigle's blog
I am currently attempting to implement a custom gridview interface to display data from
I'm attempting to implement a Patricia Trie with the methods addWord() , isWord() ,
I'm attempting to implement a delegate Service provider by overriding the bean definition for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.