What is the time complexity for each of the following code segments?
1. int i, j, y=0, s=0;
for ( j = 1; j <= n; j ++)
{
y=y+j;
}
for ( i = 1; i <= y; i ++)
{
s++;
}
My answer is O(2n) since it iterates through each loop n times and there are two loops
2. function (n) {
while (n > 1) {
n = n/3 ;
}
My answer for this one is n^(1/3) since n becomes a third of its size every time
3. function (n) {
int i, j, k ;
for ( i = n/2; i <= n; i ++ ) { //n/2?
for ( j = 1; j <= n; j = 2*j ) { //logn
for ( k = 1; k <= n; k = 2*k ) { //logn
cout << ”COSC 2437.201, 301” << endl;
}
}
}
}
I said the answer to this one was O(log2*log2n*n/2) but I'm pretty confused about the first for loop. The loop only has to iterate half of n times, so it would be n/2 correct? Thanks for your help, everyone.