1 Introduction
1 Introduction
This chapter lays the groundwork for the material covered in the course and sets out the expectations for the
readers.
The intended readership of this course includes folks who are graduates of
boot-camps, career-switchers into programming jobs or just established
industry veterans who want a quick revision on the topic of complexity
theory. The course uses layman's terms to describe the concepts commonly
used in the industry while intentionally avoiding the intricate mathematical
gymnastics often involved in analyzing algorithms so that the subject material
is comprehensible for readers at all skill levels. At the same time, this course
may not deliver great value to individuals who already have a firm grasp of
algorithms and data-structures or to those that hold a degree in computer
science.
Usually, the 101 example used to teach complexity is sorting of integers. Below
are three example programs that sort integers using different sorting
algorithms.
Bubble Sort
import java.util.Random;
class Demonstration {
Merge Sort
import java.util.Random;
class Demonstration {
if (start == end) {
return;
}
k = start;
while (k <= end) {
if (input[k] == scratch[i]) {
i++;
} else {
j++;
}
} else if (i <= mid && j > end) {
input[k] = scratch[i];
i++;
} else {
input[k] = scratch[j];
j++;
}
k++;
}
}
Merge Sort
import java.util.Random;
import java.util.Arrays;
class Demonstration {
The code implementations above run on datasets of 5000 elements. Below are
some crude tests (run on a Macbook) for the three algorithms. All times are in
milliseconds
Java's
Size Bubble Sort Merge Sort
util.Arrays.sort
5000 90 9 4
10000 299 15 17
100000 24719 26 33
The results tabulated above should provide plenty of motivation for software
developers to increase their understanding of algorithmic performance.
Bubble sort progressively degrades in performance as the input size increases,
whereas the other two algorithms perform roughly the same. We'll have more
to say about their performances in the next chapter.
The astute reader would notice the use of an additional array in the merge
sort code, named scratch array. This is the classic trade-off in the computer
science realm. Throwing more space at a problem allows us to bring down the
execution time, and vice versa. Generally, time and space have an inversely
proportional relationship with each other; if space increases then execution
time decreases, and if execution time increases then space decreases.