Question

Consider an array 'a[n]' in which you are searching for the first occurrence of integer 'x'....

Consider an array 'a[n]' in which you are searching for the first occurrence of integer 'x'. With the given pseudocode:

for (i=0; I < n; i++)

   if (x==a[i]) return i;

if (i==n) return -2;

e) x=5; a[] = {3,2,5,7,1}; How many times the loop is executed?

f) x=5; a[] = {5,2,3,7,1]; How many times the loop is executed?

g) x=5; a[] = {6,2,3,7,1}; How many times the loop is executed?

h) Provide example of an input sequence which would result in the worst case scenario for this algorithm.

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

e)
x=5;
a[]={3,2,5,7,1}
The loop runs for 3 times
at i=0 not matched
at i=1 not matched
at i=2 , x is matched

f)
x=5;
a[ ]={5,2,3,7,1 }
The loop runs for one time
at i=0 , x=5 matched
  
g)
x=5 ; a[ ]={6,2,3,7,1}
The i=0, not matched to 5
at i=1, not matched to 5
at i=2, not matched to 5
at i=3 not matched to 5
at i=4 not matched to 5
the loop runs for 5 times.
  
h) The worst case scenario runs for 5 times without matching.
x=5, a[]={2,3,4,6,7}

Add a comment
Know the answer?
Add Answer to:
Consider an array 'a[n]' in which you are searching for the first occurrence of integer 'x'....
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
  • The input is an array of N integers ( sorted ) and an integer X. The...

    The input is an array of N integers ( sorted ) and an integer X. The algorithm returns true if X is in the array and false if X is not in the array. Describe an algorithm that solves the problem with a worst case of O(log N) time.

  • URGENT Question 3 25 pts ArrayMystery: Input: n: a positive integer Pseudocode: Let output be an...

    URGENT Question 3 25 pts ArrayMystery: Input: n: a positive integer Pseudocode: Let output be an empty array For i = 1 to n j = 1 While ij <= n Addj to the end of output j - j + 1 Return output Answer the following questions about the ArrayMystery algorithm above. a) How many times will the inner while loop iterate? You should express your answer in terms of i and n, using Big-Oh notation. Briefly justify your...

  • Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below....

    Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below. Count-Occurrence(A, n, k). count 0 for j = 1 to n if A[j] ==k count = count+1 print count Which of the following is the correct loop invariant for the for loop? At the start of each iteration jof the for loop, count represents the number of times that k occurs in the subarray A[1.j]. At the start of each iteration of the for...

  • 2. Assume you have an input array A with entries numbered 1 through n. One intuitive...

    2. Assume you have an input array A with entries numbered 1 through n. One intuitive way to randomize A is to generate a set of n random values and then sort the array based on these arbitrary mumbers. (a) (10 pts) Write pseudocode for this PERMUTE-BY-SorTInG method (b) (10 pts) Do best-, average, and worst-case analyses for this method. (c) Extra credit (15 pts): The algorithm RANDOMIZE-In-PLACE(A) A. length for i 1 to n 1 n= 2 swap Ali...

  • (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an...

    (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an integer between 1 and n. (2) Suppose Algorithm-1 does f(n) = n**2 + 4n steps in the worst case, and Algorithm-2 does g(n) = 29n + 3 steps in the worst case, for inputs of size n. For what input sizes is Algorithm-1 faster than Algorithm-2 (in the worst case)? (3) Prove or disprove: SUM{i**2} [where i changes from i=1 to i=n] ϵ tetha(n**2)....

  • Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1....

    Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1. Create an algorithm in pseudocode to solve the problem. 2. Identify the factors that would influence the running time of your algorithm. For example, if your algorithm is to search an array the factor that influences the running time is the array size. Assign names (such as n) to each factor. 3. Count the operations performed by the algorithm. Express the count as a...

  • 6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2)...

    6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2) will swap the values stored in the variables v1 and v2. Note that % is the modulus operation, and will return the integer remainder r of a/b, i.e., r-a%b Require: Array P with n > 0 values 1: i-1, j-n-l 2: while i<=j do for a=i to j by i do 4: 5: 6: 7: if Pla>Pat 11 and Pla]%2--0 then swap(Plal, Pla+1l) end...

  • 2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is...

    2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Gi pseudocode for an algorithm that will solve the following...

  • That tells how many swaps are done in the worst case given n elements Consider this modification ...

    solve 3 that tells how many swaps are done in the worst case given n elements Consider this modification of the partition algorithm. Randomly choose three potential pivots. Partition around the median of the three pivots 3. a) Write the pseudocode for this algorithm b) If this use the quicksort algorithm, what is the running time for the worst-case scenario? When will this happen? c) Why is this algorithm better than the regular quicksort algorithm? 4. Give the pseudocode of...

  • Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do...

    Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do if A[i]A[i+1] > A[i+2) then return i it i+1 return -1 4. Calculate how many times the comparison A[i]A[i+1] > A[i+2] is done for a worst-case input of size n. Show your work. 5. Calculate how many times the comparison A[i]A[i+1] > A[i+2] is done for a best-case input of size n. Show your work.

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