A program is a list of tasks to be performed by definition. But it can also be defined as a way of mapping a large task to shorter tasks. A computer always performs a program as a list of tasks. I take this question as a theoretical one. In my opinion the difference is the problem solver’s way of thinking. Let’s see a real world example. Assume, we have to sort a set of values. In case we try to solve it as a list of tasks, we probably come to a result, similar to the bubble sort algorithm. Paul E. Black (2009) gives a definition of bubble sort algorithm: “Sort by comparing each adjacent pair of items in a list in turn, swapping the items if necessary, and repeating the pass through the list until no swaps are done.”. This algorithm is an iterative way of solving the given problem. What happens when we try to map the large task to shorter tasks? Although there are a lot of sources where I can find this algorithm, I chose the same source because I think this dictionary is very useful. Conrado Martinez (2009) gave a definition of Quicksort algorithm: “ Pick an element from the array (the pivot), partition the remaining elements into those greater than and less than this pivot, and recursively sort the partitions.”. Divide et impera. Nice and elegant solution of the same problem. We can see the difference in the way of thinking. Both of them can give us a solution and both ways have it’s pros and cons. Recursion’s main drawback is the intensive memory usage. It is especially important in Java, my favourite programing language. Although Shawn Bayern (2001) writes about a very nice method of reducing the size of needed memory while keeping the code nice. I also have to mention another practical aspect. Although recursive algorithms are usually more elegant and many times it’s more easy to solve problem using them, they are usually slower as shiman (2008) wrote in his blog with some nice examples. He also summarizes the main cause of using the recursion: “Q: Then why use recursion?? A: It makes the code beautiful – recursion is a beauty of programming. Sometimes it is much simpler to write the recursive version.” Reference list: Conrado Martinez (2009) quicksort in Dictionary of Algorithms and Data Structures [Online]. Available from: http://www.itl.nist.gov/div897/sqg/dads/HTML/quicksort.html (Accessed: 15 October 2009) Paul E. Black (2009) bubble sort in Dictionary of Algorithms and Data Structures [Online]. Available from: http://www.itl.nist.gov/div897/sqg/dads/HTML/bubblesort.html (Accessed: 15 October 2009) Shawn Bayern (2001) Synchronized Recursion [Online]. Available from: http://www.ddj.com/architect/184404657 (Accessed: 15 October 2009) shiman (2008) Recursion VS Iteration (Looping) : Speed & Memory Comparison [Online]. Available from: http://shiman.wordpress.com/2008/05/28/recursion-vs-iteration/ (Accessed: 15 October 2009)

# Different ways of thinking

Published by Laszlo Varga

comments powered by Disqus