Explain why the following algorithm is complete, correct, and finite.
Input: a list of n distinct integers a0 to an-1 ordered from least to greatest and an integer x
Output: the index in the list at which x is found, or -1 if x is not found
Procedure:
i = 0
while (i <= n-1 and x != ai)
i = i + 1
if i < n then location = i
else location = -1
return location
To prove an algorithm correct, the program should have
initialization, maintenance, and termination correct.
In this algorithm. This can be proved by the loop invariant
technique.
Loop Invariant: At the start of the iteration, the
variable 'i' does not hold any value. And at the end, every
iteration 'i' will have some value.
Initialization: Initialization is done for the
variable i.
Maintenance: Then, the value of x is matched with
ai and as soon as x matches with ai, then the 'i' index is
returned. Otherwise, if 'i' comes out to be zero only, it is
assumed that the index is not available and hence, returned as
-1.
Termination: The program terminates where the
maximum index reaches n-1 or the value matches ai.
To prove the completeness of the algorithm, it should have a
defined start, end and maintenance phase. Since, the program is
starting at a point where i=0 and it keep on increasing until the
value of 'i' reaches n-1 or value of x becomes ai. Hence, the
program is complete.
To prove that the algorithm is finite, we need to define that the program will have a terminate condition. Here, the termination condition is already defined. So, the program will never run infinitely.
Friend, this was a really nice question to answer. If you find my answer helpful, please like it. Thanks.
Explain why the following algorithm is complete, correct, and finite. Input: a list of n distinct...
Show that the following algorithm is correct, complete, and finite. Input: a list of n distinct positive integers a0, ..., an-1 Output: The largest even integers in the list, or 0 if there are no even integers Procedure: even= 0 for (i=0, i<n, i+=1) if ai%2 == 0 and ai > even even = ai return even
The following algorithm (Rosen pg. 363) is a recursive version of linear search, which has access to a global list of distinct integers a_1, a_2,..., a_n. procedure search(i, j, x : i,j, x integers, 1 < i < j < n) if a_i = x then return i else if i = j then 4. return 0 else return search(i + 1, j, x) Prove that this algorithm correctly solves the searching problem when called with parameters i = 1...
Meet is an operation between zero-one matrices and uses Boolean products (C. Join is an operation between zero-one matrices and uses Boolean products d. Join is an operation between zero-one matrices and uses conjunctions 29. Given set S (-2, -1, 0, 1, 2), R is a relation on S. i. R ( (x, y)| x-y 1} Write R as a set of ordered pairs. (Use roster method to write R as a set of tuples) 3pts ill. and matrix representation...
Describe an algorithm that takes as input a list of n integers and produces output the smallest integer in the list.
Consider the following problem: Input: a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers from 1 to n is missing in the list. Output: find the missing integer Let the input array be [2, 4, 1, 6, 3, 7, 8]. Elements in this list are in the range of 1 to 8. There are no duplicates, and 5 is missing. Your algorithm needs...
Search for 4 in the sequence {3,5,7,8,9,12,21,25}, by working through each step of the algorithm given below. Specify the values of i, j, m and am in each step. procedure binary search (x: integer, a1,a2,...,an: increasing integers) i := 1 {i is left endpoint of search interval } j := n {j is right endpoint of search interval } while i < j m := b(i + j)/2c if x > am then i := m + 1 else j...
8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za then anstwer-1 return answer 18 and the (a) Assume that this algorithm receives as input the numbersz-32 and corresponding sequence of integers 2 | 3 1 1 4151617| 8| 9 | 10 İ 11 İ 12 | 13 | 14|15 | 16 | 17 |...
9. (5 points) Please describe an algorithm that takes as input a list of n integers and finds the number of negative integers in the list. 10. (5 points) Please devise an algorithm that finds all modes. (Recall that a list of integers is nondecreasing if each term of the list is at least as large as the preceding term.) 11. (5 points) Please find the least integer n such that f() is 0(3") for each of these functions f()...
a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...
3. [15 marks] Describe an algorithm in pseudocode that locates an element in a list of increasing integers by successively splitting the list into four sublists of equal size (or as close to equal size as possible) and restricting the search to the appropriate piece. procedure tetrary search(x: integer, a1, a2, ..., an: increasing integers) (location is the subscript of the term equal to x, or 0 if not found) 3. [15 marks] Describe an algorithm in pseudocode that locates...