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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:54:48+00:00 2026-06-06T12:54:48+00:00

I’m working on a Coursera course, and I was given a file with 100,000

  • 0

I’m working on a Coursera course, and I was given a file with 100,000 integers to sort using a merge sort. Now, my function happens to work on the first 1000 integers, but for some reason, once I get to 10000+ it ceases to work. Yes, I modify the #define at the top depending on how many integers I am testing for. I’m going to go use another implementation I found on the web, but why did my code NOT work? I think I am missing something pretty obvious.

Oh, for the homework assignment, I need to turn in the number of inversions required to sort it (how many times, imagine a bubble sort, would an earlier/smaller number need to be moved behind a later/larger number). Hence the global variable.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define fileLineNumber 1000

void MergeSortL1 ( int arrayIn[], int arraySize );
int inversionCounter = 0;

int main (int argc, const char * argv[])
{
 // Declarations. Whee. Typical counter variable, i, arrayInOrder for boolean logic, a char for the file read type... and the array of so many bits...
 int array[fileLineNumber];
 int i, temp;
 FILE *fp;
 char* filePath = "/Users/TMC/Code/algorithmsCoursera/lesson1/IntegerArray.txt";
 char arrayInOrder = 1, fileOpenType;
 // Here, open a file.
 fileOpenType = 'r';
 fp = fopen( filePath, &fileOpenType);
 // Here, read into an array.
 for ( i = 0; i < fileLineNumber; i ++)
 {
  fscanf(fp,"%d", &temp);
  array[i] = temp;
 }
 fclose(fp);
 MergeSortL1(array, sizeof(array)/sizeof(int*));
 // Maybe check if it is in order.....
 for ( i = 0; i < fileLineNumber - 1; i++)
 {
  if ( array[i] > array[i+1] )
  {
   arrayInOrder = 0;
  }
 }
 /* Shorter, harder to read, have a 2 dimension array, a[2][4]. a[0] == " not" a[1] == ""      *
  * Just would need to printf ( "Array is%s in order.\n", a[arrayInOrder] ); Hard to maintian. */
 printf ("Array is" );
 if ( !arrayInOrder )
 {
  printf ( " not" );
 }
 printf (" in order.\n");
 printf ( "Inversion Counter says: %d\n\n", inversionCounter );
 // Write back to the file.
 fileOpenType = 'w';
 fp = fopen( filePath, &fileOpenType);
 for ( i = 0; i < fileLineNumber; i ++)
 {
  fprintf(fp, "%d", array[i]);
  fputc( '\n', fp );
 }
 fclose ( fp );
 return 0;
}

void MergeSortL1 ( int arrayIn[], int arraySize ) // Arrays are a pointer, so we don't need to capture a return...
{
    printf ( "------------------\nIn MergeSortL1, arraySize = %d\n------------------\n", arraySize );
    if ( arraySize <= 1 ) // Base Case.
    {
     printf ( "Base Case: Returning\n" );
     return;
    }
    else
    {
     int i = 0; // Counter variable
     char loopStillValid = 1, a1HasMore = 1, a2HasMore = 1;
     int temp1 = 0, temp2 = 0;
     int lenArray1 = ( floor(arraySize/2.0) ); // floor() and ceil() are just here in case we have an odd number of variables.
     int lenArray2 = ( ceil(arraySize/2.0) );
     int* array1 = malloc ( lenArray1 * sizeof (int) );
     int* array2 = malloc ( lenArray2 * sizeof (int) );
     for ( i = 0; i < lenArray1; i ++ ) // Assign values.
     {
      array1 [i] = arrayIn[i];
     }
     for ( i = 0; i < lenArray2; i ++ )
     {
      array2 [i] = arrayIn[i+lenArray1];
     }
     MergeSortL1 ( array1, lenArray1 );
     MergeSortL1 ( array2, lenArray2 );
     a1HasMore = lenArray1;
     a2HasMore = lenArray2;
     temp1 = 0;
     temp2 = 0;
     for ( i = 0; i < arraySize; i++)
     {
      loopStillValid = a2HasMore && a1HasMore;
      if ( loopStillValid && (array1[temp1] <= array2[temp2] ) )
      {
       arrayIn[i] = array1[temp1];
       temp1++;
      }
      else if ( loopStillValid && (array1[temp1] > array2[temp2] ) )
      {
       arrayIn[i] = array2[temp2];
       temp2++;
       inversionCounter ++;
      }
      else if (a2HasMore && !a1HasMore /*&& (array1[temp1] <= array2[temp2] )*/)
      {
       arrayIn[i] = array2[temp2];
       temp2++;
      }
      else if (!a2HasMore && a1HasMore /*&& (array1[temp1] > array2[temp2] )*/)
      {
       arrayIn[i] = array1[temp1];
       temp1 ++;
      }
      else
      {
       printf ("\n--------------\nERROR: UNCAUGHT STATUS\ni = %d\narray1[%d] = %d\narray2[%d] = %d\na1HasMore = %d\na2HasMore = %d\n--------------\n\n", i, temp1, array1[temp1], temp2, array2[temp2], a1HasMore, a2HasMore );
      }
      if ( temp1 >= a1HasMore )
      {
       temp1 --;
       a1HasMore = 0;
      }
      if ( temp2 >= a2HasMore )
      {
       temp2 --;
       a2HasMore = 0;
      }
     }
     free(array1);
     free(array2);
    }
}
  • 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-06T12:54:49+00:00Added an answer on June 6, 2026 at 12:54 pm

    Okay, it turns out that a char is not useful when storing values like 500, and 1000, and 100000. I fixed the problem, thanks to all who made suggestions, my writing style has improved because of them.

    char loopStillValid = 1, a1HasMore = 1, a2HasMore = 1;
    

    I used a1HasMore and a2HasMore as boolean values. Now, as chars, they wraparound to a negative value when too high a number is inputted.

    a1HasMore = lenArray1;
    a2HasMore = lenArray2;
    

    When lenArray1 and lenArray2 were 500, the chars were assigned negative values…. so I got to the wrong part of the if statement (-1 && -1 != true)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

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.