0% found this document useful (0 votes)
95 views

4 - Complexitatea Algoritmilor - Exercitii

The document discusses algorithms for analyzing the time complexity of code snippets. It provides examples of breaking down loops and operations to determine the total number of basic operations. Shortcuts for analysis are described, such as treating constant operations as taking the same time and multiplying loop iterations by the inner operations.

Uploaded by

Iulian Crihana
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

4 - Complexitatea Algoritmilor - Exercitii

The document discusses algorithms for analyzing the time complexity of code snippets. It provides examples of breaking down loops and operations to determine the total number of basic operations. Shortcuts for analysis are described, such as treating constant operations as taking the same time and multiplying loop iterations by the inner operations.

Uploaded by

Iulian Crihana
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

int i = N; while (i > 0) { int Sum = 0; int j; for (j = 0; j < i; j++) Sum++; cout << Sum << endl;

i--; }

For example, the code int Sum = 0; is 1 basic operation. Then j = 0; is another basic operation. Then j < i forms yet another basic operation. Count these, and you get your time complexity. For example, take the loop, (Toggle Plain Text)
for (j = 0; j < i; j++) Sum++;

In this loop, you end up evaluating j = 0 once, j < i a total of i + 1 times, j++ i times, and Sum++; i times. So the total number of basic operations is: 1 + (i + 1) + i + i = 3i + 2. Then take the block of code, (Toggle Plain Text)
int Sum = 0; int j; for (j = 0; j < i; j++) Sum++; cout << Sum << endl; i--; Here, we have int Sum = 0; taking one operation, for (j = 0; j < i; j++) Sum++; taking 3i + 2 operations, cout << Sum << endl; taking 1 operation (or 2 operations, depending on how you look at it, but the whole thing takes a constant amount of time anyway). Then i--;

takes one operation. So that's a total of 1 + (3i + 2) + 1 + 1 = 3i + 5. Then take the block of code, (Toggle Plain Text)
int i = N; while (i > 0) { we calculated this to take 3i + 5 operations }

Every time through the loop, 1 + (3i + 5) operations are performed (one is added for the comparison i > 0). Of course, the value of i changes every time through the loop. So calculating the number of operations here takes a little bit of math. The first time through, we take 3N + 6 operations. The second time, we take 3(N - 1) + 6 operations. The third time, 3(N - 2) + 6 operations. And so on

and so on, until we take 3(1) + 6 operations. So it's time for some math. This number of operations is equivalent to ... + 2 + 1 = N(N+1)/2.) . (It's good to know the formula N + (N - 1) +

Now, what I just described is a very long and drawn out way of doing things. Nobody actually works problems like these out, but it's something you can fall back on. There are many shortcuts you can take. For example, it doesn't change anything if you mush all the constant values together (the way I treated the cout statement as just one operation). And whenever you see a while loop that runs N times, just multiply N by the amount of operations done on the inside. //////////////////////////////////////////// int strlen(char* sin) { int length = 0; while(sin != NULL) length ++; return length; } /////////////////////////////////////////// int isNumberEven(int no){ if(no % 2 == 0) return 1; else return 0; } ////////////////////////////////////// BinarySearch(A[0..N-1], value, low, high) { if (high < low) return -1 // not found mid = low + (high - low) / 2 if (A[mid] > value) return BinarySearch(A, value, low, mid-1) else if (A[mid] < value) return BinarySearch(A, value, mid+1, high) else return mid // found } BinarySearch(A, value, 0, N-1); ////////////////////////////////

procedure cocktailSort( A : list of sortable items ) defined as: do swapped := false for each i in 0 to length( A ) - 2 do: if A[ i ] > A[ i + 1 ] then // test whether the two elements are in the wrong order swap( A[ i ], A[ i + 1 ] ) // let the two elements change places swapped := true end if end for if swapped = false then // we can exit the outer loop here if no swaps occurred. break do-while loop end if swapped := false for each i in length( A ) - 2 to 0 do: if A[ i ] > A[ i + 1 ] then swap( A[ i ], A[ i + 1 ] ) swapped := true end if end for while swapped // if no elements have been swapped, then the list is sorted end procedure /////////////////////////////////////////////////////////// /* Odd-even sort - Assumes a is an array of values to be sorted. */ var sorted = false; while(!sorted) { sorted=true; for(var i = 1; i < list.length-1; i += 2) { if(a[i] > a[i+1]) { swap(a, i, i+1); sorted = false; } } for(var i = 0; i < list.length-1; i += 2) { if(a[i] > a[i+1]) { swap(a, i, i+1); sorted = false; }

} } ///////////////////////////////////////////////////////////// Y0 jn while j >= 1 do { k0 while k < n*n do { yy+j-k kk+1 } jj-1 } return y //////////////////////////////////////////////////////// For i 1 to n do for j 1 to i do for k j to i+j do aa+1 end for end for end for ///////////////////////////////////////////////////////// For i 1 to m do for j 1 to i2 do for k 1 to j do bb+1 end for end for end for //////////////////////////////////////////////////////// for(i = 1; i < n; i++){ for(j = i; j <= n; j*=2){ <statement> } } ///////////////////////////////////////////////////////////////////////////

m=0; i=1; while (i<=n) { i=i*2; for (j=1;j<=(long int)(log10(i)/log10(2));j++) for (k=1;k<=j;k++) m++; } ////////////////////////////////////////////////////////////////////////////////////////// for(i=0;i<n-1;i++) for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } ////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <math.h> int primetest(int n){ int temp = n,i; for(i = sqrt(n);i>=2;i--){ if(!(temp%i)) return 1; } return 0; } int MOD(int x,int y,int n){ unsigned long i,j; if(x>=1000){ i=x/1000; j=x%1000; return (((i*y)%n)*1000+(j*y))%n; } else if(y>=1000){ i=y/1000; j=y%1000;

return (((i*x)%n)*1000+(j*x))%n; } else{ return (x*y)%n; } } int Fermat_test(int N,int a){ int i,temp,bit=0,binary[20],n,mod; unsigned long pm[20]; if(primetest(N)){ n=N; N=N-1; while (N!=0){ bit++; i=N%2; binary[bit]=i; N=N/2; } pm[0]=a%n; mod=pow(a,binary[1]); for(i=1;i<=bit-1;i++){ pm[i]=MOD(pm[i-1],pm[i-1],n); if(binary[i+1]!=0){ mod=MOD(mod,pm[i],n); } } if(mod==1){ return 1; } else{ return 0; } } } main() { int a,N,i; FILE * fp; fp=fopen("c:\\output.txt","w+"); printf("Enter the value of a: <=13 "); fflush(stdout); scanf("%d",&a); for(i=a+1;i<=1000000;i++){ if(Fermat_test(i,a)==1){ fprintf(fp,"%d\n",i);

} } return 0; } ///////////////////////////////////////////////////////////////////////////

You might also like