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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:44:27+00:00 2026-06-15T23:44:27+00:00

Possible Duplicate: Big array gives segmentation error in C i’m trying to compare merge

  • 0

Possible Duplicate:
Big array gives segmentation error in C

i’m trying to compare merge sort and quick sort with different input sizes like 10.000, 100.000, and 1.000.000. However, when i give one million input size program is crashing and i don’t know why ?.On the other hand, array is filled with reading from a file which contains numbers and here is my simple merge sort.

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#define SIZE 1000000

void Merge(int * , int , int , int );
void MergeSort(int *array, int left, int right);

int main(){

    struct timeval tv; 
    struct timezone tz; 
    struct tm *tm; 
    long start,stop;

    long i,num;
    int array[SIZE];

    FILE* fptr;

    gettimeofday(&tv, &tz); 
    tm = localtime(&tv.tv_sec); 
    printf("TIMESTAMP-START\t %d:%02d:%02d:%d (~%d ms)\n", tm->tm_hour, 
    tm->tm_min, tm->tm_sec, tv.tv_usec, 
    tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 + 
    tm->tm_sec * 1000 + tv.tv_usec / 1000);

    start = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 + tm->tm_sec * 1000 + tv.tv_usec / 1000;

    fptr = fopen ("onemillion.txt","r");

    for(i=0; i<SIZE-1; i++){

        fscanf(fptr,"%d",&num);
        array[i]=num;
    }

    MergeSort(array,0,SIZE-1);

    /*for(i = 0;i < SIZE;i++)
        {
                printf("%d \n",array[i]);
        }*/

    gettimeofday(&tv, &tz); 
    tm = localtime(&tv.tv_sec); 
    stop = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 + 
    tm->tm_sec * 1000 + tv.tv_usec / 1000; 
    printf("TIMESTAMP-END\t %d:%02d:%02d:%d (~%d ms) \n", tm->tm_hour, 
    tm->tm_min, tm->tm_sec, tv.tv_usec, 
    tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 + 
    tm->tm_sec * 1000 + tv.tv_usec / 1000); 

    printf("ELAPSED\t %d ms\n", stop - start);

    return 0;

}

void MergeSort(int *array, int left, int right)
{
        int mid = (left+right)/2;
        /* We have to sort only when left<right because when left=right it is anyhow sorted*/
        if(left<right)
        {
                /* Sort the left part */
                MergeSort(array,left,mid);
                /* Sort the right part */
                MergeSort(array,mid+1,right);
                /* Merge the two sorted parts */
                Merge(array,left,mid,right);
        }
}

void Merge(int *array, int left, int mid, int right)
{
        /*We need a Temporary array to store the new sorted part*/
        int tempArray[right-left+1];
        int pos=0,lpos = left,rpos = mid + 1;
        while(lpos <= mid && rpos <= right)
        {
                if(array[lpos] < array[rpos])
                {
                        tempArray[pos++] = array[lpos++];
                }
                else
                {
                        tempArray[pos++] = array[rpos++];
                }
        }
        while(lpos <= mid)  tempArray[pos++] = array[lpos++];
        while(rpos <= right)tempArray[pos++] = array[rpos++];
        int iter;
        /* Copy back the sorted array to the original array */
        for(iter = 0;iter < pos; iter++)
        {
                array[iter+left] = tempArray[iter];
        }
        return;
}

when i try with one thousand, ten thousand and one hundred thousand there is no problem. As i said i struggle with one million input size. I’m not sure but i guess it is about using an array ?. I will be glad if you can help and thanks anyway.

  • 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-15T23:44:28+00:00Added an answer on June 15, 2026 at 11:44 pm

    You are getting an stack overflow. There is not enough stack memory to accommodate your request resulting in run-time exception.

    Try allocating the array dynamically using malloc.

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

Sidebar

Related Questions

Possible Duplicate: Big O when adding together different routines What does O(n) + O(log(n))
Possible Duplicate: Array size too big - ruby Sorry if this has been asked,
Possible Duplicate: Learning C by K&R, error when trying to compile programs from book
Possible Duplicate: Passing JavaScript Array To PHP Through JQuery $.ajax I'm trying to pass
Possible Duplicate: Is Java pass-by-reference? if we have big byte[] array (like 40Mb) and
Possible Duplicate: Big Satellite Image Processing I'm trying to run Mort Canty's Python iMAD
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Sizeof an array in the C programming language? I am passing a
Possible Duplicate: Plain english explanation of Big O I'd imagine this is probably something
Possible Duplicate: HTML: Sub-pixel border The default border:1px is too big. However, border: 0.5px

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.