Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Collection and efficiency_exercises.doc
Binary file not shown.
Binary file added ComparingAlgorithms-ComplexityTheory.pdf
Binary file not shown.
9 changes: 9 additions & 0 deletions OtherResources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
###Optional Resources:


* [Java Just-In-Time (JIT) Compiler]( https://singztechmusings.wordpress.com/2011/07/03/java-what-is-jit-compiler-and-how-does-it-work/)
* [Calculator for log](http://www.calculator.net/log-calculator.html)
* [Difference between Comparator and Comparable interface ](https://www.javacodegeeks.com/2013/03/difference-between-comparator-and-comparable-in-java.html)
* [Sorting in Collection](http://tutorials.jenkov.com/java-collections/sorting.html)
* [Documentary on BBC Radio 4 called Controlling the Uncontrollable Algorithm](http://www.bbc.co.uk/programmes/b085wj18)
* [The Secret Rules of Modern Living Algorithms | Real Computer Science Documentary](https://www.youtube.com/watch?v=T1os88EvPc4)
Binary file added SestoftMicrobenchmarking.pdf
Binary file not shown.
Binary file added SestoftSearchAndSort.pdf
Binary file not shown.
127 changes: 127 additions & 0 deletions day1exercises.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Exercises for day 1

##Exercise 1 (measure execution time)

Open the project “Collections and Efficiency – demo” and open the project “time_measure_demo”.

* Run the program “MeasureSimpleCode”. Compare your observations with your neighbours.
* What can be concluded?

* Run with different values of ‘n’. Observe how the execution time varies with “n”.
* Conclusion?

Note: In this project we have disabled the "Just-in-time compilation”. In NetBeans this is done by:

Right click the project > properties > Run
set ”VM Options” to ”-Djava.compiler=NONE” > OK

##Exercise 2 (another “algorithm”)

Now, add the statement below to the loop in exercise 1 and figure out how execution time varies with “n” after the change.
```java
n = n/2;
```
Conclusion?

##Exercise 3 (Time complexity – ‘Big O’ – notation)

Determine the time complexity for the following code fragments.
Use the ’Big O’-notation in your answers. ‘n’ is assumed to be declared and initialized.

```java
for (int i=0;i<10000;i++)
Math.sqrt(i);
```
```java
for (int i=0;i<n;i++)
Math.sqrt(i);
```

```java
while(1<n)
{
n=n/2;
Math.sqrt(n);
}
```

```java
for (int i=0; i<n; i++)
{
for (int k=i; k<n; k++)
{
Math.sqrt(i);
}
}
```
##Exercise 4 (Time complexity)

What is the time growth rate (time complexity) of the following method?

```java
public static int count(int[] a, int c)
{
int count = 0;
for (int i = 0; i < a.length; i++)
{
if (a[i] == c) count++;
}
return count;
}
```

##Exercise 5 (Time complexity)

Suppose an algorithm A takes 5 seconds to handle a dataset of 1000 elements. Fill in the approximate execution time for A depending on the complexity of the algorithm.
![Alt text](../img/day1ex5table.png)

##Exercise 6 (Time complexity)

For the following expressions, what is the order of growth (time complexity) of each?

1. n<sup>2</sup>+ 2n + 1
2. n<sup>10</sup> + 9n<sup>9</sup> + 20n<sup>8</sup> + 145n<sup>7</sup>
3. n + (0.001)n<sup>3</sup>
4. n + log(n)

##Exercise 7 (classic search algorithms)

1. Run the program “MeasureAlgorithm” several times with varying values for both ‘n’ and ‘target’.

What can you conclude about the expected execution time and its dependence on ‘n’ and ‘target’ ?

2. Use “BinarySearch” and repeat.

3. Create a class called SortingAlgorithms. This class should have an array of integer as a datafield and array size. The constructor should be used to create an array of given size. SortingAlgorithms class should have three methods. One method for filling the array with random integers which you can call from the constructor.
One method for implementing inserstion sort, One method for selection sort. In the main method you create three objects with array size of 100,1000, and 10000. Identify the elapsed times when selection and insertion methods are called. What are the time complexities? Does your observation match with actual elapsed time?

##Exercise 8 (LinkedList and ArrayList)

###a)
Draw a memory referencing diagram of adding three nodes in a LinkedList of nodes 26, 13, 180, 30.



###b)
Make a “benchmark”-test between ArrayList and LinkedList.
Create an ArrayList and a LinkedList and fill both with ‘n’ Integers. Then measure the execution time on both lists for several calls to
```java
add(index, 1)
```
where ‘index’ refers to the first, the middle and the last element respectively

Draw relevant conclusions from your observations: Describe a situation where you would prefer a LinkedList rather than an ArrayList?

Hint: Remember to disable the "Just-in-time compilation” – see exercise 1.


###c)
Measure actual execution times for sorting a large number of elements in ArrayLists and LinkedLists using the method Collecions.sort().

Hint: Take care to include only the sorting code in the actual measurement.

Hint2: Read the documentation for the implementation of the sort() method. In some versions of Java, the list is copied to an array before sorting. What does this imply for the execution times?




31 changes: 31 additions & 0 deletions day2exercises.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#Day2 Exercises - binary trees and recursion




##Exercise B -Recursion

2. Solve the exercises factorial, bunnyEars, bunnyEars2, triangle from <http://codingbat.com/java/Recursion-1>


##Exercise C -Binary trees
3. Go to <https://www.cs.usfca.edu/~galles/visualization/BST.html> and play a bit with the animation.
Insert a few elements. Find an element, Delete an element.
4. Sketch a binary search tree (on paper) after the insertion of the numbers [ 5,3,2,6,8,9,10 ] in the order shown.
Assume that the tree was empty initially.
5. Sketch a (new) binary search tree after insertion of the numbers [ -1,10,-50,-20,50,4,5,6 ].
6. Sketch a (new) binary search tree after insertion of the numbers [ 1,2,3,4,5,6 ]
7. Order the insertion of the numbers [1,2,3,4,5,6,7 ] into a tree, so that the
resulting tree will have the minimal height possible.
8. Go to <https://www.khanacademy.org/computer-programming/depth-first-traversals-of-binary-trees/934024358>. Try to figure out what is the difference between traversing the tree in Preorder, Inorder and Postorder.
9. State the order in which the nodes will be visited if the tree from exercise 4 is traversed following the principle inorder, preorder and postorder principle respectively.
10. Which of the three traversals writes out the elements in sorted order?


11. Get the code from the project BinarySearchTreeDemo to run in Netbeans.

* Create the tree below using a sequence of to the insert menthod:

![Image missing](../img/day2tree.png)

11. Check that the lookup method works. Test with both an element which exist in the tree, and one which do not.
65 changes: 65 additions & 0 deletions day3exercises.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#Day 3 exercises
##Exercise 1 Quicksort

Execute the animation (http://me.dt.in.th/page/Quicksort/)
several times until you understand the principle behind the algorithm.

Notice that there are some animations further down the page

##Exercise 2 Quicksort implementation

###a)
Implement the quicksort algorithm in Java. Use the pseudo code on WIKIPEDIA as inspiration (http://en.wikipedia.org/wiki/Quicksort).

* Choose the element in the middle as the pivot element.
* Test with an array of arbitrary integers.


###b)
Measure actual execution times for the sorting implementation.

* long time = System.nanoTime() returns a value representing the current time
* Perform measurements on several arrays while varying their size.
* Compare with SelectionSort. [Source code in github](SelectionSort.java).

###c)
Produce a worst case for Quicksort by sorting an (already) sorted array and choosing the first element in the array as the pivot element.
How big an impact does it have on the execution time?

###d)
Let the algorithm choose a ”sensible” pivot element (what is a sensible pivot element?)

Measure execution times for the sort() method in the Arrays class and compare Quicksort for

* unsorted arrays
* sorted arrays

###e)
What algorithm is actually used in the sort() method in the Arrays class?

* Look up the answer in the documentation for API.

The sort() method is heavily overloaded. Make a small check for whether the library uses the same algorithm in

* sort(int[])
* sort(Object[])

##Exercise 3

See the Merge Sort - animation on <http://visualgo.net/sorting> several times until you understand the principle behind the algorithm. There are also some animations on <https://en.wikipedia.org/wiki/Merge_sort>.



##Exercise 4.

###a)
Implement the MergeSort algorithm in Java from the pseudo code on wikipedia (<http://en.wikipedia.org/wiki/Merge_sort>)

Personally I like the list based solution (the last one listed in wikipedia) better than the one named top-down and bottom-up.







48 changes: 48 additions & 0 deletions day4exercises.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#Day 4 Exercises

##Exercise 1 - Hashmap

1. Include a HashMap in the “benchmark”-test from exercise 8a from Day1) using the method put(key, value).

Are the execution times obtained in agreement with the growth rate _O_(1)?

2. Write a program which inserts the string values "zero", "one", "two", "three", "four", "five"... up to "nine" under keys 0, 1, 2, 3, 4, 5,...,9.

3. The retrive the strings stored under keys 7, 9, 3 and print out their values.

4. Change the order in which the key-value pairs were inserted in 1). Does that change the output in 2.?

5. Add an other HashMap, which maps the keys "zero", "one", "two", "three", "four", "five"... up to "nine" to the values 0, 1, 2, 3, 4, 5,...,9.

6. Retrieve the values stored under "one", "five", "seven".

##Exercise 2 (Collections class - efficiency)

Compare execution times for the method Collecions.sort() for two large collections

1. ArrayList
2. LinkedList

Do you expect one of the collections to be sorted fastest?
Do your observations agree with your expectations?

Read the documentation for the implementation of the sort() method. In some versions of Java, the list is copied to an array before sorting. What does this imply for the execution times?


##Exercise 3 (Utilizing Java Collection Framework)

Create an algorithm for eliminating duplicates from a list of elements (i.e. integers)
Consider which kind of collection can be applied to simplify the task.


##Exercise 4 (Tree Sort)

Implement the tree sort algorithm by utilizing the Binary Tree class from a previous exercise.

You need to make some extensions for traversing the Binary Tree class.


##Exercise 5 (Catch up on exercises from the previous days on Collections)



55 changes: 55 additions & 0 deletions day4red.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#Red team exercises
Day 4 is intended as a recapitulation day where we will once again look at the big-O notation and some recursion.
Some of you have grasped this completely already. These exercises are intended to be more challenging.

##Recursion & complexity
###Fibonacci implementation
Consider these two implementations of the fibonacci numbers:

```java
long fibonacci(long n) {
if ( n == 0 ) return 0;
if ( n == 1 ) return 1;
return fibonacci(n-1) + fibonacci(n-2);
}
```

and

```java
public int fibonacci(int n) {
if ( n == 0 ) return 0;
if ( n == 1 ) return 1;
int fib = 1; int prevFib = 1;
for (int i = 2; i < n; i++){
fib = fib + prevFib;
prevFib = fib-prevFib;
}
return fib;
}
```

what is the growth rate for each of these implementations as a function of n?

###A few codingbats
Solve and find the complexity for three of the exercises in [Recursion-2 of Codingbat](http://codingbat.com/java/Recursion-2).

###Classic chess-queens problem
The Queen problem is: "In how many ways can one place eight queens on a chess board so none can capture each other in the next move". The question can be generalized to a board of size N.

Write a method which takes board size N as input, and returns the number of solutions for that size board.

How many solutions exist for each board sizes up to 8?

What is the growth rate of the queens problem?

###Heap sort
Heap sort is an interesting sorting algorithm, as it is always _O_(n·log<sub>2</sub>(n)), and it sorts without using only the array itself, that is it does not need to copy elements back and forth like merge sort.
###a)
Figure out what the two phases of a heap sort are? It is necessary to understand how a heap work before trying to implement it. Find an animation which explains it. Write the algorithm in pseudo code.

###b)
Implement the heapsort algorithm. As always, use a helper method for swapping.

###c)
Why is this algorithm _O_(n·log<sub>2</sub>(n))?
Loading