In: Computer Science
I have a set of 100,000 random integers. How long does it take to determine the maximum value within the first 25,000, 50,000 and 100,000 integers? Will processing 100,000 integers take 4 times longer than processing 25,000? You may use any programming language/technique. It is possible the time will be very small, so you might need to repeat the experiment N times and determine an average value for time. Complete this table. Source code is not needed.
Number |
Time |
25,000 |
|
50,000 |
|
100,000 |
|
Language/Tool used: |
|
Technique used: |
Number | Time |
25,000 | 2 msec |
50,000 | 2 msec |
100.000 | 4 msec |
Language/Tool Used: | JAVA/Ecilipse |
Technique Used: | Comparison |
if you want source code
/**********************MaxRandom.java****************************/
import java.util.Random;
import java.util.Scanner;
public class MaxRandom {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// prompt for limit
System.out.print("Enter the limit:
");
// enter limit
int limit = scan.nextInt();
// set max number to minimum
value
int maxNumber =
Integer.MIN_VALUE;
Random random = new Random();
// start time
long startTime =
System.currentTimeMillis();
// generate random and compare with
max and set max if greater than max
for (int i = 0; i < limit; i++)
{
int
randomNumber = random.nextInt(100000);
if (maxNumber
< randomNumber) {
maxNumber = randomNumber;
}
}
// end time
long endTime =
System.currentTimeMillis();
// print value and time taken
System.out.println("Max number " +
maxNumber + " found in: " + (endTime - startTime) + " msec");
scan.close();
}
}
/*********************output*********************/
Enter the limit: 100000
Max number 99999 found in: 4 msec
Please let me know if you have any doubt or modify the answer, Thanks :)