in Programming in C
554 views
0 votes
0 votes

why this margeSort program showing time limit exceed ?

#include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
 
    void fillArray(int array[], int n)
    {
    	time_t t;
    	time(&t);//get current time
    	srand(t);//gives current time as seed for random number generator
 
 
    	for(int i = 0; i < n; i++)
    	{
    		array[i] = rand()%(10*n);//Doing mod to avoid very large numbers
    	}
    }
    void printArray(int array[], int n)
    {
    	for(int i = 0; i < n; i++)
    	{
    		printf("%d ", array[i]);
    	}
    }
    void mergeSort(int A[],int n){
    	int i;
    	int mid=n/2;
    	int p=(n-mid);
 
    	int w=sizeof(A[mid]);
    	int x=sizeof(A[p]);
    	int Aleft[w];
    	int Aright[x];
    	for(i=0;i<(mid-1);i++){
    		Aleft[i]=A[i];
    	}
    	for(i=mid;i<(n-1);i++){
    		Aright[i-mid]=A[i];
    	}
    	mergeSort(Aleft,n/2);
    	mergeSort(Aright,n/2);
    	merge(Aleft,Aright,A);
 
    }
 
    void merge(int lArray[], int rArray[],int Array[])
    {
    	int nL=sizeof(lArray);
    	int nR=sizeof(rArray);
    	int i=0;
    	int j=0;
    	int k=0;
    	while((i<nL)&&(j<nR)){
    		if(lArray[i]<=rArray[j]){
    			Array[k]=lArray[i];
    			i++;
    		}
    		else{
    			Array[k]=rArray[j];
    			j++;
    		}
    		k++;
    	}
    }
    int main()
    {
    	int *Array, n=8;
 
		//int p=(n-mid);
 
    	clock_t start, end;
        double cpu_time_used;
 
    	//printf("Enter the no. of numbers: ");
    	//scanf("%d", &n);
    	Array = malloc(n * sizeof(int));
    	//lArray = malloc(mid * sizeof(int));
    	//rArray = malloc((n-mid) * sizeof(int));
    	fillArray(Array, n);
    	//fillArray(lArray, mid);
    	//fillArray(rArray, p);
    	start = clock();
 
    	mergeSort(Array,n);
 
    	end = clock();
 
    	cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
    	printArray(Array, n);
    	printf("\n \n Time taken for sorting:  %f seconds\n\n",cpu_time_used);
    	return 0;
    }
 

 

in Programming in C
by
554 views

3 Comments

Where are you running it? What is the size of the test cases?

You'll need to provide a bit more detail.
0
0

run in any compiler 

https://ideone.com/UoZodl

 

0
0

srestha Have you written this code?

0
0

Please log in or register to answer this question.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true