Determine the Big-O value for the following functions.
The valid Big-O options are:
def function_one(list_):
for pass_ in range(len(list_) - 1, 0, -1):
for i in range(pass_):
if list_[i] > list_[i+1]:
temp = list_[i]
list_[i] = list_[i+1]
list_[i+1] = temp
def function_two(list_):
if len(list_) > 1:
midpoint_index = len(list_) // 2
left_half = list_[:midpoint_index]
right_half = list_[midpoint_index:]
function_two(left_half)
function_two(right_half)
left_half_pos = 0
right_half_pos = 0
new_position = 0
while left_half_pos < len(left_half) and right_half_pos < len(right_half):
if left_half[left_half_pos] < right_half[right_half_pos]:
list_[new_position] = left_half[left_half_pos]
left_half_pos += 1
else:
list_[new_position] = right_half[right_half_pos]
right_half_pos += 1
new_position += 1
# empty remaning items in left list
while left_half_pos < len(left_half):
list_[new_position] = left_half[left_half_pos]
left_half_pos += 1
new_position += 1
# empty remaning items in right list
while right_half_pos < len(right_half):
list_[new_position]=right_half[right_half_pos]
right_half_pos += 1
new_position += 1
def function_three(x, max_):
assert 0 < x < max_
while x < max_:
x *= 2
def function_four(x):
for i in range(x):
for j in range(x - i):
for k in range(x - j):
if i + j + k == 0:
break
if i + j == 0:
break
if i == 0:
break
return x + 1Big-O value for function:
1)_one(list_): O(N^2) you can see that an outer loop that iterates through all the items in the input list and then a nested inner loop, which again iterates through all the items in the input list. The total number of steps performed is n * n, where n is the number of items in the input array.
2)_two(list_):
it's O(2^n), or exponential, since each function call calls itself twice unless it has been recursed n times
3)_three(x, max_):
It will be O(N) because it iterates through one loop.
4)_four(x)
O(inf) because it will never stop looping goes into infinity,
Happy Coding !!!
Comment down if you have queries over a answer!!!
Determine the Big-O value for the following functions. The valid Big-O options are: 'O(1)' 'O(N)' 'O(LOG2(N))'...
Python 3
5. (16 points) Determine the big-O running time of each of the following functions: def pi (a) for i in range(len (a)): print (a[i]) for i in range(len(a)): print (ali]) def p2(a): for i in rangeClen(a)): for j in a: print (ati].j) def p3(a): for i in a: for j in a: print (i,j) def p4(a): for i in range(len(a)): pi(a) def p5(a): for i in range(len(a)): p3 (a) def p6(a): for i in range(len(a)): p5(a) def p7...
without coding
Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr, low, high, x): # Check base case if low > high : return None else: mid = (high + low) // 2 element arr[mid] == X: if element return mid elif element > X: return binary-search(arr, low, mid 1, x) else: return binary_search(arr, mid + 1, high, x) Selection Sort: def selection_sort (arr): for i in range (len(arr)): smallest index = i smallest value...
PYTHON: Im stuck here, big O notation and runtime. What
is it and Why are they those? Please look at the pic, need help as
Im confused. Thank You!
def method3(n): for i in range(n): for j in range(100): for k in range(n): print(i+j+k) What is the runtime (tightest/closest bound in terms of O) for the above python function (method 3)? Please briefly explain. Enter your answer here def method4(n): for i in range(n): for j in range(n, o, -2):...
My tests for sorting algorithms are not working. sorting.py has 6 sorting algorithms and test_sorting.py has test cases to run those algorithms, but test cases are not working. please correct the errors in test_sorting.py file so that it can test all the sorting algorithms. WARNING: DON'T COPY AND PASTE THE QUESTION IN ANSWER. I WILL REPORT YOU. sorting.py # 1. selection sort # 2. insertion sort # 3. shell sort # 4. heap sort # 5. merge sort # 6....
How to prove log2(7 + 1/n) = Big Omega(1) I know that by definition to be Big omega of 1 the f(n) is not bounded by n to infinity, but its bounded to a constant C.
Analyze the following code fragments and write down the Big-O estimates of the following code fragments. Provide a concise explanation how you got your answer. c. for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) cout << (j + k) << endl; } d. while (n > 1) { k += n *3; n = n / 2; } e. int temp = n; for (int j...
Analyze the following code fragments and write down the Big-O estimates of the following code fragments. Provide a concise explanation how you got your answer. c. for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) cout << (j + k) << endl; } d. while (n > 1) { k += n *3; n = n / 2; } e. int temp = n; for (int j...
Determine whether each of these functions = O(x^3 ) Justify your answer f(x) = x^3 + 24. f(x)=√15x (x^2+1) f(x)=11√x log_2(x^11 ) f(x)= 4x! f(x)=7^x PROBLEM 4 Given the pseudocode, estimate its running time complexity in terms of Big-O (give tight bound). a) for ( i = 0; i < N; i++ ) { for ( j = 0; j < N; j++ ) { for ( k = 0; k < N; j++ ) { statement;// printing stuff }...
How would I prove that log2(32n) = O(n) (Big Oh of N) I got: 2 log2(3) n <= c * n , however, I do not know how to continue from this part. Thanks