Algorithm is one the fundamental components that a programmer should master. But every time I read a book about it, I see intrusive instructions about how to solve a problem using several algorithms. Well they just tell you that you should use this and that. Sure, they are brilliant ideas, but how do those ideas come up? If we do not know this process, it is hard to say we have really learned.

I’ve been reading the book “How to solve it” by George Pólya. It is a great book that shows you how to approach a problem using various steps. You can ask about certain questions and answer them to get a better chance to obtain the final solution. Although this book is about mathematics, I think it also applies for algorithms or even just problems in general. So I’d like to write the conversation I made myself to show the process of how we get an algorithm without giving intrusive instructions. These are just my humble ideas and suggestions are always welcome.

This problem is about sorting: Suppose you have 100 numbers. They are 100 different cards and out of order. How can you sort them in ascending order?

**Q: What is the unknown? What is your goal?**

**Q: What are the data?**

**Q: Have you seen it before?**

**Q: OK Now tell me, how would you sort those cards?**

**Q: OK so you are doing a procedure repeatedly. What kind of component in programming is related to this?**

**Q: OK, so which one matches your need here?**

**Q: So what should you do in the loop?**

**Q: What is the ending condition?**

**Q: OK, you are comparing new number with former ones. what pattern do they have?**

**Q: OK, look at the unknown. What do we want to do with the data?**

**Q: Now do the former numbers satisfy this condition when you exam them?**

**Q: So they are…?**

**Q: So every time you pick a new number, you are comparing it to data that are…?**

**Q: Good. Now you mentioned that you need to place the new number correctly. Can you explain how to do it?**

**Q: Well, what if the new number is larger than all numbers in the former sorted numbers?**

**Q: Good. Do the former numbers have any other patterns?**

**Q: Sure. So it is changing and you need to use it every time. What should you do about it?**

**Q: Good. Now you know you need a loop and what to do in the loop. Sounds like you have a plan. Be sure to check each step. Consider special cases. And when you are done, test it with some data to make sure you have the right algorithm.**

**Q: Great now you have a solution for this problem. Is it possible to do it differently?**

**Q: You mentioned recursion. Is it possible to solve this problem using recursion?**

**Q: How does recursion usually works? What kind of problem does recursion apply to?**

**Q: Good, so can we apply it on our problem? What is the smaller problem here?**

**Q: OK but your sub-problem must relate to the original problem. So these 50 numbers must be inside…**

**Q: Good. Now we only considered 50 numbers. What about the rest 50?**

**Q: You sorted 50 numbers. To get all 100 numbers sorted, what should you do about the rest?**

**Q: I’m glad you ask this question. Now the problem is whether we could get the sorted 100 numbers from two sorted subsets. Can you think about if this is possible?**

**Q: Can you think about an example of this situation?**

**Q: Good call. Now for recursion, there are two cases. What are they?**

**Q: What is the base case?**

**Q: What about the recursive case?**

**Q: Nice. Now you have a plan. Make sure you check each step and use some test cases on your algorithm.**