Worst-Case Runtime
up vote
0
down vote
favorite
Question
Line 6 runs T(n/2) times in the worst case.
Line 8 runs T(n/2) times in the worst case.
So, T(n/2) + T(n/2) + d (+ d: for the minor constant time operations)
Using master theorem: 0 < 1 so T(n) = O(n) (Answer: A)
How in the world is the answer D?
runtime master-theorem
add a comment |
up vote
0
down vote
favorite
Question
Line 6 runs T(n/2) times in the worst case.
Line 8 runs T(n/2) times in the worst case.
So, T(n/2) + T(n/2) + d (+ d: for the minor constant time operations)
Using master theorem: 0 < 1 so T(n) = O(n) (Answer: A)
How in the world is the answer D?
runtime master-theorem
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Question
Line 6 runs T(n/2) times in the worst case.
Line 8 runs T(n/2) times in the worst case.
So, T(n/2) + T(n/2) + d (+ d: for the minor constant time operations)
Using master theorem: 0 < 1 so T(n) = O(n) (Answer: A)
How in the world is the answer D?
runtime master-theorem
Question
Line 6 runs T(n/2) times in the worst case.
Line 8 runs T(n/2) times in the worst case.
So, T(n/2) + T(n/2) + d (+ d: for the minor constant time operations)
Using master theorem: 0 < 1 so T(n) = O(n) (Answer: A)
How in the world is the answer D?
runtime master-theorem
runtime master-theorem
asked Nov 8 at 4:58
heskinreaper
62
62
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Firstly refrain from using links as they can go dead.
You could have easily typed the code in the image as I have done below
def select_number(A, low, high):
if low == high:
return A[low]
mid = (low+high) // 2
if A[mid] < A[mid+1]:
return select_number(A, mid+1, high) #line 6
else:
return select_number(A, low, mid) #line 8
The user calls select_number(A, 0, len(A)-1).
Without using the master theorem we can see that any invocation of select_number will make at most only one recursive call to itself, each time reducing the input space by half. This is similar to a binary search so your expected running time should be in log n.
You are counting the running times of lines 6 and 8 separately whereas they are mutually exclusive: if one runs the other doesn't.
So it is not 2T(n/2), just T(n/2), which is what is the solution in D.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Firstly refrain from using links as they can go dead.
You could have easily typed the code in the image as I have done below
def select_number(A, low, high):
if low == high:
return A[low]
mid = (low+high) // 2
if A[mid] < A[mid+1]:
return select_number(A, mid+1, high) #line 6
else:
return select_number(A, low, mid) #line 8
The user calls select_number(A, 0, len(A)-1).
Without using the master theorem we can see that any invocation of select_number will make at most only one recursive call to itself, each time reducing the input space by half. This is similar to a binary search so your expected running time should be in log n.
You are counting the running times of lines 6 and 8 separately whereas they are mutually exclusive: if one runs the other doesn't.
So it is not 2T(n/2), just T(n/2), which is what is the solution in D.
add a comment |
up vote
0
down vote
Firstly refrain from using links as they can go dead.
You could have easily typed the code in the image as I have done below
def select_number(A, low, high):
if low == high:
return A[low]
mid = (low+high) // 2
if A[mid] < A[mid+1]:
return select_number(A, mid+1, high) #line 6
else:
return select_number(A, low, mid) #line 8
The user calls select_number(A, 0, len(A)-1).
Without using the master theorem we can see that any invocation of select_number will make at most only one recursive call to itself, each time reducing the input space by half. This is similar to a binary search so your expected running time should be in log n.
You are counting the running times of lines 6 and 8 separately whereas they are mutually exclusive: if one runs the other doesn't.
So it is not 2T(n/2), just T(n/2), which is what is the solution in D.
add a comment |
up vote
0
down vote
up vote
0
down vote
Firstly refrain from using links as they can go dead.
You could have easily typed the code in the image as I have done below
def select_number(A, low, high):
if low == high:
return A[low]
mid = (low+high) // 2
if A[mid] < A[mid+1]:
return select_number(A, mid+1, high) #line 6
else:
return select_number(A, low, mid) #line 8
The user calls select_number(A, 0, len(A)-1).
Without using the master theorem we can see that any invocation of select_number will make at most only one recursive call to itself, each time reducing the input space by half. This is similar to a binary search so your expected running time should be in log n.
You are counting the running times of lines 6 and 8 separately whereas they are mutually exclusive: if one runs the other doesn't.
So it is not 2T(n/2), just T(n/2), which is what is the solution in D.
Firstly refrain from using links as they can go dead.
You could have easily typed the code in the image as I have done below
def select_number(A, low, high):
if low == high:
return A[low]
mid = (low+high) // 2
if A[mid] < A[mid+1]:
return select_number(A, mid+1, high) #line 6
else:
return select_number(A, low, mid) #line 8
The user calls select_number(A, 0, len(A)-1).
Without using the master theorem we can see that any invocation of select_number will make at most only one recursive call to itself, each time reducing the input space by half. This is similar to a binary search so your expected running time should be in log n.
You are counting the running times of lines 6 and 8 separately whereas they are mutually exclusive: if one runs the other doesn't.
So it is not 2T(n/2), just T(n/2), which is what is the solution in D.
answered Nov 8 at 5:34
Siddhesh Rane
14526
14526
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201788%2fworst-case-runtime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown