please send answer for part b ii Q2 please send proper answer 0 and A[j] > K doA[j + 1] + A[j]j+j-1A[j] +3478 return Ai. Write the array A after each iteration of the outer For-loop, and state...



please send answer for part b ii Q2



please send proper answer



2.<br>(a) Write an algorithm in pseudo-code that<br>• takes as input an array A[1...n], and an item M which occurs at least once<br>in A.<br>• returns the index (position) of the last occurrence of the item M in array A.<br>The following table shows some examples with the expected output of the<br>algorithm (the last occurrences of M is underlined):<br>input: A<br>output<br>[1,8, 1, 7, 1, 9<br>[4, 3, 2, 2, 2, 3] 2<br>[9, 9]<br>[3, 3, 3, 3]<br>[4, 3, 3, 3]<br>1<br>9.<br>4<br>4<br>Your algorithm should perform as few comparisons as possible.<br>(b) Consider using the following algorithm on an array A = [5,:<br>Input: Array A[1...n]<br>Output: ??<br>1 for i + 2 to n do<br>K+ A[i]<br>j+i-1<br>while j > 0 and A[j] > K do<br>A[j + 1] + A[j]<br>j+j-1<br>A[j] +<br>3<br>4<br>7<br>8 return A<br>i. Write the array A after each iteration of the outer For-loop, and state what<br>you think the algorithm does. Show all your working.<br>ii. How many times will line 5 be executed for the array A? How many times<br>for an array of length n?<br>ii. How many times will line 4 be executed in the worst case for the array A?<br>How many times for an array of length n?<br>

Extracted text: 2. (a) Write an algorithm in pseudo-code that • takes as input an array A[1...n], and an item M which occurs at least once in A. • returns the index (position) of the last occurrence of the item M in array A. The following table shows some examples with the expected output of the algorithm (the last occurrences of M is underlined): input: A output [1,8, 1, 7, 1, 9 [4, 3, 2, 2, 2, 3] 2 [9, 9] [3, 3, 3, 3] [4, 3, 3, 3] 1 9. 4 4 Your algorithm should perform as few comparisons as possible. (b) Consider using the following algorithm on an array A = [5,: Input: Array A[1...n] Output: ?? 1 for i + 2 to n do K+ A[i] j+i-1 while j > 0 and A[j] > K do A[j + 1] + A[j] j+j-1 A[j] + 3 4 7 8 return A i. Write the array A after each iteration of the outer For-loop, and state what you think the algorithm does. Show all your working. ii. How many times will line 5 be executed for the array A? How many times for an array of length n? ii. How many times will line 4 be executed in the worst case for the array A? How many times for an array of length n?

Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here