Saturday, October 18, 2008

recursion at a glance

Recursion is something like you have established a foundation and other situations are built from it. For example, in Fibonacci numbers, F(n) = n for natural numbers n < 2, which serves as a foundation and for other natural numbers, F(n) = F(n-1) + F(n-2).
Recursion can be used to break large and complex problems step by step to the smallest ones. As in binary search in a sorted array, the problem is broken down by half each time until you only have two elements to compare at last. And in mergesort, the idea is to divide the array into two parts from the middle, and then again divide each part from the middle. You keep dividing parts into two until you get an array of size one, which you don't need to sort further, but instead, you just return the element. Then you combine the sorted left and right parts into one sorted part and at last, you end up having the whole array sorted.

No comments: