Nested for loop, Data Structure & Algorithms

nested for loop

for (i = 0; i < n; i + +) {

for (j = 0; j < m; j + +) {

sequence of statements

}

}

Here, we observe that, the outer loop executes n times. Every time the outer loop executes, the inner loop executes m times. Consequently of this, statements in the inner loop execute a total of n * m times. Thus, the time complexity is O(n * m). If we modify the conditional variables, where the condition of the inner loop is j < n instead of j < m (i.e., the inner loop also executes n times), then the total complexity for the nested loop is O(n2).

Posted Date: 4/4/2013 5:56:17 AM | Location : United States







Related Discussions:- Nested for loop, Assignment Help, Ask Question on Nested for loop, Get Answer, Expert's Help, Nested for loop Discussions

Write discussion on Nested for loop
Your posts are moderated
Related Questions
After learning this, you will be able to: understand the concept of algorithm; understand mathematical foundation underlying the analysis of algorithm; to understand se

Since the stack is list of elements, the queue is also a list of elements. The stack & the queue differ just in the position where the elements may be added or deleted. Similar to

Optimal solution to the problem given below. Obtain the initial solution by VAM Ware houses Stores Availibility I II III IV A 5 1 3 3 34 B 3 3 5 4 15 C 6 4 4 3 12 D 4 –1 4 2 19 Re

INSERT FUNCTION /*prototypes of insert & find functions */ list * insert_list(list *); list * find(list *, int); /*definition of  anyinsert function */ list * inser

An undirected graph G with n vertices and e edges is shown by adjacency list.  What is the time required to generate all the connected components? O (e+n)

I am looking for some help with a data mining class with questions that are about neural networks and decision trees. Can you help? I can send document with questions.

What do you understand by term structured programming? Explain the structured programming as well.                                 Ans. S tructured Programming is expla

for (i = 0; i sequence of statements } Here, the loop executes n times. Thus, the sequence of statements also executes n times. Since we suppose the time complexity of th

Ask consider the file name cars.text each line in the file contains information about a car ( year,company,manufacture,model name,type) 1-read the file 2-add each car which is repr

5. Implement a stack (write pseudo-code for STACK-EMPTY, PUSH, and POP) using a singly linked list L. The operations PUSH and POP should still take O(1) time.