CS302 Design and Analysis of Algorithms: Muhammad Sohail Afzal
CS302 Design and Analysis of Algorithms: Muhammad Sohail Afzal
Computing time is therefore a bounded resource, and so is space in memory. You should use
these resources wisely, and algorithms that are efficient in terms of time or space will help you
do so.
Algorithms as a Technology
Different algorithms devised to solve the same problem often differ dramatically in
their efficiency. These differences can be much more significant than differences
due to hardware and software.
Example two different algorithms for sorting : Merge Sort and Insertion sort. Which is better in
terms of time ! Lets discuss..
Algorithms as a Technology
Suppose Insertion Sort takes time = (c1 x n x n) = ( c1 x n^2 )
Suppose Merge Sort takes time = ( c2 x n x log n )
Although insertion sort usually runs faster than merge sort for small input sizes, once the input
size “n” becomes large enough, merge sort’s advantage of “log n” vs. “ n” will more than
compensate for the difference in constant factors. No matter how much smaller c1 is than c2,
there will always be a crossover point beyond which merge sort is faster.
Algorithms as a Technology
Algorithm execution on Faster vs. Slower machine
Let us put a faster computer (computer A) running insertion sort against a slower computer
(computer B) running merge sort. They each must sort an array of 10 million numbers.
(Although 10 million numbers might seem like a lot, if the numbers are eight-byte integers, then
the input occupies about 80 megabytes, which fits in the memory of even an inexpensive laptop
computer many times over)
Algorithms as a Technology
Suppose that computer A executes 10 billion instructions per second (faster than any single
sequential computer at the time of this writing) and computer B executes only 10 million
instructions per second, so that computer A is 1000 times faster than computer B in raw
computing power
To make the difference even more dramatic, suppose that the world’s craftiest programmer
codes insertion sort in machine language for computer A, and the resulting code requires
“2xn^2” instructions to sort n numbers. Suppose further that just an average programmer
implements merge sort, using a high-level language with an inefficient compiler, with the
resulting code taking “50 x n x log n” instructions
Algorithms as a Technology
To sort 10 million numbers, computer A (faster computer) takes :
The advantage of merge sort is even more pronounced when we sort 100 million numbers: where
insertion sort takes more than 23 days, merge sort takes under 4 hours. In general, as the problem
size increases, so does the relative advantage of merge sort.
This is how slower machine can perform better than faster machine when algorithm running on
slower machine is better than the algorithm running on faster machine for same problem.
TASK: To be completed by next monday
Comparison of running times
For each function f (n) and time t in the following table, determine the largest size n of a
problem that can be solved in time t, assuming that the algorithm to solve the problem takes
f(n) microseconds.