Question

Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an input sequence o
(a) Describe in English how the sequence of numbers is changed by the algorithm. (Hint: try the algorithm out on a small list
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The algorithm will execute the inner and outer loops until i<j that means the loop will execute till the complete array of numbers is scanned and if after complete scan i.e. one complete iteration i<j (index i still remains less than index j) then the numbers on corresponding indices will be swapped.

sample code

int main() 1 2 { 3 4. 5 6 int a[] = {2, 3, -2, 0, -1, 1, 6}; int p = -1; int n = sizeof(a) / sizeof(int); int i = 1; int j =

Solution(b):

As it can be clearly seen from the code snippet in part a solution that increment of i and decrement of j is dependent on the condition (i<j and a[i]<p) which means that the execution of these statements will depend on the input values given in the sequence list and not on the number of elements in the list.

The execution of statement i=i+1 or j=j-1 will be maximum when the array list is already sorted.

Example: {5,4,3,2,1,0} let p=0 (should be less than all elements in the list)

here i=i+1 will be executed n(5) times.

Similarly in {0,1,2,3,4,5} let p=0

here j=j-1 will be executed n times thus maximising the execution of these statements

Solution(C):

3) Yes answer depends on the numbers partially, because if the list is of all negative integers and p = 0 then i increases ti

Solution(d):

The asymptotic lower bound of the time complexity is omega(n) where n is the number of elements. It will be when the list is sorted as explained in part b solution.

The worst-case complexity is necessary to determine the maximum time taken by the algorithm to execute. As it is clearly seen from the algorithm the number of times i or j incremented in the worst case that many numbers of swaps are possible as it is under outer while(i<j) loop.

PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION

Add a comment
Know the answer?
Add Answer to:
Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an...
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
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
ADVERTISEMENT