What is the computational complexity of the following code s
What is the computational complexity of the following code snippet? In other words, how many times will the instruction, \"x = x + 1\" be executed? for I = 1 to n for j = 1 to n x = x + 1 Suppose an algorithm will search for a member x in a list of number starting from the start of the list and iterating thought the list until the end of the list. What is the best-case algorithm?
Solution
1) the statement x=x+1 will run n*n times.
This is because,
for i=1 to n ---> when i=1
for j=1 to n ----> this will run n times
x= x+1
For every value of i, j will continue to run from 1 to n.
hence the statement executes n*n times.
2) For a list , say, list = [ 1, 4, 7, 9, 10, 11, 16 ]
The algorithm will search from the beginning of the list to the end of the list.
Hence the best case input x would be the first value in the list.
3) Taking the same above mentioned as an example, the worst case input x would be the last value in the list.
