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 8168831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:39:28+00:00 2026-06-06T20:39:28+00:00

I’m working on the first programming homework on Coursera’s Algorithms: Design and Analysis course

  • 0

I’m working on the first programming homework on Coursera’s Algorithms: Design and Analysis course ( https://www.coursera.org/course/algo ).
It involves using merge sort to count inversions ( http://en.wikipedia.org/wiki/Inversion_(discrete_mathematics) ). I thought this would be a relative no-brainer because I’ve encountered merge sort before (in school).

#include <iostream>
#include <fstream>

using namespace std;

int *half(int *array, int n, int start, int end)
{
    /*
     * Creates a new array which contains elements from ''array'' starting with ''start''
     * and ending with ''end - 1''.
     */

     int *new_array = new int[end-start];

     for(int i = start; i < end; i++)
     {
        new_array[i-start] = array[i];
     }

     return new_array;
}

int merge(int *array1, int n1, int *array2, int n2, int *new_array)
{
    /*
     *  Merges arrays 1 and 2 (with lengths n1 and n2) into a new_array, counting
     *  ''split inversions'' by the way.
     */
    int i = 0, j = 0, count = 0;

    for(int k = 0; k < n1 + n2; k++)
    {

         if(i >= n1)
        {
            new_array[k] = array2[j];
            j++;
            continue;
        }

         if(j >= n2)
        {

            new_array[k] = array1[i];
            i++;
            continue;
        }

        if( array1[i] <= array2[j] )
        {
            new_array[k] = array1[i];
            i++;
        }
        else
        {
            new_array[k] = array2[j];
            j++;
            count++;
        }
    }

    return count;
}

int mergesort(int *array, int n)
{

    if(n == 1) return 0; //base case

    int x, y, z;
    int odd;

    if(n%2 == 0) odd = 0;
    else odd = 1;

    int *half1 = new int [n/2];
    int *half2 = new int [n/2 + odd];

    half1 = half(array, n, 0, n/2);
    half2 = half(array, n, n/2, n);

    x = mergesort(half1, n/2);
    y = mergesort(half2, n/2 + odd);  //if n is odd, we add one
    z = merge(half1, n/2, half2, n/2 + odd, array); //we write a sorted array back in our starting array

    delete [] half1;
    delete [] half2;

    return x + y + z;
}

int main()
{
    int n;
    int *array = new int[n];

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        array[i] = x;
    }

    for(int i = 0; i < n; i++)
        cout << array[i] << " ";

    cout << endl;
    cout << "Number of inversions: " << mergesort(array, n) << endl;

    for(int i = 0; i < n; i++)
    cout << array[i] << " ";

    cout << endl;
    delete[] array;

    return 0;
}

So, what is so weird here?
First thing: For me, it works perfectly for some arrays, but crashes for other arrays (examples later).
Second thing: I sent code to my friend who said that everything is working fine for him, even the examples that crash dramatically for me.

So, examples:

For array [1 2 3 4 5 6 7] g++ produces this:

malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted (core dumped)

When I ”backtrack” it using gdb:

#0  0x00007ffff7753445 in __GI_raise (sig=<optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1  0x00007ffff7756bab in __GI_abort () at abort.c:91
#2  0x00007ffff779abed in __malloc_assert (assertion=<optimized out>, file=<optimized out>, line=<optimized out>, function=<optimized out>) at malloc.c:300
#3  0x00007ffff779e0f4 in sYSMALLOc (av=0x7ffff7ad3720, nb=32) at malloc.c:2448
#4  _int_malloc (av=0x7ffff7ad3720, bytes=12) at malloc.c:3892
#5  0x00007ffff779fa45 in __GI___libc_malloc (bytes=12) at malloc.c:2924
#6  0x00007ffff7b8fded in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007ffff7b8ff09 in operator new[](unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x0000000000400b12 in mergesort (array=0x603010, n=7) at jedan.cpp:81
#9  0x0000000000400cfe in main () at jedan.cpp:120

It does something similar (but not the same!) for the array [1 2 3 4 5 6 7 8 9 10], again connected to the new and delete[] functions. I can post it here later if somebody thinks that would be helpful, but I don’t want to bloat this post too much.
And it works for most of the arrays I tried (I had no problems with arrays of size <= 6, and for a pretty big number of bigger arrays).

I’m using Ubuntu 12.04, installed yesterday… pretty clean and fresh.
Help?

P.S. If you find variable names a little weird, sorry… I translated them from my native language so the code could be more readable.

  • 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-06T20:39:29+00:00Added an answer on June 6, 2026 at 8:39 pm
    int n;
    int *array = new int[n]; // Undefined behavior
    

    n is used uninitialized here, so you’ll get a “random” length allocation.

    If you’re unlucky and n holds a “large” garbage value, your code could appear to work. If it holds a small value, you’re likely to corrupt your heap when you fill your initial array – this will produce the type of error you’re seeing.

    Move the cin >> n; line before the array allocation.

    Side note: I think the two allocations you’re doing in mergesort are leaked (you’re only deleteing the memory allocated in half, you don’t actually need to allocate in mergesort itself if I read your code correctly).

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

Sidebar

Related Questions

I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.