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
that will determine the volume of the sphere or the volume of cone or volume of pyramid depending on the choice of the user

Write an algorithm for multiplication of two sparse matrices using Linked Lists.

By changing the NULL lines in a binary tree to the special links called threads, it is possible to execute traversal, insertion and deletion without using either a stack or recursi


Q. Define the sparse metrics and also explain the representation of a 4X4 matrix using linked list.         Ans: A matrix in which number of zero entries is quite h

null(nil) = true                     // nil refer for empty tree null(fork(e, T, T'))= false   //  e : element , T and T are two sub tree leaf(fork(e, nil, nil)) = true leaf(

What is quick sort? Quick sort is a sorting algorithm that uses the idea if split and conquer. This algorithm chooses an element called as pivot element; search its position in

an electrical student designed a circuit in which the impedence in one part of a series circuit is 2+j8 ohms and the impedent is another part of the circuit is 4-j60 ohm mm program

A Stack has an ordered list of elements & an array is also utilized to store ordered list of elements. Therefore, it would be very simple to manage a stack by using an array. Thoug

Merging two sequence using CREW merge