i am trying to find the time complexity of the bubble sort
n=length[A]
for j <- n-1 to 1
for i <- 0 to j-1
if A[i]>a[i+1]
temp=A[i]
A[i]=A[i+1]
A[i+1]=temp
return A
please any one can help thanks
In line 1 we are assigning length of array to n so constant time
In line 2 we have a for loop that decrements j by 1 every iteration until j=1 and in total will iterate n-2 times.
Inside the first for loop we have a second for loop that increments i by 1 every iteration until i=j-1 and will iterate j-1 times. On each iteration of the inner for loop we have lines 4,5,6,7 which are all just assignments and array access which cost, in total, constant time.
We can think about the two for loops in the following way: For every iteration of the outer for loop, the inner for loop will iterate j-1 times.
Therefore on the first iteration of the outer for loop, we have j = n-1. That means the inner for loop will iterate (n-1)-1 = (n-2) times. Then on the second iteration of the outer for loop we have j= n-2 so the inner for loop will iterate (n-2)-1 = (n-3) times and so on. And we do this until j = 1.
We will then have the equation: (n-2) + (n-3) + ... + 2 + 1 which is the total number of times the inner loop will iterate after the entire algorithm executes. We know that 1 + 2 + ... + n-1 + n = n(n-1)/2 so our expression can be simplified to this: n(n-1)/2 -(n-1) -n = n(n-1)/2 -2n + 1 = O(n^2).
Since our inner for loop will iterate O(n^2) times, and on each iteration do constant work, then that means our runtime will be O(cn^2) where c is the amount of constant work done by lines 4,5,6,7. Combine O(cn^2) with line 1 which is O(1) we have O(cn^2) + O(1) which is just O(n^2).
Therefore runtime of BubbleSort is O(n^2).
If you are still confused then maybe this will help: https://www.youtube.com/watch?v=Jdtq5uKz-w4
Related
If the the first loop runs for n+1 times.
second loop runs for n(n+1) times.
third loop will run for ??? it has n^2+1 one relation with with with the second loop i guess but how about with the first one
somefunction(n) {
c = 0
for (i = 1 to n*n)
for (j = 1 to n)
for (k = 1 to 2*j)
c = c+1
return c
}
The first loop has O(n**2) iterations.
The second loop has O(n) iterations.
The third loop has O(n) iterations as well, since j is steadily increasing towards n.
(It's a little easier to see if you sum up the number of times c = c + 1 executes for the two inner loops combined. The inner loop runs 2 times for j = 1, 4 for j = 2, ..., and 2*n times for j = n. 2 + 4 + .. + 2*n = O(n**2).)
You can then (loosely speaking) multiply the three values together to get a total bound of O(n**4).
Need help figuring this hw example out guys.
the example states 0(n) running time.
I see that the outter loop is O(logn)
I cant figure out how to describe the inner loop in relation to n.
Help is much appreciated.
for (int i = 1; i <= N; i = i*2) // log n
for (int j = 0; j < i; j++) // less than n i don't know how to describe the growth
sum++;
Answer:: 0(n)
It might be easiest to analyze the runtime of the piece of code in the aggregate. On the first iteration of the outer loop, the inner loop will run 1 time. On the second iteration of the outer loop, the inner loop will run 2 times. On the third iteration of the outer loop, the inner loop will run 4 times. More generally, on the kth iteration of the outer loop, the inner loop will run 2k times. The outer loop stops as soon as i becomes greater than N, which happens after log2 N iterations.
If we sum up the total work done, we'll see that it's
1 + 2 + 4 + 8 + ... + 2log2 N = 2log2 N + 1 - 1 = 2n - 1
(This uses the fact that 1 + 2 + 4 + 8 + ... + 2k = 2k+1 - 1). Therefore, the total work done for the entire piece of code (that is, including both loops) is O(n).
The inner loop is O(n) time because it will go through each element once. If i is 1000, 1000 times, if i is 10000, 10000 times, etc.
I have the following pseudocode:
SelectionSort(A)
n = A.length
for j=1 to n-1
smallest = j
for i=(j+1) to n
if A[i] < A[smallest]
smallest = i
exchange A[j] with A[smallest]
I think that the first for-loop test will execute n times, and the nested for-loop will execute 1 + 2 + ... + n = n(n+1)/2 times (please correct me if i'm wrong). But I don't understand how I can tell how many times the nested if-statement will execute? Is it 1/2 * n(n+1)/2 ?
The outer for-loop would run for n times. But, it also holds an inner for-loop which is dependent on the vale of j.
I think that the first for-loop test will execute n times, and the
nested for-loop will execute 1 + 2 + ... + n = n(n+1)/2 times.
The inner for-loop will run (n-1)+(n-2)+...+1 times based on all the iterations of outer for-loop. So, the net iteration of the loop-statements combinedly would be (n-1) + (n-2) + (n-3) + ... + 1 = (n-1)*n/2 = (n2-n)/2 times.
But I don't understand how I can tell how many times the nested
if-statement will execute? Is it 1/2 * n(n+1)/2 ?
As the inner-if statement is dependent on the array elements, it is not possible to directly ascertain exactly how many times it will run.
But, it is for sure that in the worst-case condition,as it is located in the inner for-loop, the maximum number of possible execution(in the worst case) of the if-statement will be (n2-n)/2 times.
So, the worst-case complexity of execution of if-statement is
O(n2)
The outer loop shall run from 1 to n-1, hence n-1 times.
The inner loop shall run from j+1 to n times. This means that when j is 1, it will run from 2 to n times (n-1 times) and when j is n-1, it will run from n to n times (1 time).
Hence the inner loop shall run (n-1 + n-2 + ... + 1) times = n(n-1)/2 times.
The if statement shall execute the same number of times as the inner loop does. Ofcourse the conditional statments shall depend on the result of if conditional.
What is the value returned by the following function? Express your answer as a
function of n. Give using O() notation the worst-case running time.
Pseudo code of the algorithm:
F1(n)
v = 0
for i = 1 to n
for j = n + 1 to 2n
v++
return v
My approach:
The output of F1(n) is a integer value returned from the variable v which is calculated by the inner for-loop during iteration on size 2n and on size n for the outer for-loop.
The variable v is initialized as 0 before the iterations of both loops begin. When the ith for-loop is at the 1st ith iteration then the jth for-loop(inner for-loop) iterates from size n+1 to 2n. Suppose n = 5 then the jth for-loop iterates from j = 6 to 10, this means the number of times v is incremented for 1 full iteration of the inner for-loop.
Since the jth for-loop always increments v by n-1(4 in this case) then in each full iteration this means when the ith for-loop starts from 1 to n then the variable v is incremented by n-1 times for every i iteration.
So this algorithm maps the function g(n) = (n-1) * (n-1). Which will be 4 * 4, so v = 16 when n = 5. This is true because when n = 5 every full jth iteration v is incremented by 4. So if the ith for-loop runs from i =1,…,4(1 to n) then v is incremented by 4 every time i is incremented so this explains why the result would be (n-1 * n-1) as the answer.
The worst-case Big Oh complexity for this program is O(n^2) because it has a nested loop structure where the outer loop iterates through 1 to n times and the inner loop iterates through n+1 to 2n times which is also equivalent to n times because it is iterating through all values of n.
Lastly, the initialization/incrementing of a variable takes O(1) time so this does not affect the complexity much in comparison to the nested for-loop.
To me this maps g(n)=n^2 function since loop for i=1 to n runs n times if you count all values from range [1,n]. Of course if it is [1,n) then you have g(n)=(n-1)^2 but that's matter of convention.
Two nested loops , each running n times give you O(n^2) complexity which is best-average-worst in this case. So your approach is fine if that was your question :)
What is the time complexity of this algorithm:
sum = 0
i = 1
while (i < n) {
for j = 1 to i {
sum = sum + 1
}
i = i*2;
}
return sum
I know that the while loop is O(logn), but what is the complexity of the for loop? Is it O(n) or O(logn)?
One way to analyze this would be to count up the number of iterations of the inner loop. On the first iteration, the loop runs one time. On the second iteration, it runs two times. It runs four times on the third iteration, eight times on the fourth iteration, and more generally 2k times on the kth iteration. This means that the number of iterations of the inner loop is given by
1 + 2 + 4 + 8 + ... + 2r = 2r + 1 - 1
Where r is the number of times that the inner loop runs. As you noted, r is roughly log n, meaning that this summation works out to (approximately)
2log n + 1 - 1 = 2(2log n) - 1 = 2n - 1
Consequently, the total work done by the inner loop across all iterations in O(n). Since the program does a total of O(log n) work running the outer loop, the total runtime of this algorithm is O(n + log n) = O(n). Note that we don't multiply these terms together, since the O(log n) term is the total amount of work done purely in the maintenance of the outer loops and the O(n) term is total amount of work done purely by the inner loop.
Hope this helps!