You are on page 1of 8

Assignment#1 for Algorithms and Data Structures

Dear Ladies and Gentlemen!


Good Luck!
1) (15 points)
a) (5 points)
Write a recursive algorithm that returns the sum of the squares of positive and
negative integers in the range of 𝑎 and 𝑏, ∀ 𝑎 < 𝑏. In other words: ∑𝑏𝑖=𝑎 𝑖 2
Input 1: 𝑎 = −2; 𝑏 = 5
Output 1: 60
Input 2: 𝑎 = 3; 𝑏 = 5
Output 2: 50
Input 3: 𝑎 = 4; 𝑏 = 2
Output 3: “Wrong inputs, try again” //because 𝑎 > 𝑏
Input 4: 𝑎 = −6; 𝑏 = −2
Output 4: 90
b) (5 points)
Write an interative analogue of the above algorithm with the same condition
c) (5 points)
Write a specific formula analogue of the above algorithm with the same
condition. Hints:
𝑛(𝑛+1)(2𝑛+1)
12 + 22 + ⋯ + 𝑛2 =
6

∑5𝑖=−2 𝑖 2 = ∑5𝑖=1 𝑖 2 + ∑2𝑖=1 𝑖 2

2) (10 points)
a) (5 points)
Write a recursive algorithm that return the sum of first 𝑛 terms of Fibbonachi
sequences:
Input 1: 𝑛 = 5
Output 1: 7 // because 0+1+1+2+3 = 7
Input 2: 𝑛 = -4
Output 2: “Enter positive n”
b) (5 points)
Write an iterative analogue of the above algorithm with the same condition

3) (15 points)
Write a recursive algorithm of calculating the sum of digits of given positive
or negative integer 𝑛. When solving this problem, you cannot use strings, lists, arrays
and cycles, of course.
Input 1: 𝑛 = 285
Output 1: 15 // 2+8+5 = 15
Input 2: 𝑛 = −18
Output 2: 9 // 1+ 8 = 9
Input 3: 𝑛 = 2.8
Output 3: “It is not integer. Please try again”
4) (20 points)
Suppose that you have the following recursive algorithm for finding sum of
two integers 𝑎 and 𝑏 without directly using “+” operator.

Figure 1 – Recursive code of summing two integers


a) (10 points)
Firstly describe how this algorithm is working. For example,you can use tree
for your explanation ( it’s time for your creativity:) ).
b) (10 points)
It is necessary to analyze the above code and find the main drawback (like in
which case above algorithm can’t work) of this algorithm. Why do we have such
disadvantage? Any ideas of how can we avoid such problem? Please write maximum
3 sentences of your idea or you can even write real working code solution or write a
pseudocode of your idea ( it’s time for your creativity:) ).
Hint: here you should manipulate (by changing) with 𝑎 and 𝑏 values.
5) (30 points)
Suppose that you have the following BubbleSort algorithm code:

import java.util.Random;
public class BubbleSort {
public static int randomNumberInRange(int min, int max) {
Random random = new Random();
return random.nextInt((max - min) + 1) + min;
}

public static void main(String[] args) {


Integer[] arr;
int n = 50000;
arr = new Integer[n];
int temp, i, j, k;
for (i = 0; i < n; i++) {
k = randomNumberInRange(999, 9999);
arr[i] = k;
}

System.out.println("Sorted array: ");


Stopwatch stopwatch = new Stopwatch();
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
double time = stopwatch.elapsedTime();
System.out.println("Time in ms = " + time * 1000); //time in miliseconds
}
}
a) (5 points)
Firstly, it is necessary to run above algorithm on your computer. And then please
fulfill the following table which is related to your computer characteristics:

Processor
RAM
Type of system
Operating
System

For example, in my case:

Processor 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz 2.80 GHz


RAM 16 GB
Type of system x64
Operating Windows 11
System

Figure 2 – Computer Characteristics

A teacher needs to know such information in order to have an idea on which


computer you will run this code.

b) (5 points)
By changing size of your array 𝑛, it is necessary to fulfill the following table:

𝑛 𝑇(𝑛) in ms
100
200
400
800
1600
3200
6400
12800

P.S Here it is expected that each student should have the unique table, because of
you are running on your personal computers and logically you will have different
computer characteristics. More generally, computers with high RAM can execute
the above code faster. Logically, low RAM computers should give you long
runnning time. But of course, there are also exists other characteristics as how many
parallel processes your computer runs and etc.

c) (5 points)
Now it is necessary to display the above table in terms of 2D-graph. Hint: for
example, I used MS Excel, you can see demonstration below:

Figure 3 – 2D graph representation.

You can use any tools for visualizing your graph: Python, Excel, yotx.ru and
etc. (it’s time for your creativity:))
P.S I hope that your graph more or less will have the same behaviour.

d) (5 points)
Then it is necessary to convert 𝑁 and 𝑇(𝑁) into log(𝑁) and log(𝑇(𝑁)). And
again display in terms of graph:
Figure 3 – 2D graph representation in terms of log-scale.

You can use any tools for visualizing your graph: Python, Excel, yotx.ru and
etc. (it’s time for your creativity:))
P.S I hope that your graph more or less will have the same behaviour.

e) (5 points)
Now, it is necessary to derive a hypothesis (mathematical model). And then
try to predict for 𝑛 = 25600. Now you have predicted value.
f) (2 points)
This step is related to V&V (verification and validation). It is necessary to run
your code for 𝑛 = 25600. By running your code and measuring time you can find
real value.

P.S If you have weak computer characteristics, please be patient because running process can take
some time (even up to 2 mins for example).

g) (3 points)
Accept or reject your hypothesis. As a main indicator you can take relative
error. If relative error does not exceed 10% then accept, otherwise reject. Hint:

|𝑝𝑟𝑒𝑑𝑖𝑐𝑡𝑒𝑑−𝑟𝑒𝑎𝑙|
𝑅𝑒𝑙𝑎𝑡𝑖𝑣𝑒 𝐸𝑟𝑟𝑜𝑟 = ∗ 100%
𝑟𝑒𝑎𝑙

Common Rules for Everyone:


1) Format of Submission:
In those tasks where writing code is required, you must adhere to the
following format of submission:
For example: WarmUpExcercise
Write a code to sum up two integer numbers in Java.
This task is just warm up exercise (it is not necessary to do it). Here I would
like to show the format of your submssion. You need to:
- take a screenshoot of your code. // P.S remember that it should be visible
- take a screenshoot of your inputs and outputs. //P.S remember that it should be
visible
- copy paste your coding part.
As you can see, we have 3 main conditions. If at least one of the 3 conditions is not
fulfilled by a student, then this task will be considered as not completed (0) at all.

public class Main {


public static void main(String[] args) {
int a,b,c;
a = 5; b = 7;
c = a+b;
System.out.println("The sum of a and b = "+ c);
}
}
2) General Concept of Grading:

Evaluation Criteria Range of grades


All codes are runnable. All inputs and outpus from examples 90-100
are verified. There exists good explanation of some
algorithms where it requires. All inserted pictures and images
are visible and readable.
Not all inputs can give the expected output. Not good 50-89
explanation (by hand derivation) of mathematical side. Codes
are giving some mistakes after debugging, but general concept
(or idea) is correct. Some parts of tasks are missing. Syntax
mistakes in Java, like forgetting “;” and etc.
General concept of coding is not (almost) correct. More than 0-49
half of inputs give non-expected outputs. Almost no “by-
hand” derivation of mathematical side. Most of the tasks are
not fulfilled.

3) Late submission penalties

Time Ranges Substracted Points from Final


grade
[0-4) hours -15
[4-8) hours -30
[8-12) hours -45
[12-24) hours -60
≥ 24 hours -100

In case of extraordinary events penalties will be cancelled. But those events


will be considered individually case-by-case.
4) Points for Creativity:
If you will sum up all points from the above tasks, then you will notice that
total sum is equal to 90. The rest 10 points can be given to the following parts of the
assignment but not limited:
- Task 4 part a)
- Task 4 part b)
- Task 5 part c)
- Task 5 part d)
- You can write in general conclusion, what types of tasks did you solve, what
type of useful information did you derive for yourself and etc. No more than
10 sentences
5) Submit only pdf or docx format file. Web links to Google Docs will not be
accepted. Due to the fact that it is impossible to check the date of submission.

That’s all about 1st Assignment. I hope that you really enjoyed during solving these interesting
problems and derived some useful information for yourself. Thank you for your accurate reading
and for your attention until the end. If you have any questions, please do not hesitate to contact me
via MS Teams.

Kind Regards,
Ruslan Omirgaliyev
Senior-Lecturer of Department of Computer Engineering
Astana IT University

You might also like