Question
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
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 //n 1 iterations
Line 3: large Math.max(large, A[I]) // one comparison
Line 4: small A[0]
Line 5: for I from 1 to n - 1 do
Line 6: small Math.min(small, A[I])
Line 7: return large and small
- Implement this algorithm (java)
- Answer the question - How many comparisons are used in the algorithm?
Improve the above Max-Min-VER1(A, n) to decrease the number of comparisons. Name your algorithm Max-Min-VER2.
Requirements
- using the format show in ( to write your pseudocode)
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-VER2(A, n)
Line 1:
Line 2:
Line 3:
Line 4:
Line 5:
- implementation of Max-Min-VER2 (you may use any language, such as Java, C++, C#, python etc)
- Compare Max-Min-VER1 and Max-Min-VER2 using a table or graph for the time used in milliseconds for running 100 randomly generated lists of each input size 102, 104, and 106.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started