Question

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a) The best case running time is Θ(n)
(b) The worst case running time is also Θ(n)
(c) If L is already sorted, the the max element is the last element in the list. The function can return L[n-1]. This algorithm is useless as to sort the list it will take O(nlogn) time to sort itself. So total time to find the max element will be O(nlogn).

Add a comment
Know the answer?
Add Answer to:
Consider the following Python function: def find_max (L): max = 0 for x in L: if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a python code function that selects top ten max numbers from a sequence of n...

    Write a python code function that selects top ten max numbers from a sequence of n integers, test it, and determine the Big-Oh of running time for the algorithm (Do not have to prove) -1/i and Write a python recursive function for computing the nth Harmonic number, Hn= test it.

  • Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MA...

    Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MAX) Marks (20,24,26,19,5,31,24,32,32,45 print (MaxVal (Marks) a. Express the number of operations in terms of a function f(n), where n is the input size. (1 Mark) b. What is the total number of operations in the for loop of the algorithm...

  • Consider the following Python program, where x is assumed to be a list of integers. def...

    Consider the following Python program, where x is assumed to be a list of integers. def mystery_func(x): n = len(x) for i in range(n - 1): for j in range(i + 1, n): if x[j] - x[i] < j - i: return False return True 3a. In plain English, describe what it accomplishes (i.e., the relationship between input and output, not how the code works). 3b. Analyze the running time and express it with big O. 3c. Rewrite this function...

  • 1. What is the worst case time complexity of insertion into a binary search tree with...

    1. What is the worst case time complexity of insertion into a binary search tree with n elements? You should use the most accurate asymptotic notation for your answer. 2. A binary search tree is given in the following. Draw the resulting binary search tree (to the right of the given tree) after deleting the node with key value 8. 10 3. You have a sorted array B with n elements, where n is very large. Array C is obtained...

  • Question3 10 pts Let A [L.n] be a max-heap with n > 1 and consider the index ị such that l 〈 i 〈 n . Assume that a...

    Question3 10 pts Let A [L.n] be a max-heap with n > 1 and consider the index ị such that l 〈 i 〈 n . Assume that all the elements of A are distinct. Write the pseudocode of an algorithm which replaces A [i] by A i 100 and then re-arranges the elements of A into a max-heap. The running time of your algorithm must be O (log, n Upload Choose a File Question3 10 pts Let A [L.n]...

  • problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to...

    problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to write its pseudo-code). To sort an array A, you will then call Det-QuickSort(A, 1, n). You also need to provide the worst case time complexity analysis of your algorithm. 2. (20 points) Given a set of n distinct numbers, we wish to find the k largest in sorted order using a comparison-based algorithm. Give an algorithm that implements each of the following methods, and...

  • For each problems segment given below, do the following: Create an algorithm to solve the problem...

    For each problems segment given below, do the following: Create an algorithm to solve the problem Identify the factors that would influence the running time, and which can be known before the algorithm or code is executed. Assign names (such as n) to each factor. Identify the operations that must be counted. You need not count every statement separately. If a group of statements always executes together, treat the group as a single unit. If a method is called, and...

  • 2. Here is a sorting algorithm that I like to use. Given an unsorted list of...

    2. Here is a sorting algorithm that I like to use. Given an unsorted list of size n, let Xx represent the data in location k of the unsorted list. Compare xi to X2 and switch if necessary so that they are in sorted order, smallest first. Compare Xn-1 and Xn and switch if necessary so that they are in sorted order, smallest first. • Compare x3 with its left neighbors, switching if necessary so that the 3 first entries...

  • Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_...

    Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_ Terminal + A sequential search of a sorted list can halt when the target is less than a given element in the list. 1 " 2 File: search.py 3 Project 11.1 4 5 Optimizes sequential search for sorted lists. 6 The complexity is O(n) in the worst case and 0(1) in the best case. 7 The average case is less than n / 2,...

  • 01 def head(Q):   02   return Q[0]      03 def tail(Q):   04   return Q[1:]      05 def mergesort(U):   06   if...

    01 def head(Q):   02   return Q[0]      03 def tail(Q):   04   return Q[1:]      05 def mergesort(U):   06   if U == [] or tail(U) == []:   07     return U   08   else:   09     L = []   10     R = []   11     while U != [] and tail(U) != []:   12       L = L + [head(U)]   13       U = tail(U)   14       R = R + [head(U)]   15       U = tail(U)   16     L = L + U   17     L = mergesort(L)   18     R = mergesort(R)   19     S = []   20     while L != [] and R != []:   21       if head(L)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT