Lecture 16: Searching and Sorting (Thursday)

27
1 Lecture 16: Searching and Sorting (Thursday) Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science Test Tomorrow .

description

CompSci 105 SS 2005 Principles of Computer Science. Test Tomorrow. Lecture 16: Searching and Sorting (Thursday). Lecturer: Santokh Singh. Array Implementation. 0. 1. 2. 3. 4. 5. 6. 7. items:. 3. 5. 1. front:. 0. back:. 2. Textbook, pp. 306ff. “Circular Array”. 7. 0. 3. - PowerPoint PPT Presentation

Transcript of Lecture 16: Searching and Sorting (Thursday)

Page 1: Lecture 16:  Searching and Sorting (Thursday)

1

Lecture 16: Searching and Sorting (Thursday)

Lecturer: Santokh Singh

CompSci 105 SS 2005

Principles of Computer Science

Test Tomorrow.

Page 2: Lecture 16:  Searching and Sorting (Thursday)

2

Array Implementation

items: 153

0

front:

0 1 2 3

4 5 6 7

Textbook, pp. 306ff

2back:

Page 3: Lecture 16:  Searching and Sorting (Thursday)

3

0

1

2

34

5

6

7

3

5

1

“Circular Array”

items:

0front:

2back:

Java Code, Textbook, pp. 310ff

Page 4: Lecture 16:  Searching and Sorting (Thursday)

4

Linked List Implementation

3 5 1

firstNode

Textbook, pp. 302ff,

lastNode

Page 5: Lecture 16:  Searching and Sorting (Thursday)

5

Circular Linked List

3 5 1

Java code, textbook, pp. 305ff,

lastNode

Page 6: Lecture 16:  Searching and Sorting (Thursday)

6

ADT List Implementation

Java Code, Textbook, pp. 263ff

1. 1

2. 5

3. 3

4.

5.

6.

Page 7: Lecture 16:  Searching and Sorting (Thursday)

7

Algorithm Efficiency

Challenges in Measuring Efficiency

Counting Operations

Growth Rates of Functions

Big O Notation

Examples

General Rules

Page 8: Lecture 16:  Searching and Sorting (Thursday)

8

Algorithm Efficiency

Challenges in Measuring Efficiency

Counting Operations

Growth Rates of Functions

Big O Notation

Examples

General Rules

Page 9: Lecture 16:  Searching and Sorting (Thursday)

9

Node curr = head;

while (curr != null )

{

System.out.println( curr.getItem() );

curr = curr.getNext();

}

Textbook, p. 374

Page 10: Lecture 16:  Searching and Sorting (Thursday)

10

Rate of Growth

x + n y

n

time

Page 11: Lecture 16:  Searching and Sorting (Thursday)

11

for ( i=1 to n ) {

for ( j=1 to n ) {

for ( k=1 to 5 ) {

Task T

}

}

}

Page 12: Lecture 16:  Searching and Sorting (Thursday)

12

Rate of Growth n2 z

n

time

Page 13: Lecture 16:  Searching and Sorting (Thursday)

13

Rate of Growth

x + n y

n2 z

n

time

Page 14: Lecture 16:  Searching and Sorting (Thursday)

14

Algorithm Efficiency

Challenges in Measuring Efficiency

Counting Operations

Growth Rates of Functions

Big O Notation

Examples

General Rules

Page 15: Lecture 16:  Searching and Sorting (Thursday)

15

Big O Notation

“An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥  n0”

- Textbook, p. 376

Page 16: Lecture 16:  Searching and Sorting (Thursday)

16

Big O Notation

“An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥  n0”

- Textbook, p. 376

Page 17: Lecture 16:  Searching and Sorting (Thursday)

17

Big O Notation

“An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥  n0”

- Textbook, p. 376

Worst Case

Page 18: Lecture 16:  Searching and Sorting (Thursday)

18

Big O Notation

“An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥  n0”

- Textbook, p. 376

Worst Case Large Problems

Page 19: Lecture 16:  Searching and Sorting (Thursday)

19

Rate of Growth n2 z

n

time

“An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥  n0”

x + n y

Book Computes k and n0 exactly, p. 377

Page 20: Lecture 16:  Searching and Sorting (Thursday)

20

Rate of Growth

x + n y

n2 z

n

time

“An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥  n0”

k n

Book Computes k and n0 exactly, p. 377

Page 21: Lecture 16:  Searching and Sorting (Thursday)

21

for ( i=1 to n ) {

for ( j=1 to n ) {

for ( k=1 to 5 ) {

Task T

}

}

}

Page 22: Lecture 16:  Searching and Sorting (Thursday)

22

for ( i=1 to n ) {

for ( j=1 to i ) {

for ( k=1 to 5 ) {

Task T

}

}

}

Textbook, p. 374-5, 377

Page 23: Lecture 16:  Searching and Sorting (Thursday)

23

O Notation Tricks

• Ignore “low-order” terms

• Ignore Constants

• Can combine O()’s

Textbook, p. 380

Page 24: Lecture 16:  Searching and Sorting (Thursday)

24

Comparing Growth Rates

O(n)

FasterGrowth

FasterCode

O(n2)

Textbook, p. 378

Page 25: Lecture 16:  Searching and Sorting (Thursday)

25

Comparing Growth Rates

O(n)

FasterGrowth

FasterCode

O(n2)

O(1)Textbook, p. 378

Page 26: Lecture 16:  Searching and Sorting (Thursday)

26

Comparing Growth Rates

O(n)

FasterGrowth

FasterCode

O(n2)

O(log n)

O(1)

O(2n)

Textbook, p. 378

Page 27: Lecture 16:  Searching and Sorting (Thursday)

27

Comparing Growth Rates

O(n)

FasterGrowth

FasterCode

O(n2)

O(n3)

O(n log n)

O(log n)

O(1)

O(2n)

Textbook, p. 378