Lab 1 - Chapter 4 The Efficiency of Algorithms 01/24/2023 In this lab, you will practice how to measure the running time of a section of code in Java. One approach is to use System.nanoTime() where the current time is stored as a long integer equals to the number of nanoseconds. By subtracting the starting time in nanoseconds from the ending time in nanoseconds, you get the run time in nanoseconds of a section of code. For example, suppose that AlgorithmA is the name of a method you wish to time. The following statements will compute the number of nanoseconds that AlgorithmA requires to execute: public static void main(String[] args) { int n1 = 10, n2 = 100, n3 = 1000, n4 long n1Time, n2Time, n3Time, n4Time; n1Time = AlgorithmA (nl); } public static long AlgorithmA (int n) { long startTime, endTime, elapsedTime; startTime = System.nanoTime (); int sum = 0; for (int i = 1; i <= n; i++) { sum sum + i; } 1. } endTime = System.nanoTime (); // Get current time in nanosecond elapsedTime = endTime- startTime; // Elapsed time calculation return elapsedTime; Algorithm A Write a Java program that implements three different algorithms below in Figure 4-1 and times them for various values of n (10, 100, 1000, 10000). The program should display a table of the run times of each algorithm for the values of n. Figure 4-1 sum = 0 for i=1 to n sum = sum + 1 Algorithm B sum = 0 for i=1 to n { 10000; for j = 1 to 1 // Get current time in nanosecond sum sum +1 Algorithm C sum = n(n+1) / 2 } Three algorithms for computing the sum 1+2+...+n for an integer n > 0 2. By midnight, Tuesday, Jan 24th, submit your Java source file and a screenshot of the execution result via 'Submit Lab Assignments' in the course Blackboard.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
CSCI 2436:01L Data Structures Lab
Lab 1 - Chapter 4 The Efficiency of Algorithms
In this lab, you will practice how to measure the running time f a section of code in Java. One
approach is to use System.nano Time() where the current time is stored as a long integer equals to
the number of nanoseconds. By subtracting the starting time in nanoseconds from the ending time in
nanoseconds, you get the run time-in nanoseconds of a section of code.
public static void main(String[] args) {
int n1 = 10, n2 = 100, n3 = 1000, n4 = 10000;
long n1Time, n2Time, n3Time, n4Time;
n1Time AlgorithmA (nl);
For example, suppose that AlgorithmA is the name of a method you wish to time. The following
statements will compute the number of nanoseconds that AlgorithmA requires to execute:
}
public static long AlgorithmA (int n) {
long startTime, endTime, elapsedTime;
startTime = System.nanoTime ();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum sum + i;
}
1.
}
Algorithm A
endTime= System.nanoTime ();
// Get current time in nanosecond
elapsedTime = endTime - startTime; // Elapsed time calculation
return elapsedTime;
sum = 0
for i=1 to n
Write a Java program that implements three different algorithms below in Figure 4-1 and
times them for various values of n (10, 100, 1000, 10000). The program should display a table of
the run times of each algorithm for the values of n.
Figure 4-1
sum sum + 1
Algorithm B
sum = 0
for i=1 to n
{
}
for j = 1 to i
Spring 2023
sum sum + 1
01/24/2023
// Get current time in nanosecond
Algorithm C
sum = n(n+1) / 2
Three algorithms for computing the sum 1+2+...+n for an integer n > 0
1
2. By midnight, Tuesday, Jan 24th, submit your Java source file and a screenshot of the
execution result via 'Submit Lab Assignments' in the course Blackboard.
Transcribed Image Text:CSCI 2436:01L Data Structures Lab Lab 1 - Chapter 4 The Efficiency of Algorithms In this lab, you will practice how to measure the running time f a section of code in Java. One approach is to use System.nano Time() where the current time is stored as a long integer equals to the number of nanoseconds. By subtracting the starting time in nanoseconds from the ending time in nanoseconds, you get the run time-in nanoseconds of a section of code. public static void main(String[] args) { int n1 = 10, n2 = 100, n3 = 1000, n4 = 10000; long n1Time, n2Time, n3Time, n4Time; n1Time AlgorithmA (nl); For example, suppose that AlgorithmA is the name of a method you wish to time. The following statements will compute the number of nanoseconds that AlgorithmA requires to execute: } public static long AlgorithmA (int n) { long startTime, endTime, elapsedTime; startTime = System.nanoTime (); int sum = 0; for (int i = 1; i <= n; i++) { sum sum + i; } 1. } Algorithm A endTime= System.nanoTime (); // Get current time in nanosecond elapsedTime = endTime - startTime; // Elapsed time calculation return elapsedTime; sum = 0 for i=1 to n Write a Java program that implements three different algorithms below in Figure 4-1 and times them for various values of n (10, 100, 1000, 10000). The program should display a table of the run times of each algorithm for the values of n. Figure 4-1 sum sum + 1 Algorithm B sum = 0 for i=1 to n { } for j = 1 to i Spring 2023 sum sum + 1 01/24/2023 // Get current time in nanosecond Algorithm C sum = n(n+1) / 2 Three algorithms for computing the sum 1+2+...+n for an integer n > 0 1 2. By midnight, Tuesday, Jan 24th, submit your Java source file and a screenshot of the execution result via 'Submit Lab Assignments' in the course Blackboard.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education