data structures using c++ 2e the big-o notation and array-based lists

32
Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Upload: isis-flook

Post on 28-Mar-2015

242 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E

The Big-O Notation and Array-Based Lists

Page 2: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 2

Algorithm Analysis: The Big-O Notation

• Analyze algorithm after design

• Example– 50 packages delivered to 50 different houses– 50 houses one mile apart, in the same area

FIGURE 1-1 Gift shop and each dot representing a house

Page 3: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Algorithm Analysis: The Big-O Notation (cont’d.)

• Example (cont’d.)– Driver picks up all 50 packages– Drives one mile to first house, delivers first package– Drives another mile, delivers second package– Drives another mile, delivers third package, and so on– Distance driven to deliver packages

• 1+1+1+… +1 = 50 miles

– Total distance traveled: 50 + 50 = 100 miles

Data Structures Using C++ 2E 3

FIGURE 1-2 Package delivering scheme

Page 4: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Algorithm Analysis: The Big-O Notation (cont’d.)

• Example (cont’d.)– Similar route to deliver another set of 50 packages

• Driver picks up first package, drives one mile to the first house, delivers package, returns to the shop

• Driver picks up second package, drives two miles, delivers second package, returns to the shop

– Total distance traveled• 2 * (1+2+3+…+50) = 2550 miles

Data Structures Using C++ 2E 4

FIGURE 1-3 Another package delivery scheme

Page 5: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 5

Algorithm Analysis: The Big-O Notation (cont’d.)

• Example (cont’d.)– n packages to deliver to n houses, each one mile

apart– First scheme: total distance traveled

• 1+1+1+… +n = 2n miles

• Function of n

– Second scheme: total distance traveled• 2 * (1+2+3+…+n) = 2*(n(n+1) / 2) = n2+n

• Function of n2

Page 6: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 6

Algorithm Analysis: The Big-O Notation (cont’d.)

• Analyzing an algorithm– Count number of operations performed

• Not affected by computer speed

TABLE 1-1 Various values of n, 2n, n2, and n2 + n

Page 7: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation (cont’d.)

• Example 1-1– Illustrates fixed number of executed operations

1 operation

2 operations1 operation1 operation

1 operation

3 operations

Total of 9 operations

Page 8: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E

Algorithm Analysis: The Big-O Notation• Example 1-2 Illustrates dominant operations

2 operations

1 operation1 operation

1 operation

N+1 operations

2N operationsN operationsN operations

3 operations

1 operation2 operations

1 operation

3 operation

If the while loop executes N times then:

2+1+1+1+5*N + 1 + 3 + 1 + (2 ) + 3 = 5N+(15 )

Page 9: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Example

For(i=1; i<=5; i++)

For(j=1;j<=5; j++){

Cout<<“*”;Sum=sum+j;

}

1 op 6 op 5 op

5 op 30 op 25 op

25 op

25 op 25 op

Finally: 147

For(i=1; i<=n; i++)

For(j=1; j<=n; j++)

{

cout<<“*”;

Sum=sum+j;

}

2n+2+2n2+2n+3n2

= 5n2+ 4n+2

Data Structures Using C++ 2E

Page 10: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 10

Algorithm Analysis: The Big-O Notation (cont’d.)

• Search algorithm– n: represents list size– f(n): count function

• Number of comparisons in search algorithm

– c: units of computer time to execute one operation– cf(n): computer time to execute f(n) operations– Constant c depends computer speed (varies)– f(n): number of basic operations (constant)– Determine algorithm efficiency

• Knowing how function f(n) grows as problem size grows

Page 11: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 11

Algorithm Analysis: The Big-O Notation (cont’d.)

TABLE 1-2 Growth rates of various functions

Page 12: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 12

Algorithm Analysis: The Big-O Notation (cont’d.)

Figure 1-4 Growth rateof functions in Table 1-3

TABLE 1-3 Time for f(n) instructions on a computer that executes 1 billion instructions per second

Page 13: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 13

Algorithm Analysis: The Big-O Notation (cont’d.)

• Notation useful in describing algorithm behavior– Shows how a function f(n) grows as n increases

without bound

• Asymptotic– Study of the function f as n becomes larger and larger

without bound– Examples of functions

• g(n)=n2 (no linear term)

• f(n)=n2 + 4n + 20

Page 14: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 14

Algorithm Analysis: The Big-O Notation (cont’d.)

• As n becomes larger and larger– Term 4n + 20 in f(n) becomes insignificant– Term n2 becomes dominant term

TABLE 1-4 Growth rate of n2 and n2 + 4n + 20n

Page 15: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 15

Algorithm Analysis: The Big-O Notation (cont’d.)

• Algorithm analysis– If function complexity can be described by complexity

of a quadratic function without the linear term• We say the function is of O(n2) or Big-O of n2

• Let f and g be real-valued functions– Assume f and g nonnegative

• For all real numbers n, f(n) >= 0 and g(n) >= 0

• f(n) is Big-O of g(n): written f(n) = O(g(n))– If there exists positive constants c and n0 such that

f(n) <= cg(n) for all n >= n0

Page 16: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 16

Algorithm Analysis: The Big-O Notation (cont’d.)

TABLE 1-5 Some Big-O functions that appear in algorithm analysis

Page 17: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 17

Array-Based Lists

• List– Collection of elements of same type

• Length of a list– Number of elements in the list

• Many operations may be performed on a list

• Store a list in the computer’s memory– Using an array

Page 18: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 18

Array-Based Lists (cont’d.)

• Three variables needed to maintain and process a list in an array – The array holding the list elements– A variable to store the length of the list

• Number of list elements currently in the array– A variable to store array size

• Maximum number of elements that can be stored in the array

• Desirable to develop generic code– Used to implement any type of list in a program– Make use of templates

Page 19: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 19

Array-Based Lists (cont’d.)

• Define class implementing list as an abstract data type (ADT)

FIGURE 3-29 UML class diagramof the class arrayListType

Page 20: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 20

Array-Based Lists (cont’d.)

• Definitions of functions isEmpty, isFull, listSize and maxListSize

Page 21: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 21

Array-Based Lists (cont’d.)

• Template print (outputs the elements of the list) and template isItemAtEqual

Page 22: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 22

Array-Based Lists (cont’d.)

• Template insertAt

Page 23: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 23

Array-Based Lists (cont’d.)

• Template insertEnd and template removeAt

Page 24: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 24

Array-Based Lists (cont’d.)

• Template replaceAt and template clearList

Page 25: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 25

Array-Based Lists (cont’d.)

• Definition of the constructor and the destructor

Page 26: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 26

Array-Based Lists (cont’d.)

• Copy constructor– Called when object passed as a (value) parameter to

a function– Called when object declared and initialized using the

value of another object of the same type– Copies the data members of the actual object into the

corresponding data members of the formal parameter and the object being created

Page 27: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 27

Array-Based Lists (cont’d.)

• Copy constructor (cont’d.)– Definition

Page 28: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 28

Array-Based Lists (cont’d.)

• Overloading the assignment operator– Definition of the function template

Page 29: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 29

Array-Based Lists (cont’d.)

• Searching for an element– Linear search example: determining if 27 is in the list

– Definition of the function template

FIGURE 3-32 List of seven elements

Page 30: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 30

Array-Based Lists (cont’d.)

• Inserting an element

Page 31: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 31

Array-Based Lists (cont’d.)

• Removing an element

Page 32: Data Structures Using C++ 2E The Big-O Notation and Array-Based Lists

Data Structures Using C++ 2E 32

Array-Based Lists (cont’d.)TABLE 3-1 Time complexity of list operations