0% found this document useful (0 votes)
17 views

CS341 - Data Structures and Algorithms

Uploaded by

Athir Tarraz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

CS341 - Data Structures and Algorithms

Uploaded by

Athir Tarraz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SMU-MedTech Lab-CS341 L2-Spring24

CS341 Lab 2

• To calculate the execution time of java program you need to insert these two lines of codes
one as a starting point and the other as a finishing one.

// get the start time


long start = System.nanoTime();
// call the method
//***************************************//
// get the end time
long end = System.nanoTime();

• Afterwards to calculate the exact time you need to declare the following:

// execution time in seconds


long execution = (end - start);
System.out.println("Execution time of Recursive Method is");
System.out.println(execution + " nanoseconds");

The main objective of Lab 2 is testing the temporal execution of different algorithms with different
temporal complexities.

Exercise 1:

Write a java program that returns the first letter in a user defined string.

a. Insert two strings with VERY different lengths and calculate the time execution of your
program.
b. Save the result.

Exercise 2:

Write a java program of binary search algorithm.

a. Test your program on two different arrays sizes. Do not forget that your array needs to be
sorted.
b. Save the result of the execution time.

1
SMU-MedTech Lab-CS341 L2-Spring24

Exercise 3:

Write a java program that has a linear logarithmic complexity.

Exercise 4:

Write a java program that calculates the factorial of a user defined integer using a loop.

a. Insert two different numbers and calculate the time execution of your program.
b. Save the result.

Reminder:

N! = N x (N-1) x (N-2) x …. x 2 x 1 and N! = 0 if N = 0

Exercise 5:

Write a java program that returns the Nth Fibonacci number. The Nth Fibonacci Number can be
found using the recurrence relation shown above:

• if n = 0, then return 0.
• If n = 1, then it should return 1.
• For n > 1, it should return Fn-1 + Fn-2
a. Calculate the time of execution of your program.
b. Save the result.

Homework

Fill in the following table and upload it on Moodle.


Exercise Big O Cases Execution Screen shot
time

2
SMU-MedTech Lab-CS341 L2-Spring24

You might also like