Showing posts with label time complexity. Show all posts
Showing posts with label time complexity. Show all posts

Sunday, 24 September 2017

Time Complexity analysis


A program with time complexity 0.01*n2 is more complex than the program with time complexity 1600*n
For ex: Let n=1,000,000
0.01*n2 = 0.01*1,000,000*1,000,000 =10,000,000,000
1600*n  = 1600*1,000,000=1,600,000,000
Hence for larger values of n, a program with time complexity 0.01*n2 is more complex than the program with time complexity 1600*n  
Time complexity of 0.01*n2 +1600*n  would be O(n2)

Monday, 18 September 2017

Time complexity Calculation


A program with time complexity 4n is more complex than the program with time complexity 16*n16
For ex: Let n=1024 =210
4n can be written as  22n
and substituting n value in 22n, we get
22*1024  =22048

and substituting n value in 16*(1024)16, we get
24 *(210)16
= 24 *2160
=2164
Hence for larger values of n, a program with time complexity 4n is more complex than the program with time complexity 16*n16
Time complexity of 4n+16*n16 would be O(22n)



Tuesday, 12 September 2017

C program to remove duplicate values from an array



#include <stdio.h>
main()
{
               int a[10]={1,2,1,1,2,3,2,3,3,4},b[10],i,j=0,c,flag;
               b[0]=a[0];
               for(i=1;i<10;i++)
               {
                              flag=0;
                              for(c=0;c<=j;c++)
                              {
                                             if(a[i]==b[c])//  check whether a[i] is present in entire b array
                                             {
                                                            flag=1;
                                                            break;
                                             }
                              }
                              if(flag == 0)
                              {
                                             b[++j]=a[i];
                              }
               }
               printf("the array elements after removing duplicate values are\n");
               for(i=0;i<=j;i++)
                              printf("%d\t",b[i]);
}


Output:



Time complexity :

In worst case : Worst case occurs when there are no duplicate values in 'a' array

b[0]= a[0]
i.e, a[1]  is present in b array or not  --1 comparison as b array contains only one element at that moment
Since there are no duplicate elements in 'a' array then b[1] will contain a[1] i.e, b contains 2 elements now.

then 

a[2] is checked if it is present in b[0] or b[1]---doing 2 comparisons 
a[3] is checked if it is present in b[0] or b[1] or b[2]---doing 3 comparisons
:
:
a[n-1] is checked if it is present in b[0] or b[1] or......b[n-2] --- doing n-1 comparisons


adding all the comparisons 
1+2+3+....n-1 .

this is nothing but sum of first n-1 natural number i.e, = ((n-1)*(n-2))/2 = n^2 

Hence Time complexity of above program is O(n^2)

In Best Case:  occurs when all the array elements in 'a' array are same
b[0]=a[0]

a[1] is compared with b[0] for equality ...since a[1] and b[0] are same, b array will contain only one element.....1 comparison is done
a[2] is compared with b[0] for equality  ...1 comparison is done
a[3] is compared with b[0] for equality  ...1 comparison is done
:
:
a[n-1] is compared with b[0] for equality  ...1 comparison is done

Adding all comparisons
1+1+1+......n-1 times.
that is n-1 comparisons

Hence Best Time complexity is 'n'