课程: Java Algorithms
How to optimize an algorithm
- [Instructor] There are many different ways to complete a given task. This means there are also many different algorithms to solve a problem. Some algorithms are more efficient and others are less efficient. With the sandwich algorithm, one way to make a sandwich is to go to the store for each ingredient. You make multiple trips to the store and you start making the sandwich after gathering all the ingredients. Before we assumed we had all the proper ingredients and required tools in the kitchen. By making this assumption, we were able to save time by not running to the store. The same is true for algorithms. To make an algorithm more efficient, you have to make an assumption about your data. You might assume that your data is sorted or that it will only contain integers or strings. Maybe it will only contain positive integers or strings with only alphabetical characters. With these assumptions, we can skip steps, such as normalizing the string, or for the sandwich analogy, running to the grocery store. However, it's very important to understand what assumptions your algorithm is making. If you make a wrong assumption about your data, the algorithm will likely not perform the task as expected. A slow working algorithm is always preferred to a fast algorithm that isn't trustworthy and can spit out a wrong response. That's why it's essential to make informed assumptions about your data, and you can leverage these assumptions to optimize your algorithms.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。