In this chapter we will: introduce recursion as a programming technique, show how recursion can be used to simplify the design of complex algorithms, present several well known recursive algorithms (e.g. quicksort, merge sort, Guaussian elmination), introduce the linked list data structure and recursive implementations for several of its methods, present programs for drawing two types of fractal curves.
Trang 1Chapter 14
Recursion
Lecture Slides to Accompany
An Introduction to Computer Science Using Java (2nd Edition)
by
Trang 2Chapter Preview
In this chapter we will:
• introduce recursion as a programming technique
• show how recursion can be used to simplify the design
of complex algorithms
• present several well known recursive algorithms
(e.g quicksort, merge sort, Guaussian elmination)
• introduce the linked list data structure and recursive implementations for several of its methods
• present programs for drawing two types of fractal
Trang 3• A recursive algorithm is a problem solution
that has been expressed in terms of two or
more easier to solve subproblems
Trang 5Counting Digits in Java
Trang 6• Use f(y) to compute f(x)
• For this to work, there has to be at least one value of x for which f(x) can be computed
directly (e.g these are called base cases)
Trang 8Divide and Conquer
• Using this method each recursive subproblem
is about one-half the size of the original
problem
• If we could define power so that each
subproblem was based on computing kn/2
instead of kn – 1 we could use the divide and
conquer principle
• Recursive divide and conquer algorithms are
Trang 9Evaluating Exponents Using
Divide and Conquer
static int power(int k, int n) {
// raise k to the power n
Trang 10Selection Sort
private static void selectionSort
(double[] A, int lo, int hi) { // A[0] A[lo-1] contain smallest
// values in A, in ascending order
if (lo < hi) {
swap(A, lo, findMiniumum(A, lo, hi); selectionSort(A, lo + 1, hi);
}
Trang 11Find Minimum
private static int findMinimum
(double[] A, int lo, int hi) {
Trang 12Selection Sort Helper Function
• Client programs should not need to know
anything about how selectionSort was implemented
• A single argument helper function can be
used to help clients make use of the new
version
• Example
public static void selectionSort(double[] A) { selectionSort(A, 0, A.length – 1);
Trang 13Insertion Sort
private static void insertionSort
(double[] A, int hi) { // Sort A[0] A[hi]
Trang 14Insert in Order
private static void insertInOrder
(double[] A, int hi, double x) { // Insert x into A[0] A[hi-1],
// filling in A[hi} in the process
// A[0] A[hi – 1] are sorted
Trang 15Insertion Sort Helper Function
• Again we should provide potential clients with
a single argument helper functon
public static void InsertionSort(double[] A) { InsertionSort(A, A.length – 1);
}
Trang 16Quicksort Overview
• Choose the middle element among A[lo] A[hi]
• Move other elements so that
– A[lo] A[m – 1] are less than A[m]
– A[m + 1] A[hi] are greater than A[m]
• Return m to the caller
Trang 17static void quickSort
(double[] A, int lo, int hi) {
int m;
if (hi > lo + 1) { // 3 or more subarray values
m = partition(A, lo, hi);
quickSort(A, lo, m – 1);
quickSort(A, m + 1, hi);
}
else // less than 3 subarray values
if ((hi == lo + 1) && (A[lo] > A[hi])
swap(A, lo, hi);
Trang 18static int partition (double[] A, int lo, int hi) { // choose middle element from A[lo} A[hi]
// move elements so A[lo] A[m-1] are all < A[m]
// move elements so A[m+1] A[hi] are all > A[m]
swap(A, lo, medianLocationA, lo+1, hi,
Trang 19Partition Helper
static int partition
(double[] A, int lo, int hi, double pivot) {
Trang 20Median Location
static int medianLocation
(double[] A, int i, int j, int k) {
Trang 22Solving Linear Systems
solve (System E of n equations in n unkonwns) {
Trang 23Integer List Class
class IntList {
private int value;
private IntList tail;
public IntList(int v, IntList next) { value = v;
Trang 24Constructing a Linked List
• One approach would be:
IntList list = new IntList(-14, null);list = new IntList(616, list);
list = new IntList(10945, list);
list = new IntList(17, list);
• Alternatively we could have written:
IntList list =
new IntList(17,
new IntList(616,
new IntList(10945,
Trang 25Constructing a Linked List
from Input
Intlist readReverseList () {
int inputval;
IntList front = null;
Inputbox in = new IntputBox();
Trang 26Printing Linked List
void print (IntList list) {
Output out new OutputBox();
while (list != null) {
out.print(list.getValue() + “ “); list = list.getTail();
}
out.println();
}
Trang 27Computing List Length
• This method would be added to IntList
public int length() {
Trang 28Converting List to String
• This method would be added to IntList
public String toString() {
String myValue = Integer.toString(value);
Trang 29Retrieving nth List Element
• This method would be added to IntList
public IntList nth(int n) {
Trang 30Adding Element to End of List
• This method would be added to IntList
public void addToEndM(int n) {
Trang 32Adding Element to List in Order
• This method would be added to IntList
public IntList addInorderM(int n) {
Trang 34public static IntList mergeSort (IntList L) {
// Sort L by recursively splitting and merging
if ((L == null) || (L.getTail() == null);
// zero or one item return L;
else { // two or more items // Split the list into two parts
IntListPair p = L.split();
// Sort and merge the two parts
return mergeSort(p.x).merge(mergeSort(p.y)); }
Trang 36Splitting the List
• This method would be added to IntListPair
public IntList split() {
}
Trang 37Merging the Lista
• This method would be added to IntList