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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:22:21+00:00 2026-06-13T05:22:21+00:00

This program takes two input . first Array size(n) and choice ex:- if n

  • 0

This program takes two input . first Array size(n) and choice
ex:- if n is 100 than my program generate 100 random numbers and sort them using merge
sort and quick sort.

Now if you enter choice==2 than it display time taken by both sorting algorithm

My program works upto input size 10^8 but I want it to work upto 10″10 .If i enter input size 10^9 it gives segmentation fault . there is problem in allocation that much amount of memory .malloc returns null if input size exceeds 10^9

Can anyone please tell me how can I improve my program’s input size………..

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

long long merge(int *A,long long i,long long mid,long long j)                   

{   

int *C;
C=(int *)malloc(sizeof(int)*(j-i+1));                           
long long  r,start,k;
r=0;
start=i;
k=mid+1;
while((i<=mid)&&(k<=j))
{
    if(A[i]>A[k])
    {
        C[r]=A[k];
        r++;k++;
    }
    else
    {
        C[r]=A[i];
        r++;i++;
    }
}
while(i<=mid)
{
    C[r]=A[i];
    r++;i++;
}
while(k<=j)
{
    C[r]=A[k];
    r++;k++;
}
for(i=0;i<r;i++,start++)
    A[start]=C[i];
free(C);
  }
   long long partition(int *A,long long i,long long j)                           
   {

long long mid;
mid=(i+j)/2;                                    
if(i<j)
{
    partition(A,i,mid);                         
    partition(A,mid+1,j);
    merge(A,i,mid,j);                       
}
    }

    long long find_position(int A[],long long i,long long j)                        

    {

long long pivot,temp,end;
end=j;
pivot=i;
while(i<j)
{
    while(A[i]<=A[pivot]&&i<end)
        i++;
    while(A[j]>A[pivot])
        j--;
    if(i<j)
    {
        temp=A[i];
        A[i]=A[j];
        A[j]=temp;
    }
}
temp=A[pivot];
A[pivot]=A[j];
A[j]=temp;
return j;
  } 
  long long quicksort(int A[],long long  i,long long  j)                                
  {
long long position;
if(j>i)
{
    position = find_position(A,i,j);
    quicksort(A,i,position-1);
    quicksort(A,position+1,j);
  }
    }
   int main()
  {
clock_t start,end,quick_sort,merge_sort;
long long input_size,i;
int choice,x;
srand(time(NULL));
printf("Enter input Size\n");
scanf("%lld",&input_size);
int *A,*B;
printf("input your choice\n");
scanf("%d",&choice);
A=(int *)malloc(input_size*sizeof(int));
B=(int *)malloc(input_size*sizeof(int));
if(A==NULL||B==NULL)
{
    printf("sorry that much memory can't be allocated\n");
    return 0;
}
for(i=0;i<input_size;i++)
{
    x=rand();
    A[i]=x;
    B[i]=x;
}
if(choice==1)
{
    printf("Array entered by user\n");
    for(i=0;i<input_size;i++)
        printf("%d  ",A[i]);
}
start=clock();
partition(A,0,input_size-1);
end=clock();
merge_sort=end-start;
if(choice==2)
    printf("\n time taked by merge sort is %6.6f",((double)(merge_sort)/CLOCKS_PER_SEC));
if(choice==1)
{
    printf("\nmerge sorted array\n");
    for(i=0;i<input_size;i++)
        printf("%d  ",A[i]);
}
start=clock();
quicksort(B,0,input_size-1);
end=clock();
quick_sort=end-start;
if(choice==2)
    printf("\n time taked by quick sort is %6.6f",((double)(quick_sort)/CLOCKS_PER_SEC));
if(choice==1)
{
    printf("\nquick sorted array\n");
    for(i=0;i<input_size;i++)
        printf("%d  ",B[i]);
}
printf("\n");
return 0;

}

  • 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-13T05:22:22+00:00Added an answer on June 13, 2026 at 5:22 am

    The max allocatable size is 2GB for malloc(which takes size_t). Hence your program crashes. I am assuming you’re on a 32 bit machine.

    Please see below link to learn more about it and test if you can actually allocate that much memory.

    How Big can a malloc be in C

    To get around this, you will need to write your own VIRTUAL MEMORY MANAGER and MEMORY ALLOCATOR using binary files and write your own free() and malloc() routines.

    See here for an example(this is for iPhones only): Create your own VMM

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

Sidebar

Related Questions

This program takes the first number in a file and indicates how many numbers
This program takes 2 numbers from user input, asks them whether they'd like to
I wrote this function to communicate with an external program. Such program takes input
I am building a program that takes an input file in this format: title
My program takes in two parameters, first parameter is name to be changed, and
I have two integer arrays created at runtime (size depends on the program input).
there is this check-in-out program here at my workplace, it only takes the data
I have a program, that takes a long time to load. Because of this,
This program builds a dictionary out of a list based on what two index
Purpose: Create a program that takes two separate files, opens and reads them, assigns

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.