a)
Algorithm->
median(a,b,c)
if a>b and a<c then return a
if a>c and a<b then return a
if b>a and b<c then return b
if b>c and b<a then return b
return c
b) Dn = {a,b,c} in any order
c) assuming two comparison in one statements, if c is median, it requires 8 comparisons
d) 3 comparison required in minimum
if
(a > b)
{
if
(b > c)
return
b;
else
if
(a > c)
return
c;
else
return
a;
}
else
{
//
Decided a is not greater than b.
if
(a > c)
return
a;
else
if
(b > c)
return
c;
else
return
b;
}
Example 1.9: 1.23 "The median of an ordered set is an element such that the number...
Given two arrays A and B of n integers both of which are sorted in ascending order. Write an algorithm to check whether or not A and B have an element in common. Find the worst case number of array element comparisons done by this algorithm as a function of n and its Big-O complexity
4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...
Consider an ordered array A of size n and the following ternary search algorithm for finding the index i such that A[i] = K. Divide the array into three parts. If A[n/3] > K. the first third of the array is searched recursively, else if A[2n/3] > K then the middle part of the array is searched recursively, else the last thud of the array is searched recursively. Provisions are also made in the algorithm to return n/3 if A[n/3]...
I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...
(10 pts.) Count the worst-case number of array element comparisons (A[j] < A[j-1]) made by InsertionSort on arrays of size n: void InsertionSort(int A[], int n) { for (int i = 1; i < n; ++i) for (int j = i; j > 0 && A[j] < A[j-1]; --j) swap(A[j], A[j-1]); } Do the same for the number of swap's. 2. Which function grows faster: 2^((lg?))2 or ?^(2019)? Justify your answer. 3. Use "name and conquer" to give a derivation...
Disclaimer: This is just One question with multiple parts. It is not multiple questions in one post. Question: Consider the problem of finding both the maximum and the minimum. We will show that you can not find them both in less than (about) 3n/2 comparisons. Consider the run of any algorithm and defined the following sets that depend on the comparison the algorithm chose. Let N be the collection of numbers. We denote n = |N |. After comparisons were...
And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for the Partition subroutine of QuickSort, applied to an array A. .Let n be the number of elements of the array A. If n 24, perform an Insertion Sort of A and return. Otherwise: Choose 2n/2)| elements at random from n; let S be the new list with the chosen elements. Sort the list S using Insertion Sort and use the median m of S...
4. (7 pts) Let A be an array of 5 integers, whose contents are as follows: 3, 2, 1, 5, 4. We will apply insertion sort to sort this array. Show all of the element-wise comparisons made by the algorithm in the correct order. Here an element-wise comparison means the comparison of one element of the array with another element of the array or the key set in a particular step of the algorithm. Since the algorithm may move the...
(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)....
Copy of Consider the following algorithm: i+2 while (x mod i)=0 do iti+1 Now suppose x is an element from the set {n EN|2sn s50). What is the worst-case number of comparisons that this algorithm will perform? O O O O O O