Main Index

49
1 Main Index Conten ts 1 Main Index Conten ts Selection Sort (3 slides) Selection Sort Alg . (3 slides) Search Algorithms (6 slides) Illustrating the Binary Illustrating the Binary Search Search -Successful (3 slides) -Unsuccessful (3 slides) Binary Search Alg . (3 slides) Big-O Notation Constant Time Algorithm s Chapter 3 Chapter 3 Introduction to Algorithms Introduction to Algorithms Selection Sort Selection Sort Algorithm Algorithm -Integer Version -String Version Template Syntax (4 slides) Recursive Def n of the Power Fnc Stopping Conditions for - Recursive Algorithms Implementing the Recurs ive- Power Function Tower of Hanoi w/ Recur sion (3 slides) Fibonacci Numbers Using - Iteration (2 slides)

description

Chapter 3 – Introduction to Algorithms. 1. Main Index. Contents. Selection Sort (3 slides) Selection Sort Alg. (3 slides) Search Algorithms (6 slides) Illustrating the Binary Search - Successful (3 slides) - Unsuccessful (3 slides) Binary Search Alg. (3 slides) Big-O Notation - PowerPoint PPT Presentation

Transcript of Main Index

1 Main Index Contents1 Main Index Contents

Selection Sort (3 slides)Selection Sort Alg. (3 slides)Search Algorithms (6 slides)

Illustrating the Binary SearchIllustrating the Binary Search-Successful (3 slides)-Unsuccessful (3 slides)

Binary Search Alg. (3 slides)Big-O NotationConstant Time AlgorithmsLinear Time AlgorithmsExponential Algs. (2 slides)Logarithmic Time Algorithms

Chapter 3 Chapter 3 – – Introduction to AlgorithmsIntroduction to Algorithms

Selection Sort AlgorithmSelection Sort Algorithm-Integer Version-String Version

Template Syntax (4 slides)

Recursive Defn of the Power Fnc

Stopping Conditions for-Recursive Algorithms

Implementing the Recursive-Power Function

Tower of Hanoi w/ Recursion(3 slides)

Fibonacci Numbers Using- Iteration (2 slides)

Summary Slides (5 slides)

2 Main Index Contents2 Main Index Contents

Selection SortSelection Sort- 5 Element Array- 5 Element Array

Pass 0: Scan the entire list from arr[0] to arr[4] and

identify 20 at index 1 as the smallest element. Exchange 20 with arr[0] = 50, the first element

in the list.

P as s 0 : S e le c t 20 a t in d e x 1 E x ch a n ge a rr[1 ] a n d a rr[0 ]

5 0 4 0 7 5 3 5

p a s s = 0

2 0

3 Main Index Contents3 Main Index Contents

Selection SortSelection Sort- 5 Element Array- 5 Element Array

Pass 1: Scan the sublist 50, 40, 75, and 35. Exchange the smallest element 35 at index 4

with arr[1] = 50.

2 0 5 0 4 0 7 5

p a s s = 1

3 5

P as s 1 : S e lec t 35 a t in d e x 4 E x c h a n ge a rr[4 ] an d a rr[1 ]

4 Main Index Contents4 Main Index Contents

Selection SortSelection Sort- 5 Element Array- 5 Element Array

Pass 2: Locate the smallest element in the sublist 40,

75, and 50.

2 0 3 5 7 5 5 0

p a s s = 2

4 0

P a s s 2 : S e lec t 40 a t in d e x 2 N o e x c h a n ge n e ces s a ry

5 Main Index Contents5 Main Index Contents

Selection SortSelection Sort- 5 Element Array- 5 Element Array

Pass 3: Two elements remain to be sorted. Scan the sublist 75, 50 and exchange the

smaller element with arr[3]. The exchange places 50 at index 4 in arr[3].

p a s s = 3

2 0 3 5 4 0 7 5 5 0

P a s s 3 : S e lec t 50 a t in d e x 4 E x c h a n ge a rr[4 ] an d a rr[3 ]

6 Main Index Contents6 Main Index Contents

Selection SortSelection Sort- 5 Element Array- 5 Element Array

2 0 3 5 4 0

S o rted lis t

5 0 7 5

7 Main Index Contents7 Main Index Contents

Selection Sort AlgorithmSelection Sort Algorithmvoid selectionSort(int arr[], int n){int smallIndex;// index of smallest

// element in the sublistint pass, j;int temp;

 // pass has the range 0 to n-2

8 Main Index Contents8 Main Index Contents

Selection Sort AlgorithmSelection Sort Algorithmfor (pass = 0; pass < n-1; pass++){// scan the sublist starting at index // pass

smallIndex = pass;// 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

9 Main Index Contents9 Main Index Contents

Selection Sort AlgorithmSelection Sort Algorithmif (arr[j] < arr[smallIndex])smallIndex = j;

// if smallIndex and pass are not the // same location, exchange the // smallest item in the sublist

with // arr[pass]if (smallIndex != pass){temp = arr[pass];arr[pass] = arr[smallIndex];arr[smallIndex] = temp;

}}

}

10 Main Index Contents

Search AlgorithmsSearch Algorithms

0 1 2 6 7 8

firs t las t

A rray arr

543

Search algorithms start with a target value and employ some strategy to visit the elements looking for a match.– If target is found, the index of the matching

element becomes the return value.

11 Main Index Contents11 Main Index Contents

6 4 2 9 5 10

ind ex = s eq S earc h(arr, 0 , 8 , 3);

7Ind ex 0 1 2 3 4 5 6 7

3

target = 38

m atc h at ind ex = 5return ind ex 5

Search Search AlgorithmsAlgorithms

6 4 2 9 5 3 10 7Ind ex 0 1 2 3 4 5 6 7

target = 98

no m atc hreturn ind ex 8

ind ex = s eq S earc h(arr, 0, 8 , 9);

12 Main Index Contents12 Main Index Contents

Search AlgorithmsSearch Algorithms- Sequential Search Algorithm- Sequential Search Algorithm

int seqSearch(const int arr[], int first, int last, int target){int i = first;// scan indices in the range first <= I < last; // test for a match or index out of range.while(i != last && arr[i] != target)i++;

 return i;// i is index of match or i = last if no

match}

13 Main Index Contents

Search Algorithms Search Algorithms Case 1.

A match occurs. The search is complete and mid is the index that locates the target. if (midValue == target)

// found matchreturn mid;

m idfirs t

target

C as e 1: target = m id valueS earc h is d o ne

las t-1 las t

14 Main Index Contents

Search Algorithms Search Algorithms Case 2.

The value of target is less than midvalue and the search must continue in the lower sublist. Reposition the index last to the

end of the sublist (last = mid).// search the lower sublist

if (target < midvalue)<reposition last to mid><search sublist arr[first]…arr[mid-1]

las t-1firs t

target

C as e 2: target < m id valueS earc h lo w er s ub lis t

m id -1 las t

15 Main Index Contents

Search Algorithms Search Algorithms Case 3.

The value of target is greater than midvalue and the search must continue in the upper sublist . Reposition the index first to the front of the sublist (first = mid+1).// search upper sublist if (target > midvalue) <reposition first to mid+1><search sublist arr[mid+1]…arr[last-1]>

C as e 3: target > m id valueS earc h up p er s ub lis t

las t-1new firs t = m id + 1

firs t

target

las t

16 Main Index Contents16 Main Index Contents

Illustrating the Binary SearchIllustrating the Binary Search- Successful Search- Successful Search

1. Search for target = 23Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4.

Since target = 23 > midvalue = 12, step 2 searches the upper sublist with first = 5 and last = 9.

m id

-7 3 5 8 12 16arr

0 1 2 3 4 5

23 33 55

6 7 8 9

17 Main Index Contents17 Main Index Contents

Illustrating the Binary SearchIllustrating the Binary Search- Successful Search- Successful Search

Step 2:Indices first = 5, last = 9, mid = (5+9)/2 = 7.

Since target = 23 < midvalue = 33, step 3 searches the lower sublist with first = 5 and last = 7.

m id

-7 3 5 8 12 16arr0 1 2 3 4 5

23 33 556 7 8 9

18 Main Index Contents18 Main Index Contents

Illustrating the Binary SearchIllustrating the Binary Search- Successful Search- Successful Search

Step 3: Indices first = 5, last = 7, mid = (5+7)/2 = 6.

Since target = midvalue = 23, a match is found at index mid = 6.

m id

-7 3 5 8 12 16arr0 1 2 3 4 5

23 33 556 7 8 9

19 Main Index Contents

Illustrating the Binary SearchIllustrating the Binary Search- Unsuccessful Search- Unsuccessful Search

Search for target = 4. Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4.

m id

-7 3 5 8 12 16arr

0 1 2 3 4 5

23 33 55

6 7 8 9

Since target = 4 < midvalue = 12, step 2 searches the lower sublist with first = 0 and last = 4.

20 Main Index Contents

Illustrating the Binary SearchIllustrating the Binary Search- Unsuccessful Search- Unsuccessful Search

Step 2: Indices first = 0, last = 4, mid = (0+4)/2 = 2.

Since target = 4 < midvalue = 5, step 3 searches the lower sublist with first = 0 and last 2.

m id

-7 3 5 8 12 16arr0 1 2 3 4 5

23 33 556 7 8

21 Main Index Contents

Illustrating the Binary SearchIllustrating the Binary Search- Unsuccessful Search- Unsuccessful Search

Step 3: Indices first = 0, last = 2, mid = (0+2)/2 = 1.

Since target = 4 > midvalue = 3, step 4 should search the upper sublist with first = 2 and last =2. However, since first >= last, the target is not in the list and we return index last = 9.

m id

-7 3 5 8 12 16arr

0 1 2 3 4 5

23 33 55

6 7 8 9

22 Main Index Contents22 Main Index Contents

Binary Search AlgorithmBinary Search AlgorithmInt binSearch(const int arr[], int

first, int last, int target){int mid; // index of the midpointint midvalue;// object that is

// assigned arr[mid]int origLast = last;

// save original value of last

23 Main Index Contents23 Main Index Contents

Binary Search AlgorithmBinary Search Algorithmwhile (first < last)// test for nonempty sublist{mid = (first+last)/2;midvalue = arr[mid];if (target == midvalue)return mid; // have a match

// determine which sublist to // search

24 Main Index Contents24 Main Index Contents

Binary Search AlgorithmBinary Search Algorithmelse if (target < midvalue)last = mid;// search lower sublist. reset

lastelsefirst = mid+1;// search upper sublist. Reset

first

}

return origLast;// target not found

}

25 Main Index Contents

Big-O notationBig-O notation

For the selection sort, the number of comparisons is T(n) = n2/2 - n/2.

2

n

2

2n

T(n)

Entire expression is called the "Big-O" measure for the algorithm.

** Big-O notation provides a machine independent means for determining the efficiency of an

Algorithm.

n = 100: T(100) = 1002/2 -100/2 = 10000/2 - 100/2 = 5,000 - 50 = 4,950

26 Main Index Contents26 Main Index Contents

Constant Time AlgorithmsConstant Time AlgorithmsAn algorithm is O(1) when its running time is independent of the number of data items. The algorithm runs in constant time.

The storing of the element involves a simple assignment statement and thus has efficiency O(1).

fro nt rear

D irec t In se r t a t R ear

27 Main Index Contents

Linear Time AlgorithmsLinear Time AlgorithmsAn algorithm is O(n) when its running time is proportional to the size of the list. When the number of elements doubles, the number of operations doubles.

S eq ue ntial S e arch fo r the Minim um E lem ent in an A rray

32 46 8 12 3

m in im u m elem en t fou n d in th e list a fter n com p a rison s

n = 51 2 3 4 5

28 Main Index Contents28 Main Index Contents

Exponential AlgorithmsExponential AlgorithmsAlgorithms with running time O(n2) are

quadratic.– practical only for relatively small values of n.

Whenever n doubles, the running time of the algorithm increases by a factor of 4.

Algorithms with running time O(n3)are cubic.– efficiency is generally poor; doubling the size

of n increases the running time eight-fold.

29 Main Index Contents29 Main Index Contents

Exponential AlgorithmsExponential Algorithms

n log2n n log2n n2 n3 2n 2 1 2 4 8 4 4 2 8 16 64 16 8 3 24 64 512 256 16 4 64 256 4096 65536 32 5 160 1024 32768 4294967296 128 7 896 16384 2097152 3.4 x 1038 1024 10 10240 1048576 1073741824 1.8 x 10308 65536 16 1048576 4294967296 2.8 x 1014 Forget it!

30 Main Index Contents

Logarithmic Time AlgorithmsLogarithmic Time AlgorithmsThe logarithm of n, base 2, is commonly used when analyzing computer algorithms.Ex. log2(2) = 1

log2(75) = 6.2288When compared to the functions n and n2, the function log2 n grows very slowly.

nn 2

lo g 2n

31 Main Index Contents31 Main Index Contents

Selection Sort AlgorithmSelection Sort AlgorithmInteger VersionInteger Version

void selectionSort(int arr[], int n) { . . .int temp; // int temp used for the exchangefor (pass = 0; pass < n-1; pass++){. . . if (arr[j] < arr[smallIndex]) // compare integer elements . . .

}}

32 Main Index Contents32 Main Index Contents

Selection Sort AlgorithmSelection Sort AlgorithmString VersionString Version

void selectionSort(string arr[], int n) { . . .string temp; // double temp used for the exchange

for (pass = 0; pass < n-1; pass++) {. . .if (arr[j] < arr[smallIndex])// compare string element

. . .}

}

33 Main Index Contents

Template SyntaxTemplate Syntax template function syntax includes the keyword

template followed by a non-empty list of formal types enclosed in angle brackets.

In the argument list, each type is preceded by the keyword typename, and types are separated by commas.

// argument list with a multiple template // types template <typename T, typename U,

typename V, ...>

34 Main Index Contents

Template Syntax ExampleTemplate Syntax Exampletemplate <typename T>void selectionSort(T arr[], int n){ int smallIndex; // index of smallest element in the // sublistint pass, j;T temp;

35 Main Index Contents

Template Syntax ExampleTemplate Syntax Example// pass has the range 0 to n-2for (pass = 0; pass < n-1; pass++){

// scan the sublist starting at // index pass

smallIndex = pass;

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

for (j = pass+1; j < n; j++) // update if smaller element found

36 Main Index Contents

Template Syntax ExampleTemplate Syntax Exampleif (arr[j] < arr[smallIndex])

smallIndex = j;// if smallIndex and pass are not // the same location, exchange the // smallest item in the sublist

with // arr[pass]

if (smallIndex != pass){temp = arr[pass];arr[pass] =

arr[smallIndex];arr[smallIndex] = temp; }

} }

37 Main Index Contents37 Main Index Contents

Recursive Definition of the Recursive Definition of the Power FunctionPower Function

A recursive definition distinguishes between the exponent n = 0 (starting point) and n 1 which assumes we already know the value xn-1.

After determining a starting point, each step uses a known power of 2 and doubles it to compute the next result.– Using this process gives us a new definition for the

power function, xn. We compute all successive powers of x by

multiplying the previous value by x.

1,*0,1

1 nxxn

x nn

38 Main Index Contents

Stopping Conditions for Stopping Conditions for Recursive AlgorithmsRecursive Algorithms

Use a recursive function to implement a recursive algorithm. – The design of a recursive function consists

of1. One or more stopping conditions that can be directly evaluated for certain arguments. 2. One or more recursive steps in which a current value of the function can be computed by repeated calling of the function with arguments that will eventually arrive at a stopping condition.

39 Main Index Contents39 Main Index Contents

Implementing the Recursive Implementing the Recursive Power FunctionPower Function

Recursive power():double power(double x, int n) // n is a non-negative integer{if (n == 0)return 1.0; // stopping condition

elsereturn x * power(x,n-1);// recursive step

}

40 Main Index Contents40 Main Index Contents

Solving the Tower of Hanoi Solving the Tower of Hanoi Puzzle using RecursionPuzzle using Recursion

N eed le A

. . . . . . . .

N eed le CN eed le B N eed le C

. . . . . . . .

N eed le BN eed le A

N e e dle A N e e dle B N e e dle C

1

N e e dle B N e e dle CN e e dle A

2

3

N e e dle A N e e dle B N e e dle C N e e dle A N e e dle B N e e dle C

41 Main Index Contents41 Main Index Contents

Solving the Tower of Hanoi Solving the Tower of Hanoi Puzzle using RecursionPuzzle using Recursion

N eed le A

. . . . . . . .

N eed le CN eed le B N eed le C

. . . . . . . .

N eed le BN eed le A

N e e dle A N e e dle B N e e dle C N e e dle A N e e dle B N e e dle C

4

42 Main Index Contents42 Main Index Contents

Solving the Tower of Hanoi Solving the Tower of Hanoi Puzzle using RecursionPuzzle using Recursion

N eed le A

. . . . . . . .

N eed le CN eed le B N eed le C

. . . . . . . .

N eed le BN eed le A

N eed le A N eed le B N eed le C N eed le A N eed le B N eed le C

56

N eed le A N eed le B N eed le C N eed le A N eed le B N eed le C

7

43 Main Index Contents

Fibonacci Numbers using Fibonacci Numbers using IterationIteration

int fibiter(int n){// integers to store previous two

// Fibonacci value int oneback = 1, twoback = 1, current;int i;

// return is immediate for first two numbers

if (n == 1 || n == 2)return 1;

44 Main Index Contents

Fibonacci Numbers using Fibonacci Numbers using IterationIteration

else// compute successive terms beginning at 3

for (i = 3; i <= n; i++){current = oneback + twoback;twoback = oneback;// update for next calculation

oneback = current;}

return current;}

45 Main Index Contents45 Main Index Contents

Summary Slide 1Summary Slide 1

§- The simplest form of searching is the sequential search.

§- It compares the target with every element in a list until matching the target or reaching the end of

the list.

§- If the list is in sorted order, the binary search algorithm is more efficient.

§- It exploits the structure of an ordered list to produce very fast search times.

46 Main Index Contents46 Main Index Contents

Summary Slide 2Summary Slide 2

§- Big-O notation measures the efficiency of an algorithm by estimating the number of certain

operations that the algorithm must perform.- For searching and sorting algorithms, the

operation is data comparison.- Big-O measure is very useful for selecting among

competing algorithms.

47 Main Index Contents47 Main Index Contents

Summary Slide 3Summary Slide 3

§- The running time of the sequential search is O(n) for the worst and the average cases.

§- The worst and average case for the binary search is O(log2n).

§- Timing data obtained from a program provides experimental evidence to support the greater

efficiency of the binary search.

48 Main Index Contents48 Main Index Contents

Summary Slide 4Summary Slide 4

§- C++ provides a template mechanism that allows a programmer to write a single version of a function with general type arguments.- If a main program wants to call the function

several times with different runtime arguments, the compiler looks at the types of the runtime arguments and creates different versions of the function that matches the types.

49 Main Index Contents49 Main Index Contents

Summary Slide 5Summary Slide 5

§- An algorithm is recursive if it calls itself for smaller problems of its own type.

§- Eventually, these problems must lead to one or more stopping conditions.

- The solution at a stopping condition leads to the solution of previous problems.

- In the implementation of recursion by a C++ function, the function calls itself.