1

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;

}

TLE
  • 705
  • 1
  • 7
  • 16
  • what operating system and compiler? – Aniket Inge Oct 21 '12 at 17:53
  • It's probably going over a 2GB limit. – chris Oct 21 '12 at 17:53
  • 2
    When I see this: `mid=(i+j)/2;` (although you are using `long long`) it always remind me this: http://googleresearch.blogspot.ch/2006/06/extra-extra-read-all-about-it-nearly.html – ouah Oct 21 '12 at 17:55
  • 10 ^ 10 should be no problem with a 64 bits machine and let's say 96GB RAM (10 billion ints = 40GB, which you allocate **twice**). 32 bits machines can only access 2GB work memory. You should probably rethink your problem, this ain't gonna work... – fvu Oct 21 '12 at 18:05
  • @fvu there's an "edit" option for comments. –  Oct 21 '12 at 18:05
  • @H2CO3 I know but it has a 5 minute window in which edits are allowed, and it was only afterwards that I saw he wants in fact 10^10 ints * 2... – fvu Oct 21 '12 at 18:07

2 Answers2

0

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

Community
  • 1
  • 1
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • So what's the solution.Should I run my program on 64 bit machine. – TLE Oct 22 '12 at 06:34
  • I am using ubuntu(32 bit),gcc compiler. – TLE Oct 22 '12 at 06:44
  • @stranger001 yes the solution is to run it in a 64 bit machine with more than 6-8GB RAM. But to remain architecture independent(that is runnable on 32 and 64 bit) yu might have to write your own VMM and malloc()/free() – Aniket Inge Oct 22 '12 at 07:56
0

You need to take into account that the address range can be 32bit or 64 bit and some of that is required for the stack, other variables, housekeeping etc. In addition many (all?) OS have soft and hard limits to prevent a process soaking up all the physical and swap memory.

Anyway why are you doing this?

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • He is probably coding a megavirus that will crash all our systems and then he will call in for firesale and take over our worlds. I probably think its homework or a question posed by an interviewer or something. – Aniket Inge Oct 21 '12 at 18:15
  • 1
    Personally I hate those interview questions. I have posed some questions for interview. The person is nervous so why the hell do you what to prove thhhat you are a bright spark? I think it is better to get a better candidate to test his or her knowledge on the subect. For example what does a pointer mean? – Ed Heal Oct 21 '12 at 20:59
  • Personally I like to test people on what they already know and how they would solve a tricky problem. The solution is simple but the question will trick the candidate. It tests how they will perform under pressure and how fast they can think. – Aniket Inge Oct 21 '12 at 21:01
  • @PrototypeStark - You are in the middle of a chess board and you can walk four squares where are you? – Ed Heal Oct 21 '12 at 23:05
  • in the middle of a chess board – Aniket Inge Oct 21 '12 at 23:08
  • Why bother on the technical stuff. That is easy to prove one way or the other. But will that person get one with the team? That question is most illuminating. – Ed Heal Oct 21 '12 at 23:21
  • I agree. Its important to understand who will fit in and who will not. – Aniket Inge Oct 21 '12 at 23:23