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

  • Home
  • SEARCH
  • 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 8706181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:33:06+00:00 2026-06-13T03:33:06+00:00

So I have written a heap-sort program in C++ which takes in an array

  • 0

So I have written a heap-sort program in C++ which takes in an array of doubles and the size of the array and then sorts it. The program works however when I attempt to pass it arrays larger then 1000 I get “Bus error: 10” I think this has to do with how memory is being allocated, however I can not seem to find a solution.

    #ifndef _HEAPSORT_
    #define _HEAPSORT_

void Heapsort(double arrayToSort[], int sizeOfArray);

void Heapsort(double arrayToSort[], int sizeOfArray)
{

//      Building Heap:
// ==========================

int halfSize = sizeOfArray-1 / 2;

for(int i = halfSize; i >= 0; i--){
    double temp = arrayToSort[i];
    int I1 = i, I2 = i+i;
    do {
        if( I2 < sizeOfArray - 1 && arrayToSort[I2+1] > arrayToSort[I2] ) { I2++; }
        if( arrayToSort[I2] > temp ){
            arrayToSort[I1] = arrayToSort[I2];
            I1 = I2;
            I2 = I1+I1;
        } else {
            I2 = sizeOfArray;
        }
    } while ( I2 < sizeOfArray );
    arrayToSort[I1] = temp;
}

//      Sorting Heap:
// =========================

for(int i = sizeOfArray-1; i >= 2; i--){ // i is the number of still competing elements
    double temp = arrayToSort[i];
    arrayToSort[i] = arrayToSort[0]; // store top of the heap
    int I1 = 0, I2 = 1;
    do {
        if((I2+1) < i && arrayToSort[I2+1] > arrayToSort[I2] ) { I2++; }
        if(arrayToSort[I2] > temp ){
            arrayToSort[I1] = arrayToSort[I2];
            I1 = I2;
            I2 = I1+I1;
        } else {
            I2 = i;
        }
    } while( I2 < i );
    arrayToSort[I1] = temp;

}

double Temp = arrayToSort[1];
arrayToSort[1] = arrayToSort[0];
arrayToSort[0] = Temp;
}

#endif /* _HEAPSORT_ */

Any insight into how I can fix this would be greatly appreciated.
Here is the code where I allocate the memory.

#include <iostream>
#include "heapsort.h"
#include "rmaset.h"
#include "ranmar.h"
#include "common.h"


using namespace std;

int main(void)
{
const int size = 1000;
struct Common block;

rmaset(block);

double array[size];

for(int i = 0; i < size; i++){
    array[i] = ranmar(block);
}

Heapsort(array,size);


return 0;
}

This just creates a struct which then gets passed to a function which initializes it and then to another function ranmar which populates it with random numbers. I have checked all other functions thoroughly and am sure that the error is coming from the Heapsort function.

  • 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-06-13T03:33:07+00:00Added an answer on June 13, 2026 at 3:33 am

    In the following line int halfSize = sizeOfArray-1 / 2; the right side is evaluated as sizeOfArray-(1 / 2). The integer division (1 / 2) results in 0 so it initializes halfSize with the value sizeOfArray. You begin the loop off the end of the array. I think you meant to do (sizeOfArray-1) / 2 instead.

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

Sidebar

Related Questions

I have written a bash script which installs a number of packages, however for
I have written the following heap sort code and I get the wrong output
I have written some piece of code for my program in Matlab 7.10.0 which
I've written a simple application that works with database. My program have a table
I have written the following heap sort code and I get the wrong output
have written this little class, which generates a UUID every time an object of
I have written this code in JavaScript and works perfectly fine when I include
I have written a function that sorts a big scale of data. To test
I have written a java program generates lots of files (say txt files) in
I have written a raw binary image file into a buffer which is an

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.