70
Algorithm Speed Efficiency and Big Oh notation

Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Algorithm SpeedEfficiency and Big Oh notation

Page 2: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Algorithm

• A series of steps to complete a task• Eg: IKEA assembly instructions, computer

program, flowchart, recipe to bake a cake• Cornerstone of computer science; a break-

though in an algorithm often means a radical change in the industry

Page 3: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

PageRank: Took search results and ordered them

Google worth $632 billion

in 2020

Page 4: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,
Page 5: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Problem: The teacher needs to hand out a set of assignments, one to each student.

Page 6: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

One of the big considerations is the time it will take to complete.

That is related to to the efficiency of the algorithm.

How will we hand out the papers?

Page 7: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Because time (seconds or nanoseconds) is hardware dependant, we measure an algorithm in the number of operations it takes.

Page 8: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

The number of operations depends on the size of the data set.

Page 9: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

In this case, the “data set” is the class size.

Thus, we will measure it in terms of n, which will be the class size.

Later this lesson, n will be the array size.

Page 10: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1

Start at one corner,Go up and down the rows,Handing out the paper one by one.

Algorithm

Page 11: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24

25 26 27 28 29 30

Page 12: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24

25 26 27 28 29 30

Page 13: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24

25 26 27 28 29 30

Page 14: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24

25 26 27 28 29 30

Page 15: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24

25 26 27 28 29 30

Page 16: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24

25 26 27 28 29 30

Page 17: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24

25 26 27 28 29 30

Page 18: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24

25 26 27 28 29 30

Page 19: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24

25 26 27 28 29 30

To pass out one by one…= n= 30

Page 20: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

2

Hand out one pile to each rowEach student passes the pile back.

Algorithm

Page 21: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

Page 22: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

1

Count of Actions

Page 23: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

2

Count of Actions

Page 24: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

3

Count of Actions

Page 25: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

4

Count of Actions

Page 26: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

5

Count of Actions

Page 27: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

6

Count of Actions

Page 28: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

7

Count of Actions

Page 29: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

8

Count of Actions

Page 30: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

9

Count of Actions

Page 31: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

10

Count of Actions

Page 32: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

11

Count of Actions

Page 33: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1 2 3 4 5 6

2

3

4

5

One to each row

n/5 rows + (n/6) pass back= 6 + 5= 11

11

Count of Actions

Page 34: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

3

Throw the papers in the airThe student shuffle in to grab them

Algorithm

Page 35: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1

2

3

4

Throw in the air….= n/4= 8 + time to shuffle out….= 8+ n*20 ?!?= 608

Additional considerations…It’s chaos….

Page 36: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

4

Take one yourself.Find two people who don’t have the sheet, give each of them half the pile.

Algorithm

Page 37: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

2 3

4 5 6 7

8 9 10 11 12 13 14 15

1617

1819

2021

2223

2425

2627

2829

30

1

1Count of Actions

Page 38: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

2 3

4 5 6 7

8 9 10 11 12 13 14 15

1617

1819

2021

2223

2425

2627

2829

30

1

2Count of Actions

Page 39: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

2 3

4 5 6 7

8 9 10 11 12 13 14 15

1617

1819

2021

2223

2425

2627

2829

30

1

3Count of Actions

Page 40: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

2 3

4 5 6 7

8 9 10 11 12 13 14 15

1617

1819

2021

2223

2425

2627

2829

30

1

4Count of Actions

Page 41: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

2 3

4 5 6 7

8 9 10 11 12 13 14 15

1617

1819

2021

2223

2425

2627

2829

30

1

5Count of Actions

Page 42: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

1617

1819

2021

2223

2425

2627

2829

30

Divide in half, =log n = 5

5Count of Actions

Page 43: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Big Oh Notation

• A way of measuring algorithm speed• Uses a mathematical expression for the total number of

operations that will be needed, based on the array size• Meaning of the pieces:

• O = order• n = number of elements in the array

• One loop is O(n)• One loop inside another is O(n2)

Page 44: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

for (int i = 0 ; i < DaysOfWeek.length ; i++)

{

System.out.println (DaysOfWeek [i]);

}

What speed is

this?

Page 45: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Let’s say the array has 8 values.array.length is 8.

The loop runs 8 times.

Tracing the values of i

for (int i = 0 ; i < array.length ; i++)

{

System.out.println (array [i]);

}

Page 46: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Let’s say the array has 8 values.array.length is 8.

The loop runs 8 times.

Tracing the values of i

for (int i = 0 ; i < array.length ; i++)

{

System.out.println (array [i]);

}

O(n)

Page 47: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

double min = price [0];

for (int i = 1 ; i < price.length ; i++)

{

if (min > price [i])

min = price [i];

}

System.out.println ("The lowest price is: $" + min);

Page 48: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

double min = price [0];

for (int i = 1 ; i < price.length ; i++)

{

if (min > price [i])

min = price [i];

}

System.out.println ("The lowest price is: $" + min);

O(n)

Page 49: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

for (int i = 0 ; i < a.length - 1 ; i++)

{

for (int j = 0 ; j < a.length - 1 - i ; j++)

{ // compare the two neighbours

if (a [j + 1] < a [j])

{ //swap the neighbours if necessary

int temp = a [j];

a [j] = a [j + 1];

a [j + 1] = temp;

}

}

}

The inner loop runs roughly n

times.

The outer loop runs roughly n

times

Thus, this code is run n*n times

Page 50: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Let’s say the array has 8 values.array.length is 8.

for (int i = 0 ; i < a.length - 1 ; i++)

{

for (int j = 0 ; j < a.length - 1 - i ; j++)

{ // compare the two neighbours

if (a [j + 1] < a [j])

{ //swap the neighbours if necessary

int temp = a [j];

a [j] = a [j + 1];

a [j + 1] = temp;

}

}

} The inner loop runs

roughly 64 times.

Tracing the values of i and j

Page 51: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Let’s say the array has 8 values.array.length is 8.

for (int i = 0 ; i < a.length - 1 ; i++)

{

for (int j = 0 ; j < a.length - 1 - i ; j++)

{ // compare the two neighbours

if (a [j + 1] < a [j])

{ //swap the neighbours if necessary

int temp = a [j];

a [j] = a [j + 1];

a [j + 1] = temp;

}

}

} The inner loop runs

roughly 64 times.

Tracing the values of i and j

O(n2)

Page 52: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Algorithm speeds (in order from fastest to slowest)

1.O(1), constant time2.O(logn), logarithmic time3.O(n), linear time4.O(n logn)5.O(n2), quadratic time6.O(n3), cubic time7.O(n4)8.O(2n), exponential time

Where would O(n2 logn) go?

Page 53: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Algorithm speeds (in order from fastest to slowest)

1.O(1), constant time2.O(logn), logarithmic time3.O(n), linear time4.O(n logn)5.O(n2), quadratic time6.O(n3), cubic time7.O(n4)8.O(2n), exponential time

Where would O(n2 logn) go?

Page 54: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,
Page 55: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

Speed Algorithms

O(1) Swap, add, finding the length

O(log n) Binary search

O(n) print, min, max, sum, average, delete, linear search, Bin sort

O(n logn) Quicksort, Mergesort

O(n2) Selection sort, Bubblesort

The Grade 11 algorithms and their speeds:

Page 56: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

How fast is the min algorithm?

How fast is the swap

algorithm?

Page 57: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

How fast is the min algorithm?

O(n)

How fast is the swap

algorithm?

linear

Page 58: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

How fast is the min algorithm?

O(n)

How fast is the swap

algorithm? O(1)

linear

Constant time

Page 59: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,
Page 60: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,
Page 61: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,
Page 62: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

N Cray – great hw,

0(n^3) – awful sw

TRS-80 – bad hw,

O(n) – great sw

10 0.000003 sec 0.2 sec

100

1000

2500

10,000

100,000

1,000,000

Page 63: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

N Cray – great hw,

0(n^3) – awful sw

TRS-80 – bad hw,

O(n) – great sw

10 0.000003 sec 0.2 sec

100 0.003 sec 2.0 sec

1000

2500

10,000

100,000

1,000,000

Page 64: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

N Cray – great hw,

0(n^3) – awful sw

TRS-80 – bad hw,

O(n) – great sw

10 0.000003 sec 0.2 sec

100 0.003 sec 2.0 sec

1000 3 sec 20 sec

2500

10,000

100,000

1,000,000

Page 65: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

N Cray – great hw,

0(n^3) – awful sw

TRS-80 – bad hw,

O(n) – great sw

10 0.000003 sec 0.2 sec

100 0.003 sec 2.0 sec

1000 3 sec 20 sec

2500 47 sec 49 sec

10,000

100,000

1,000,000

Page 66: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

N Cray – great hw,

0(n^3) – awful sw

TRS-80 – bad hw,

O(n) – great sw

10 0.000003 sec 0.2 sec

100 0.003 sec 2.0 sec

1000 3 sec 20 sec

2500 47 sec 49 sec

10,000 50 min 3.25 min

100,000

1,000,000

Page 67: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

N Cray – great hw,

0(n^3) – awful sw

TRS-80 – bad hw,

O(n) – great sw

10 0.000003 sec 0.2 sec

100 0.003 sec 2.0 sec

1000 3 sec 20 sec

2500 47 sec 49 sec

10,000 50 min 3.25 min

100,000 34.7 days 32.5 min

1,000,000

Page 68: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

N Cray – great hw,

0(n^3) – awful sw

TRS-80 – bad hw,

O(n) – great sw

10 0.000003 sec 0.2 sec

100 0.003 sec 2.0 sec

1000 3 sec 20 sec

2500 47 sec 49 sec

10,000 50 min 3.25 min

100,000 34.7 days 32.5 min

1,000,000 95 5.4

Page 69: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

N Cray – great hw,

0(n^3) – awful sw

TRS-80 – bad hw,

O(n) – great sw

10 0.000003 sec 0.2 sec

100 0.003 sec 2.0 sec

1000 3 sec 20 sec

2500 47 sec 49 sec

10,000 50 min 3.25 min

100,000 34.7 days 32.5 min

1,000,000 95 years 5.4 hours

Page 70: Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of steps to complete a task •Eg: IKEA assembly instructions, computer program, flowchart,

The Moral of Bentley’s Example

Fast hardware cannot compensate for a slow

algorithm.