Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The algorithm S(A, n, i) selects all the j-th smallest elements (with j <= i) fr

ID: 3563539 • Letter: T

Question

The algorithm S(A, n, i) selects all the j-th smallest elements (with j <= i) from an array A of n elements, by using linearselect to select each of the j-th smallest elements (with j <= i). One could also implement S alternatively as T (A, n, i), which first sort A (on average-case and on worst- case, the sorting takes time O(n log n) using mergesort) and then select the first i elements. Compare the average-case space complexities of the two algorithms; i.e., For the average-case complexities, under what conditions (on the choices for i), S is better than T or vice versa.

Explanation / Answer

Design an algorithm to search a given value in an array.


Solution:


Assumptions:

Let the array A is unordered.


Algorithm Name: LinearSearchArray(A, N, LB, UB, X, R)

Input : A is any numeric array with N number of elements, we have to search X in the given array A. LB & UB are the Lower Bound & Upper Bound of the array segment where we are going to search for the element X.

Output: R will hold -1 if X is not found in the array A, otherwise R will hold the index of X in the given array A.


Step 1: R = -1

Step 2: i =LB

Step 3: WHILE ( i < UB )

          IF ( A[i] = X ) Then

             R = i

             Return

          EndIf

          i = i + 1

        EndWhile

Step 4: Return

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote