0

How to calculate Algorithm time complexity about for loop?and the other?

Tags: asked May 29, 2012
1

Complexity of the loop is "On the Order of N" time, or O(N), because it is Linear. Run time varies directly with N.

For example, let us assume that any single statement takes a constant amount of time, T. So if your statement is

i = 3;

the time it takes to run is T.

Now suppose you have a loop that runs N times:

for (int i = 0; i<N; i++)

{

i = 3;

}

It will take N*C, since you're doing a statement that takes T time, N times.

Suppose you have two loops:

for (int i = 0; i<N; i++)

for (int j = 0; j<N; j++)

i = 3;

Now you're running the statement N*N times, so the complexity of this code is O(N^2).

Other imaginable sequences could take sqrt(N) time. We say this are O(log N).

chitra

35

Please login to post questions.

Email: |

8951
algorithm
array
ASM program
binary tree
C/C++
C/C++ Programms
c graphics
c interview question
class
cpp
c program
c programs
C Programs
Data structure
download
engineering questions
free
frequently asked interview questions
Graphics
HDL
interview questions and answers
it interview questions
Java
Java program
Keyboard Interface
lab programs
linked list
Linked Lists
Microcontroller
microprocessor
program
programs
questions for an interview
Queue
source code
Source Codes
stack
Stepper motor
templates
top interview questions
verilog
Verilog program
Verilog programs
vlsi

This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.

#Home #Sitemap #Resources #Terms of Use

Copyright©2012 electrofriends.com All Rights Reserved

Contact:info@electrofriends.com