Reason to study algorithms ??
Suppose computers were infinitely fast and computer memory was free. Would you have any reason to study algorithms?
Before deriving the solution I would like to reflect on a great example from the very famous book "Introduction to Algorithms". Though the computing potential of newer generation computers is increasing exponentially that does not decrease the importance of a good design and efficient algorithm.
You might be familiar with two very famous sorting algorithms Insertion sort & merge sort. The first, known as insertion sort, takes time roughly equal to (n2)to sort n items, The second, merge sort, takes time roughly equal to (n lg n), where lg n stands for log2 n.
Now suppose we have two computers, computer A running insertion sort and computer B running merge sort and these computers need to sort an array of 10 million elements(numbers) which is roughly around 80 MB. Now to show the difference what a designed algorithm can make Assume computer A executes 10 billion instructions per second which is like having a computer with 10GHz clock speed and computer B executes only 10 million instructions per second which is around 10MHz, 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 2n2 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 50n log n instructions. To sort 10 million numbers,
Computer A takes
领英推荐
Computer B takes
By using an algorithm whose running time grows more slowly, even with a poor compiler, computer B runs more than 17 times faster than computer A! 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 four hours. In general, as the problem size increases, so does the relative advantage of merge sort.
This example vividly illustrates the crucial importance of studying algorithms, even in an era where computing power and memory are seemingly limitless. The raw speed of a computer can be overshadowed by the efficiency of the algorithm it runs. As shown, a less powerful computer running a well-designed algorithm can significantly outperform a more powerful machine using a suboptimal approach. Therefore a problem should be approached by different solutions with increasing levels of optimization.
Thank you for reading, let me know if you find this insightful :)