-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94dc7fe
commit a82ed73
Showing
9 changed files
with
277 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
title: "What is recursion?" | ||
--- | ||
|
||
# What is recursion? | ||
|
||
There are two classical approaches to solving algorthmic problems: | ||
|
||
1. Iterative | ||
2. Recursive | ||
|
||
## Iterative solution | ||
|
||
The distinctive property of *iterative* solutions is that they do not reduce a problem to a simpler form of itself. | ||
|
||
### EXAMPLE | ||
|
||
Add all integers between `low` and `high` (inclusive on both sides, and assuming `low` <= `high`), I can go through each integer and add it to an accumulating variable, say `total`. | ||
|
||
```java | ||
public static int sum(int low, int high) { | ||
if(low % 2 != 0) { | ||
low++; | ||
} | ||
int total = 0; | ||
for(int i=low; i<=high; i++) { | ||
total = total + i; | ||
} | ||
return total; | ||
} | ||
``` | ||
|
||
## Recursive solution | ||
|
||
The distinctive property of *recursive* solutions is that they reduce a problem to a simpler form of itself. | ||
|
||
### EXAMPLE | ||
|
||
For the same problem statement used for iterative solutions, we can say that the sum of all integers from `low` to `high` is: | ||
|
||
``` | ||
if low <= high: | ||
sub = sum of all integers from (low+1) to high | ||
return (low + sub) | ||
else | ||
return 0 | ||
``` | ||
|
||
Focus on the part | ||
|
||
>``` | ||
>sum of all integers from `low+1` to `high` | ||
>``` | ||
It is the same problem as the original problem, except there is one less number to handle, and thus is *simpler*. | ||
## Equivalence | ||
It has been proven that there is a recursive solution for every iterative solution and vice versa. We will soon look at some of the aspects to consider while deciding on which approach to take. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: "Motivation for searching and sorting algorithms" | ||
--- | ||
|
||
# Why should I use recursion? | ||
|
||
## Advantages | ||
|
||
1. Some solutions have an intuitive recursive design. Some examples (we assume n >= 0 for all examples): | ||
|
||
1. x<sup>n</sup> = x<sup>n-1</sup> * x (if n > 0, or 1 if n == 0) | ||
2. `nDigits(n)` = `nDigits(n/10) + 1` (if n > 0, or 0 if n == 0) | ||
3. `sum(n) = sum(n-1) + n` if(n > 0, or 0 if n == 0) | ||
|
||
2. | ||
>return `false` | ||
{: challenge} | ||
> ## Define a method that returns the highest number in an unsorted array. Return 0 if array is null or empty. | ||
>> ## SOLUTION | ||
>>> ```java | ||
>>> public static int highestUnsorted(int[] arr) { | ||
>>> if(arr == null || arr.length == 0) { | ||
>>> return 0; | ||
>>> } | ||
>>> int result = arr[0]; | ||
>>> for(int i=1; i < arr.length; i++) { | ||
>>> if(arr[i] > result) { | ||
>>> result = arr[i]; | ||
>>> } | ||
>>> } | ||
>>> return result; | ||
>>> } | ||
>>> ``` | ||
>{: .solution} | ||
{: .challenge} | ||
{: challenge} | ||
> ## Define a method that returns the highest number in an array sorted in ascending order. Return 0 if array is null or empty. | ||
>> ## SOLUTION | ||
>>> ```java | ||
>>> public static int highestSorted(int[] arr) { | ||
>>> if(arr == null || arr.length == 0) { | ||
>>> return 0; | ||
>>> } | ||
>>> return arr[arr.length-1]; //last item is the highest item | ||
>>> } | ||
>>> ``` | ||
>{: .solution} | ||
{: .challenge} | ||
### Cost of sorting needs to be recognized | ||
Of course, sorting involves a one-time fixed-cost of sorting the array, but the ongoing or variable cost of these operations reduces considerablly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.