a)
Elements(A[]) //The function Element accepts an array A[]
{
if(A.length%2==0) // if the length of Array is even
if(A[0]<A[i]) // if element in the 0th pos < element in 1st pos then,
min=A[0] //initialize min as element in the 0th pos
max=A[1] //initialize max as element in the 1st pos
else
min= A[1] //initialize min as element in the 1st pos
max=A[0] //initialize max as element in the 0th pos
else // else if the length of Array is odd
min=A[0] //initialize min as element in the 0th pos
max=A[0] //initialize max as element in the 0th pos
for i = 2 to A.length-1 // from i = 2 till ((Length of Array A) - 1) (because null element in pos length of array)
{
//Compare current pair to find the who is maximum and who is minimum.
if A[i]>A[i+1] //if element at i > element at i+1
maxTemp= A[i] //maxTemp is a temporary value to store max from the current pair
minTemp= A[i+1] //minTemp is a temporary value to store min from the current pair
else //if element at i < element at i+1
maxTemp = A[i+1]
minTemp = A[i]
//compare the maxTemp with original max and minTemp with original min
if maxA > max //if maxTemp > original max
max = maxA //original max = maxTemp
if minA < min //if minTemp < original min
min = minA //original min = minTemp
i+=2 //get the next pair by incrementing the value of i 2 times
}
//now we have the maximum and minimum value in max and min variables
}
b)
Elements(A[]) //The function Element accepts an array A[]
{
if(A.length%2==0)
if(A[0]<A[i])
min=A[0]
max=A[1]
else
min= A[1]
max=A[0]
else
min=A[0]
max=A[0]
//Time taken by the above if and else is constant since it is executed only once hence we ignore the time
for i = 2 to A.length-1 //The value of i increments 2 times on each loop hence the executed n/2 times
{
// either if or else is executed on 1 comparison hence 1 execution
if A[i]>A[i+1]
maxA= A[i]
minA= A[i+1]
else
maxA= A[i+1]
minA= A[i]
// if is executed once hence 1 execution
if maxA > max
max = maxA
// if is executed once hence 1 execution
if minA < min
min = minA
i+=2
//adding the executions within the forloop for each if we get 1+1+1 =3
}
// 3 executions per loop and loop executes n/2 times
// Hence total execution is 3*n/2
}
1.we need n-1 comparisons at most 3*n/2comparisons suffice to find both the min and max Basic...
Below is a linear time complexity algorithm Max-Min-VER1 to find the biggest and smallest element a given list. Input: A is a given list Output: two integers Example: Given A = {1, 5, 9, -3}. It returns -3 (the smallest element), and 9 (the biggest element) Max-Min-VER1(A, n) Line 1: large ← A[0] Line 2: for I from 1 to n - 1 do Line 3: large ← Math.max(large, A[I]) Line 4: small ← A[0] Line 5: for I from...
help ASAP for my test Suppose we are investigating max./min. behavior of a function (1). We intend using the first derivative test, and have gleaned the following information in preparation for applying the test. Interval Test value Sign behavior of f'() of !") of (2) 7-20,-2) f'(-10) = -0.5 (-2,0) f'(-1) = -3 (0,2) SO = 2 + (2,00) f'(5) <0 Apply the first derivative using the information in the table to select the appropriate conclusion for each critical point....
write in java 1. Assume the availability of a method named makeLine that can be passed a non-negative integer n and a character c and return a String consisting of n identical characters that are all equal to c. Write a method named printTriangle that receives two integer parameters n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...
C++ Question 5 5 pts In a min-heap of N elements, if we want to find the max element, we have to search all the leaves. What is the big-o running time of findMax? O(N^2) Oſlog N) O(N) OIN log N) Question 6 5 pts An AVL tree is a Binary Search Tree that has the following additional property for every node in the tree, the height of the left and right subtrees is the same none of the above...
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...
Subject: Algorithm. solve only part 3 and 4 please. 2.2 Selection- 5 points each 1. Run the simultaneous min-and-max algorithm on the array A 4, 2, 12, 6, 13,9,15). (16, 7, 10, 1,5, 11,3,8, 14, 2. Explain why the above algorithm is better than the naive algorithm for finding minimum and maximum separately. How many comparisons does the naive algorithm do? How many comparisons does the simultaneous min and max do? 3. Use the randomized select algorithm based on partition...
I'm not too sure what the question is asking for when calculating for S, we need to use MatLab for writing the script. If anybody could be of some assistance, I need the script, and the sample output to match 8. Let n be the number of elements in a data set and let x denote an individual element, for i 1,2,. .. , n. The mean /u and standard deviation s are given by ηΣ α-(ΣΗ ) n(n -...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the console (inclusive). The identified prime numbers must be stored in an array . The inputted number should be between 0 and 1000 (inclusive). array is large enough to o Make sure your hold all the prim e numbers. Dynamic memory allocation is not required for this assignment, so you can have unused space in your array Make sure you can handle the cases of...
Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...