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
Enumerate the Types in Ruby Ruby is a pure object-oriented language, meaning that all types in Ruby are classes, and each value in a Ruby program is an instance of a class. Thi

Program: Program segment for insertion of an element into the queue add(int value) { struct queue *new; new = (struct queue*)malloc(sizeof(queue)); new->value = val

Algorithm for Z-Buffer Method (a)  Initialize every pixel in the viewport to the smallest value of z, namely z0 the z-value of the rear clipping plane or "back-ground". Store a

Methods of Collision Resolution 1)  Collision Resolution by separate chaining  2)  Collision Resolution by open addressing

A mathematical-model with a collection of operations described on that model is known as??? Abstract Data Type

What are the structures used in file-system implementation? Several on-disk and in-memory structures are used to execute a file system a. On-disk structure include P

Best Case: If the list is sorted already then A[i] T (n) = c1n + c2 (n -1) + c3(n -1) + c4 (n -1)  = O (n), which indicates that the time complexity is linear. Worst Case:

write an algorithm for multiplication of two sparse matrices using Linked Lists

Algorithm to Delete a given node from a doubly linked list Delete a Node from Double Linked List DELETEDBL(INFO, FORW, BACK, START, AVAIL,LOC) 1. [Delete Node] Set FOR

I =PR/12 Numbers of years .Interest rate up to 1yrs . 5.50 up to 5yrs . 6.50 More than 5 yrs . 6.75 design an algorithm based on the above information