1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture An introduction to computer science using java (2nd Edition): Chapter 14 - S.N. Kamin, D. Mickunas, E. Reingold

37 50 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 37
Dung lượng 206,26 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Chapter 14

Recursion

Lecture Slides to Accompany

An Introduction to Computer Science Using Java (2nd Edition)

by

Trang 2

Chapter 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 5

Counting 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 8

Divide 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 9

Evaluating Exponents Using

Divide and Conquer

static int power(int k, int n) {

// raise k to the power n

Trang 10

Selection 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 11

Find Minimum

private static int findMinimum

(double[] A, int lo, int hi) {

Trang 12

Selection 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 13

Insertion Sort

private static void insertionSort

(double[] A, int hi) { // Sort A[0] A[hi]

Trang 14

Insert 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 15

Insertion 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 16

Quicksort 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 17

static 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 18

static 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 19

Partition Helper

static int partition

(double[] A, int lo, int hi, double pivot) {

Trang 20

Median Location

static int medianLocation

(double[] A, int i, int j, int k) {

Trang 22

Solving Linear Systems

solve (System E of n equations in n unkonwns) {

Trang 23

Integer List Class

class IntList {

private int value;

private IntList tail;

public IntList(int v, IntList next) { value = v;

Trang 24

Constructing 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 25

Constructing a Linked List

from Input

Intlist readReverseList () {

int inputval;

IntList front = null;

Inputbox in = new IntputBox();

Trang 26

Printing Linked List

void print (IntList list) {

Output out new OutputBox();

while (list != null) {

out.print(list.getValue() + “ “); list = list.getTail();

}

out.println();

}

Trang 27

Computing List Length

• This method would be added to IntList

public int length() {

Trang 28

Converting List to String

• This method would be added to IntList

public String toString() {

String myValue = Integer.toString(value);

Trang 29

Retrieving nth List Element

• This method would be added to IntList

public IntList nth(int n) {

Trang 30

Adding Element to End of List

• This method would be added to IntList

public void addToEndM(int n) {

Trang 32

Adding Element to List in Order

• This method would be added to IntList

public IntList addInorderM(int n) {

Trang 34

public 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 36

Splitting the List

• This method would be added to IntListPair

public IntList split() {

}

Trang 37

Merging the Lista

• This method would be added to IntList

Ngày đăng: 11/01/2020, 18:46

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN