Calculates partial sum of an integer, Data Structure & Algorithms

Now, consider a function that calculates partial sum of an integer n. int psum(int n)


int i, partial_sum;

partial_sum = 0;                                           /* Line 1 */

for (i = 1; i <= n; i++) {                                /* Line 2 */

partial_sum = partial_sum + i*i;            /* Line 3 */


return partial_sum;                                                 /* Line 4 */


This function returns the sum by i = 1 to n of i squared, which means p sum = 12 + 22+ 32

+ .............  + n2 .

Ø  As we ought to determine the running time for each of statement in this program, we ought to count the number of statements which are executed in this process. The code at line 1 & line 4 are one statement each. Actually the for loop on line 2 are 2n+2 statements:

  • i = 1; statement: simple assignment, therefore one statement.
  • i <= n; statement is executed once for each value of i from 1 to n+1 (until the condition becomes false). The statement is executed n+1 times.
  • i++ is executed once for each of execution of body of the loop. It is executed for n times.

Therefore, the sum is equal to 1+ (n+1) + n+1 = 2n+ 3 times.

In terms of big-O notation described above, this function is O (n), since if we choose c=3, then we notice that cn > 2n+3. As we have already illustrious earlier, big-O notation only provides a upper bound to the function, it is also O(nlog(n)) & O(n2), since n2 > nlog(n) > 2n+3. However, we will select the smallest function which describes the order of the function and it is O (n).

Through looking at the definition of Omega notation & Theta notation, it is also apparent that it is of Θ(n), and thus ?(n) too. Because if we select c=1, then we see that cn < 2n+3, therefore ?(n) . Since 2n+3 = O(n), & 2n+3 = ?(n), this  implies that 2n+3 = Θ(n) , too.

Again it is reiterated here that smaller order terms and constants may be avoided while describing asymptotic notation. For instance, if f(n) = 4n+6 rather than f(n) = 2n +3 in terms of big-O, ? and Θ, It does not modify the order of the function. The function f(n) = 4n+6 = O(n) (through choosing c appropriately as 5); 4n+6 = ?(n) (through choosing c = 1), and thus 4n+6 = Θ(n). The spirit of this analysis is that in these asymptotic notation, we may count a statement as one, and should not worry regarding their relative execution time that may based on several hardware and other implementation factors, as long as this is of the order of 1, that means O(1).

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

Related Discussions:- Calculates partial sum of an integer, Assignment Help, Ask Question on Calculates partial sum of an integer, Get Answer, Expert's Help, Calculates partial sum of an integer Discussions

Write discussion on Calculates partial sum of an integer
Your posts are moderated
Related Questions
Q. Write an algorithm INSERT which takes a pointer to a sorted list and a pointer to a node and inserts the node into its correct position or place in the list.  Ans: /* s

Define Prim's Algorithm Prim's  algorithm  is  a  greedy  algorithm  for  constructing  a  minimum  spanning  tree  of  a  weighted linked graph. It works by attaching to a bef

Program: Creation of Doubly Linked List OUTPUT Input the values of the element -1111 to come out : 1 Input the values of the element -1111 to come out : 2 Inpu

1. Apply the variant Breadth-First Search algorithm as shown in Figure 2 to the attached graph. This variant is used for computing the shortest distance to each vertex from the sta

Example: (Double left rotation while a new node is added into the AVL tree (RL rotation)) Figure: Double left rotation when a new node is inserted into the AVL tree A

Explain the bubble sort algorithm. Answer This algorithm is used for sorting a list. It makes use of a temporary variable for swapping. It compares two numbers at an insta

Example: Insertion of a key 33 into a B-Tree (w/split) Step 1: Search first node for key closet to 33. Key 30 was determined. Step 2: Node pointed through key 30, is se

Simulation of queues: Simulation is the process of forming an abstract model of a real world situation in order to understand the effect of modifications and the effect of introdu

Triangular Matrices Tiangular Matrices is of 2 types: a)  Lower triangular b)  Upper triangular

write a pseudocode to input the top speed (in km''s/hours) of 5000 cars output the fastest speed and the slowest speed output the average (mean) speed of all the 5000 cars answers