The order of complexity of the Pascal code -- like n * log(n)....

In summary, the conversation discusses finding the order of complexity of a function and solving similar problems. The example of code provided is in Pascal and has a complexity of n*logn, with the k-part repeating n times and the inner while loop repeating logn times. The conversation also mentions manually looking at the first few runs of the outer loop to better understand the pattern of s and k values and their relationship to the total iterations in the inner loop. Ultimately, the conclusion is that the values for s and k double every time and the inner loop will run n times, leading to the overall complexity of n*logn.
  • #1
doktorwho
181
6

Homework Statement


The kind of problems i we are currently dealing with in school are like this:
Complexity.JPG

Find the order of complexity of the function

Homework Equations


3. The Attempt at a Solution [/B]
Im suppose to guess or calculate the complexity of this based on code sample. Its in Pascal and to clarify what is meant here here is an example of the code and its complexity
Code:
for k:=1 to n do
   while x>0
      k:=k+1
      x:= x DIV 2
The complexity of this code is ##n*logn##. The k-part is repeated n times while the inner while repeats logn times. How to solve these kinds of problems? What would be the best way?
As i see this, the i<n part is repeated n times, but the s-part confuses me. I can't see ##2^n##
 
Physics news on Phys.org
  • #2
It will help if you look at the first 3 runs through of the outer loop manually. What happens to s in each step? How is that linked to the total iterations in the inner loop?
 
  • #3
mfb said:
It will help if you look at the first 3 runs through of the outer loop manually. What happens to s in each step? How is that linked to the total iterations in the inner loop?

In the first run s is 1 and it is assigned to k. Then the inner code runs once, increases k by one and assigns it to s. Then the outer again assigns so to k but now the inner runs twice and increases k by 2. In the next run s is 8. Is that the pattern, then it will be 16 and it runs n times since the outer checks for i<n and it is increased by 1 every time?
 
  • #4
The values used for s and k double every time, and they will do that n times, right.

That should lead to the number of steps done in the inner loop.
 

What is the order of complexity of the Pascal code?

The order of complexity of code refers to the rate at which the execution time or space requirements of a program increase as the input size increases. In Pascal code, this can be represented by a mathematical expression, such as n * log(n) or n^2, where n is the input size.

What does n * log(n) represent in Pascal code?

n * log(n) is a common order of complexity in Pascal code, which is known as "linearithmic". This means that as the input size increases, the execution time or space requirements of the code will increase at a rate that is less than the input size itself, but more than a constant. This is a more efficient order of complexity than linear (n) or quadratic (n^2).

Can the order of complexity of the Pascal code be improved?

Yes, the order of complexity of Pascal code can be improved by optimizing the algorithm used in the code. This may involve finding a more efficient algorithm, using data structures that can reduce the time or space requirements, or implementing techniques such as memoization or dynamic programming.

How do I determine the order of complexity of my Pascal code?

To determine the order of complexity of Pascal code, you can analyze the code and identify the number of operations and loops that are dependent on the input size. You can then express this dependence as a mathematical expression, which will represent the order of complexity of the code.

What is the significance of understanding the order of complexity of Pascal code?

Understanding the order of complexity of Pascal code is important for optimizing code and predicting its performance on larger input sizes. It also helps in making informed decisions when choosing between different algorithms or data structures to use in a program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
916
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
Back
Top