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 1 to n - 1 do
Line 6: small ← Math.min(small, A[I])
Line 7: return large and small
improve above algorithm to decrease the number of comparisons
and Compare both for the time used in milliseconds for running 100 randomly generated lists of each input size 102, 104, and 106.
import java.util.Random;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Vamsi
*/
public class MaxMin {
//finds the max and min in a[]
public static void Max_Min_Ver1(int a[],int n)
{
int large = a[0];
int c=0;//to count comparisions
for(int i=1;i<n;c++,i++)
if(large<a[i])//1 comparision herr
{
large =a[i];
}
int small = a[0];
for(int i=1;i<n;c++,i++)
if(a[i]<small)//comparision
{
small = a[i];
}
System.out.println("Max is :"+large+"\nMin is
:"+small+"\nComparisions:"+c);
}
//improved method
public static void Max_Min_Ver2(int a[],int n)
{
int large = a[0];
int small = a[0];
int c=0;//to count comparisions
for(int i=1;i<n;c++,i++)
if(large<a[i])//1 comparision here
{
large =a[i];
}
else if(a[i]<small)//comparision here
{
small = a[i];
c++;
}
System.out.println("Max is :"+large+"\nMin is
:"+small+"\nComparisions:"+c);
}
//method to fill array with random numbers
public static void fill(int a[])
{
Random r = new Random();//to generate random numbers
for(int i=0;i<a.length;i++)
{
a[i] =r.nextInt()%1000;
}
}
public static void main(String argv[])
{
//arrays with different sizes
int a[] = new int[102];
int b[] = new int[104];
int c[] = new int[106];
//testing on 100 different list
for(int i=1;i<=1;i++)//change here 1 to 100, to generate 100
different tests
{
System.out.println("Test"+i+"\n");
fill(a);//filling a
System.out.println("Results for array A");
System.out.println("Max-Min-VER1");
Max_Min_Ver1(a,a.length);
System.out.println("Max-Min-VER2");
Max_Min_Ver2(a,a.length);
System.out.println();
fill(b);//filling b
System.out.println("Results for array B");
System.out.println("Max-Min-VER1");
Max_Min_Ver1(b,b.length);
System.out.println("Max-Min-VER2");
Max_Min_Ver2(b,b.length);
System.out.println();
fill(c);//filling c
System.out.println("Results for array C");
System.out.println("Max-Min-VER1");
Max_Min_Ver1(c,c.length);
System.out.println("Max-Min-VER2");
Max_Min_Ver2(c,c.length);
System.out.println();
}
}
}
output:
run:
Test1
Results for array A
Max-Min-VER1
Max is :998
Min is :-990
Comparisions:202
Max-Min-VER2
Max is :998
Min is :-990
Comparisions:105
Results for array B
Max-Min-VER1
Max is :997
Min is :-967
Comparisions:206
Max-Min-VER2
Max is :997
Min is :-967
Comparisions:110
Results for array C
Max-Min-VER1
Max is :962
Min is :-991
Comparisions:210
Max-Min-VER2
Max is :962
Min is :-991
Comparisions:108
BUILD SUCCESSFUL (total time: 1 second)
import java.util.Random;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Vamsi
*/
public class MaxMin {
//finds the max and min in a[]
public static void Max_Min_Ver1(int a[],int n)
{
int large = a[0];
int c=0;//to count comparisions
for(int i=1;i<n;c++,i++)
if(large<a[i])//1 comparision herr
{
large =a[i];
}
int small = a[0];
for(int i=1;i<n;c++,i++)
if(a[i]<small)//comparision
{
small = a[i];
}
System.out.println("Max is :"+large+"\nMin is
:"+small+"\nComparisions:"+c);
}
//improved method
public static void Max_Min_Ver2(int a[],int n)
{
int large = a[0];
int small = a[0];
int c=0;//to count comparisions
for(int i=1;i<n;c++,i++)
if(large<a[i])//1 comparision here
{
large =a[i];
}
else if(a[i]<small)//comparision here
{
small = a[i];
c++;
}
System.out.println("Max is :"+large+"\nMin is
:"+small+"\nComparisions:"+c);
}
//method to fill array with random numbers
public static void fill(int a[])
{
Random r = new Random();//to generate random numbers
for(int i=0;i<a.length;i++)
{
a[i] =r.nextInt()%1000;
}
}
public static void main(String argv[])
{
//arrays with different sizes
int a[] = new int[102];
int b[] = new int[104];
int c[] = new int[106];
//testing on 100 different list
for(int i=1;i<=1;i++)//change here 1 to 100, to generate 100
different tests
{
System.out.println("Test"+i+"\n");
fill(a);//filling a
System.out.println("Results for array A");
System.out.println("Max-Min-VER1");
Max_Min_Ver1(a,a.length);
System.out.println("Max-Min-VER2");
Max_Min_Ver2(a,a.length);
System.out.println();
fill(b);//filling b
System.out.println("Results for array B");
System.out.println("Max-Min-VER1");
Max_Min_Ver1(b,b.length);
System.out.println("Max-Min-VER2");
Max_Min_Ver2(b,b.length);
System.out.println();
fill(c);//filling c
System.out.println("Results for array C");
System.out.println("Max-Min-VER1");
Max_Min_Ver1(c,c.length);
System.out.println("Max-Min-VER2");
Max_Min_Ver2(c,c.length);
System.out.println();
}
}
}
output:
run:
Test1
Results for array A
Max-Min-VER1
Max is :998
Min is :-990
Comparisions:202
Max-Min-VER2
Max is :998
Min is :-990
Comparisions:105
Results for array B
Max-Min-VER1
Max is :997
Min is :-967
Comparisions:206
Max-Min-VER2
Max is :997
Min is :-967
Comparisions:110
Results for array C
Max-Min-VER1
Max is :962
Min is :-991
Comparisions:210
Max-Min-VER2
Max is :962
Min is :-991
Comparisions:108
BUILD SUCCESSFUL (total time: 1 second)
Below is a linear time complexity algorithm Max-Min-VER1 to find the biggest and smallest element a...
Explain how you get the answer please. Thank you 5. Find the time complexity, big-9 function, of the algorithm of finding the maximum element in a finite sequence. (Determine the number of comparisons.) (10 points) LGORITIMs Finding the Maximum Element in a Finite Sequence procedure maaistegers for 2 to " return maxf max is the larpest eleet 5. Find the time complexity, big-9 function, of the algorithm of finding the maximum element in a finite sequence. (Determine the number of...
Analyze the time complexity of the following algorithm. You may assume that the floor function in line 2 takes Theta (1) time. Please show your work. Input: data: array of integers Input: n: size of data Output: median of data 1 Algorithm: MedianSelect 2 lim = [n/2] + 1 3 min = - infinity 4 for i = 1 to lim do 5 prev = min 6 min = infinity 7 for j = 1 to n do 8 if...
1.we need n-1 comparisons at most 3*n/2comparisons suffice to find both the min and max Basic Strategy: Maintain the minimum and maximum of elements seen so far. Don't compare each element to the minimum and maximum separately. Process elements in pairs. Compare the elements of a pair to each other. Then compare the larger element to the maximum so far, and compare the smaller element to the minimum so far. This leads to only 3 comparisons for every 2 elements...
Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an input sequence of numbers. MysteryAlgorithm Input: a1, a2....,an n, the length of the sequence. p, a number Output: ?? i != 1 j:=n While (i < j) While (i <j and a < p) i:= i + 1 End-while While (i <j and a 2 p) j:=j-1 End-while If (i < j), swap a, and a End-while Return( aj, a2,...,an) (a) Describe in English...
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...
Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MAX) Marks (20,24,26,19,5,31,24,32,32,45 print (MaxVal (Marks) a. Express the number of operations in terms of a function f(n), where n is the input size. (1 Mark) b. What is the total number of operations in the for loop of the algorithm...
Here is a recursive algorithm that answers the same question as posed on Group HW3, finding the number of people who are taller than everyone before them in line. NumCanSeeRec(a1,... , an : list of n 2 1 distinct heights) (a) ifn -1 then (b return 1 (c) c= ŅumCanSeeRee(a1, , an-1) d) for i:- 1 ton- 1 (e) if a, an then return c (g) return c+1 Answer the following questions about this algorithm. Please show your work. (a)...
3. Calculate the time complexity of the following algorithm: 1. Initialize sum to 0. I 2. Input the value of n. 3. While i = 0 to n-1 do a. sum=sumti; ; b. Increment i
2. Here is a sorting algorithm that I like to use. Given an unsorted list of size n, let Xx represent the data in location k of the unsorted list. Compare xi to X2 and switch if necessary so that they are in sorted order, smallest first. Compare Xn-1 and Xn and switch if necessary so that they are in sorted order, smallest first. • Compare x3 with its left neighbors, switching if necessary so that the 3 first entries...
Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...