From the course: C# Algorithms

Optimize an algorithm in C# - C# Tutorial

From the course: C# Algorithms

Optimize an algorithm in C#

- [Instructor] While studying algorithmic theory is important, it's vital to apply the theory to practice. Let's modify a C# algorithm and make it more efficient. In this sample code, we have an algorithm called findMaximum. This finds the largest of three numbers. If there are duplicate entries, we still return whatever the maximum is. You can find it in the exercise files of this course. Now inside the algorithm, we do several comparisons in order to find that maximum. We can actually eliminate some of these comparisons and make our algorithm more efficient using assumptions. On the first line of the algorithm, we check if A is greater than B. Then later on, we check the opposite, is B greater than A. Instead of doing multiple checks with each input, what if we kept track of the maximum using a variable? Let's create a new function with our refactor. Static int, we'll call it findMaximum2 and it will take in those three numbers. The value of the max will start off at A. Then we'll compare B to the max. If B is greater than the max, then we'll set the max to B. In the previous implementation, we had to add checks for if A and B or A and C were equal. With this implementation, it doesn't matter because the value is already stored in the max variable. At this point in the execution, the max variable contains whatever the maximum of A and B is. To check whether C is greater, we just need to add one more comparison. If C is greater than the max, then we'll set the max to C. Now the maximum variable contains the maximum of A, B, and C, and we can return it from the function. Let's test our new algorithm's implementation. To run the code, we have a few different options. We can run it in the terminal with dotnet run. In the output, we get the maximum for each function call. Another option is to set up a run configuration with your IDE. I'm using VS Code and this is the configuration that will run your application as a console application with the output to the integrated terminal. Using a run configuration allows you to add break points to your code. Let's put a break point on line 25. To run it, we'll use the play button. The program's execution stops on line 25. We can see the values for each of our variables at this point in the execution. Then we can walk through the algorithm using the step over function. The max is reset to B and the max is set to C. Then we return it. In the terminal, we see three. We'll stop it for now. No matter how you run your code, you'll get the maximum for each set of values in the output. With the new implementation, we did only two comparisons per call. The code is also more readable with the defined variable and less comparisons. With this algorithm, we use the assumption that the max variable will contain the maximum for that point in the program's execution. When we check the max with C, we assume that the max variable holds the maximum of A and B. This allows us to do less comparisons and optimize the algorithm.

Contents