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.
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
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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F5c1cc754-64b3-4c8b-8d95-a77ce12f8dce%2F853c6928-99ff-43f2-bb60-eabcc8f18cdb%2Fsjuw8ax_processed.png&w=3840&q=75)
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
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.Recommended textbooks for you
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education