• Tidak ada hasil yang ditemukan

PPT Slide 1

N/A
N/A
Protected

Academic year: 2023

Membagikan "PPT Slide 1"

Copied!
47
0
0

Teks penuh

(1)

Data Structures for Java Data Structures for Java

William H. Ford William H. Ford William R. Topp William R. Topp

Chapter 4 Chapter 4

Introduction to Algorithms

Introduction to Algorithms

(2)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Selection Sort Selection Sort

 Proceed through an array position by Proceed through an array position by position, starting with index 0. At the position, starting with index 0. At the

current position, select the element from current position, select the element from

the remaining elements that is next in the remaining elements that is next in

order and copy it into the current position.

order and copy it into the current position.

(3)

Selection Sort Example Step 1 Selection Sort Example Step 1

 The ordering begins by locating the The ordering begins by locating the smallest animal, the fish, in the first smallest animal, the fish, in the first

position. The operation occurs by having position. The operation occurs by having

the owl that currently occupies the first the owl that currently occupies the first position exchange places with the fish.

position exchange places with the fish.

The effect is to place the smallest animal

The effect is to place the smallest animal

(4)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Selection Sort Example Step 2 Selection Sort Example Step 2

 The tail of the list, starting at the second The tail of the list, starting at the second position is unordered. The process

position is unordered. The process continues by identifying the smallest continues by identifying the smallest

animal among the owl, dragon, and dog animal among the owl, dragon, and dog

and arranging it in the second position.

and arranging it in the second position.

The selection is the owl that is already in The selection is the owl that is already in

its proper position and so we can move to its proper position and so we can move to

the next step.

the next step.

O r d e r e d U n o r d e r e d

(5)

Selection Sort Example Step 3 Selection Sort Example Step 3

 With the fish and owl in position, only the With the fish and owl in position, only the last two animals must be ordered. We

last two animals must be ordered. We identify the dog as the next-smallest identify the dog as the next-smallest

animal and have it exchange positions animal and have it exchange positions

with the dragon. After this step, the tail of with the dragon. After this step, the tail of

the list has only one item left, which must the list has only one item left, which must

be the largest animal. The entire list is be the largest animal. The entire list is

O r d e r e d U n o r d e r e d

(6)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Selection Sort in Action

Selection Sort in Action

(7)

selectionSort() selectionSort()

public static void selectionSort(int[] arr) {

// index of smallest element in the sublist int smallIndex;

int pass, j, n = arr.length;

int temp;

// pass has the range 0 to n-2

for (pass = 0; pass < n-1; pass++) {

// scan the sublist starting at index pass smallIndex = pass;

(8)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

selectionSort() (concluded) selectionSort() (concluded)

// j traverses the sublist // arr[pass+1] to arr[n-1]

for (j = pass+1; j < n; j++)

// if smaller element found, assign // smallIndex to that position

if (arr[j] < arr[smallIndex]) smallIndex = j;

// swap the next smallest // element into arr[pass]

temp = arr[pass];

arr[pass] = arr[smallIndex];

arr[smallIndex] = temp;

} }

(9)

Selection Sort Example Selection Sort Example

// declare an integer array

int[] arr = {66, 20, 33, 55, 53, 57, 69, 11, 67, 70};

// call selectionSort() to order the array Arrays.selectionSort(arr);

System.out.print("Sorted: ");

for (int i=0; i < arr.length; i++) System.out.print(arr[i] + " ");

Output:

Sorted: 11 20 33 53 55 57 66 67 69 70

(10)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Sublists in an Array Sublists in an Array

 A sublist of an array is a sequence of A sublist of an array is a sequence of

elements whose range of indices begin at elements whose range of indices begin at

index

index first first and continue up to, but not and continue up to, but not including

including last last . Indicate a sublist using . Indicate a sublist using the notation

the notation [first, last) [first, last) . .

(11)

Sequential Search Sequential Search

 Begin with a target value and an index Begin with a target value and an index range

range [first, last) [first, last) . Scan the sublist . Scan the sublist item by item, looking for the first

item by item, looking for the first

occurrence of a match with an item occurrence of a match with an item

named

named target target . Return the index of the . Return the index of the match or -1 if target is not in the sublist.

match or -1 if target is not in the sublist.

(12)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The seqSearch() Method

The seqSearch() Method

(13)

seqSearch() Implementation seqSearch() Implementation

public static int seqSearch(int[] arr,

int first, int last, int target) {

// scan indices in the range first // <= i < last; return the index

// indicating the position if a match occurs;

// otherwise return -1

for (int i = first; i < last; i++) if (arr[i] == target)

return i;

// no return yet if match is // not found; return -1

return -1;

}

(14)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Binary Search Binary Search

The binary search exploits the fact that a list is The binary search exploits the fact that a list is

ordered. This allows large sections of the list to be ordered. This allows large sections of the list to be

removed from the search.

removed from the search.

Select the midpoint of the current sublist Select the midpoint of the current sublist [first,last)

[first,last) . .

If target matches midpoint, the search is successful. If target matches midpoint, the search is successful.

If the target is less than the current midpoint value, look If the target is less than the current midpoint value, look in the lower sublist by selecting its midpoint; otherwise, in the lower sublist by selecting its midpoint; otherwise,

look in the upper sublist by selecting its midpoint.

look in the upper sublist by selecting its midpoint.

Continue until locating the target or the sublist reduces Continue until locating the target or the sublist reduces to size 0.

to size 0.

(15)

Binary Search Case 1 Binary Search Case 1

 A match occurs. The search is complete, A match occurs. The search is complete,

and and mid mid is the index that locates is the index that locates target target . .

(16)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Binary Search Case 2 Binary Search Case 2

 The value The value target target is less than is less than midValue midValue , , and the search must continue in the lower and the search must continue in the lower

sublist. The index range for this sublist is sublist. The index range for this sublist is

[first, mid)

[first, mid) . Reposition the index last . Reposition the index last to the end of the sublist (

to the end of the sublist ( last = mid last = mid ). ).

(17)

Binary Search Case 3 Binary Search Case 3

 The value The value target target is greater than is greater than midValue

midValue , and the search must continue , and the search must continue in the upper sublist . The index range for in the upper sublist . The index range for

this sublist is

this sublist is [mid+1,last) [mid+1,last) , because the , because the sublist begins immediately to the right of sublist begins immediately to the right of

mid . Reposition the index first to the front mid . Reposition the index first to the front

of the sublist

of the sublist (first = mid+1) (first = mid+1) . .

(18)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Binary Search Termination Binary Search Termination

 The binary search terminates when a The binary search terminates when a

match is found or when the sublist to be match is found or when the sublist to be

searched is empty. An empty sublist searched is empty. An empty sublist

occurs when

occurs when first >= last first >= last . .

(19)

Binary Search Success Binary Search Success

Example Step 1 Example Step 1

target = 23

target = 23

(20)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Binary Search Success Binary Search Success

Example Step 2 Example Step 2

target = 23

target = 23

(21)

Binary Search Success Binary Search Success

Example Step 3 Example Step 3

target = 23

target = 23

(22)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Binary Search Failure Binary Search Failure

Example Step 1 Example Step 1

target = 4

target = 4

(23)

Binary Search Failure Binary Search Failure

Example Step 2 Example Step 2

target = 4

target = 4

(24)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Binary Search Failure Binary Search Failure

Example Step 3 Example Step 3

target = 4

target = 4

(25)

Binary Search Failure Binary Search Failure

Example Step 4 Example Step 4

Index range [2,2). first

Index range [2,2). first ≥ last, so the search terminates ≥ last, so the search terminates unsuccessfully. The return value is -1.

unsuccessfully. The return value is -1.

(26)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

binSearch() Method binSearch() Method

public static int binSearch(int arr[], int first, int last, int target) {

// index of the midpoint int mid;

// value that is assigned arr[mid]

int midValue;

// test for nonempty sublist while (first < last)

{

mid = (first+last)/2;

midValue = arr[mid];

(27)

binSearch() Method binSearch() Method

(concluded) (concluded)

if (target == midValue) // have a match

return mid;

// determine which sublist to search else if (target < midValue)

// search lower sublist; reset last last = mid;

else

// search upper sublist; reset first first = mid+1;

}

// target not found

(28)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

System/Memory Efficiency System/Memory Efficiency

 System efficiency measures how well an System efficiency measures how well an algorithm runs on a particular machine.

algorithm runs on a particular machine.

 Memory efficiency is a measure of the Memory efficiency is a measure of the

amount of memory an algorithm uses. If amount of memory an algorithm uses. If

an algorithm uses too much memory, it an algorithm uses too much memory, it

can be too slow or may not execute at all can be too slow or may not execute at all

on a particular system.

on a particular system.

(29)

Running Time Analysis Running Time Analysis

 Machine independent measures of Machine independent measures of

efficiency involve counting the number of efficiency involve counting the number of

comparisons, assignment statements, etc.

comparisons, assignment statements, etc.

 To analyze the complexity of an algorithm, To analyze the complexity of an algorithm, identify the dominant operation and

identify the dominant operation and

measure the number of times it occurs.

measure the number of times it occurs.

(30)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Running Time: min() Method Running Time: min() Method

 Count the number of comparisons, T(n), Count the number of comparisons, T(n), required to locate the minimum of the required to locate the minimum of the

elements in an n-element array.

elements in an n-element array.

T(n) = n-1

T(n) = n-1

(31)

Running Time: Selection Sort Running Time: Selection Sort

Count the number of comparisons in the n-1 Count the number of comparisons in the n-1 passes of the algorithm.

passes of the algorithm.

T(n) = (n-1) + (n-2) + ... + 2 + 1 T(n) = (n-1) + (n-2) + ... + 2 + 1

T(n) = n(n-1)/2 = n

T(n) = n(n-1)/2 = n

22

/2 - n/2 /2 - n/2

(32)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Running Time:

Running Time:

Sequential Search Sequential Search

Best case: Locating the target at index 0. Best case: Locating the target at index 0.

T(n) = 1 T(n) = 1

Worst caset: Locating the target at index n-1 or Worst caset: Locating the target at index n-1 or finding no match.

finding no match.

T(n) = n T(n) = n

Average case: Average of the number of Average case: Average of the number of

comparisons to locate a target at each position.

comparisons to locate a target at each position.

T(n)=(1+2+3...+n)/n=n(n+1)/2 (1/n) T(n)=(1+2+3...+n)/n=n(n+1)/2 (1/n)

= (n+1)/2

= (n+1)/2

(33)

Running Time: Binary Search Running Time: Binary Search

Best case: Target found at first midpoint. Best case: Target found at first midpoint.

T(n) = 1 T(n) = 1

Worst case: Length of sublists decrease by a factor of two at Worst case: Length of sublists decrease by a factor of two at each iteration.

each iteration.

T(n) = (int)log

T(n) = (int)log

22

n + 1 n + 1

Average case: A sophisticated analysis shows that the average Average case: A sophisticated analysis shows that the average number of iterations is one less than the number in the worst number of iterations is one less than the number in the worst

case.

case.

(34)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Big-O Notation Big-O Notation

To get an approximate measure for T(n), To get an approximate measure for T(n),

define a notation that involves the dominant define a notation that involves the dominant

term of T(n). O(<term>) is called the

term of T(n). O(<term>) is called the Big-O Big-O measure for the algorithm.

measure for the algorithm.

Selection sort is O(n Selection sort is O(n

22

). ).

The average case for sequential search is O(n). The average case for sequential search is O(n).

The worst case for binary search is O(log The worst case for binary search is O(log

22

n). n).

If T(n) = 8n If T(n) = 8n

33

+5n +5n

22

-11n+1, then T(n) is O(n -11n+1, then T(n) is O(n

33

). ).

(35)

Common Orders of Magnitude Common Orders of Magnitude

Constant time: Algorithm is O(1) when its Constant time: Algorithm is O(1) when its running time is independent of the

running time is independent of the number of items.

number of items.

Examples: Find the minimum of an ordered Examples: Find the minimum of an ordered n-element array.

n-element array.

1 3 4 6 8 1 0 1 5 2 0 3 5 5 5

m i n i m u m = a r r [ 0 ]

a r r

(36)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Common Orders of Magnitude Common Orders of Magnitude

(continued) (continued)

 Linear: Algorithm is O(n) when its running Linear: Algorithm is O(n) when its running time is proportional to n, the size of the

time is proportional to n, the size of the list. If n doubles, number of operations list. If n doubles, number of operations

doubles.

doubles.

Example: Find the minimum of an unordered Example: Find the minimum of an unordered n-element array.

n-element array.

(37)

Common Orders of Magnitude Common Orders of Magnitude

(continued) (continued)

Quadratic: An algorithm is quadratic if its Quadratic: An algorithm is quadratic if its running time is O(n

running time is O(n

22

). If n doubles, the number ). If n doubles, the number of operations increases by a factor of 4.

of operations increases by a factor of 4.

Example: Selection sort. Example: Selection sort.

Cubic: An algorithm is cubic if its running time Cubic: An algorithm is cubic if its running time is O(n

is O(n

33

). Doubling n increases the number of ). Doubling n increases the number of operations by a factor of 8.

operations by a factor of 8.

Example: The number of products in matrix Example: The number of products in matrix multiplication is cubic.

multiplication is cubic.

(38)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Common Orders of Magnitude Common Orders of Magnitude

(continued) (continued)

 Logarithmic: An algorithm is logarithmic if Logarithmic: An algorithm is logarithmic if its running time is O(log

its running time is O(log

22

) or O(n log ) or O(n log

22

). ).

Occurs when the algorithm repeatedly Occurs when the algorithm repeatedly

subdivides the data into sublists whose subdivides the data into sublists whose

sizes are 1/2, 1/4, 1/8, ... of the original sizes are 1/2, 1/4, 1/8, ... of the original

size n.

size n.

Example: Binary search is O(log Example: Binary search is O(log

22

), and ), and quicksort is O(n log

quicksort is O(n log

22

). ).

(39)

Common Orders of Magnitude Common Orders of Magnitude

(concluded) (concluded)

 Exponential: An algorithm is exponential if Exponential: An algorithm is exponential if its running time is O(a

its running time is O(a

nn

). These algorithms ). These algorithms deal with problems that require searching deal with problems that require searching

through a large number of potential through a large number of potential

solutions before finding an answer.

solutions before finding an answer.

Example: The traveling salesperson problem. Example: The traveling salesperson problem.

(40)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Comparison of Graphs

Comparison of Graphs

(41)

Orders of Magnitude for Orders of Magnitude for

Selected Values of n

Selected Values of n

(42)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Timing Class

The Timing Class

(43)

Timing Class Example Timing Class Example

Timing sortTimer = new Timing();

double timeInSec;

// flank the process with calls to start() and stop() sortTimer.start(); // start timing

Sort.selectionSort(arr); // the sorting process timeInSec = sortTimer.stop(); // get time in seconds

(44)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Program 4.1 Program 4.1

import java.util.Random;

import java.text.DecimalFormat;

import ds.util.Arrays;

import ds.time.Timing;

public class Program4_1 {

public static void main(String[] args) {

final int ARRAY_SIZE = 100000, TARGET_SIZE = 50000;

// arrays for the search

int[] listSeq = new int[ARRAY_SIZE], listBin = new int[ARRAY_SIZE],

targetList = new int[TARGET_SIZE];

int i;

(45)

Program 4.1 (continued) Program 4.1 (continued)

// use Timing object t to compute // times for each process

Timing t = new Timing();

double seqTime, binTime;

// random number object Random rnd = new Random();

// format real numbers with // three decimal places

DecimalFormat fmt =

new DecimalFormat("#.000");

(46)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Program 4.1 (continued) Program 4.1 (continued)

// initialize the arrays with // random numbers in the

// range 0 to 999,999

for (i = 0; i < ARRAY_SIZE; i++) listSeq[i] =

listBin[i] =

rnd.nextInt(1000000);

// initialize targetList with // random numbers in the

// same range 0 to 999,999

for (i=0;i < TARGET_SIZE; i++)

targetList[i] = rnd.nextInt(1000000);

// time the sequential search // with elements from listSeq t.start();

for (i = 0; i < TARGET_SIZE; i++)

Arrays.seqSearch(listSeq,0,ARRAY_SIZE, targetList[i]);

(47)

Program 4.1 (concluded) Program 4.1 (concluded)

binTime = t.stop();

System.out.println(

"Binary Search takes " +

fmt.format(binTime) + " seconds.");

System.out.println(

"Ratio of sequential to" + "binary search time is " + fmt.format(seqTime/binTime));

} } /*

Run:

Sequential Search takes 16.094 seconds.

Referensi

Dokumen terkait

You will want to write two functions, one that is a method to call on a binary search tree to delete a value, the other would be a hidden recursive delete from tree function.. The