Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of...
Transcript of Speedgorskicompsci.ca/ICS3U/Unit5/ppt7_Speed.pdf · 2020. 12. 17. · Algorithm •A series of...
![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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/1.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/2.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/3.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/4.jpg)
![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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/5.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/6.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/7.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/8.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/9.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/10.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/11.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/12.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/13.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/14.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/15.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/16.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/17.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/18.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/19.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/20.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/21.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/22.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/23.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/24.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/25.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/26.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/27.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/28.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/29.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/30.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/31.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/32.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/33.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/34.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/35.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/36.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/37.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/38.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/39.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/40.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/41.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/42.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/43.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/44.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/45.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/46.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/47.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/48.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/49.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/50.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/51.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/52.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/53.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/54.jpg)
![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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/55.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/56.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/57.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/58.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/59.jpg)
![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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/60.jpg)
![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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/61.jpg)
![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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/62.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/63.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/64.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/65.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/66.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/67.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/68.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/69.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022071412/6108c09ee226d34a9967b1ae/html5/thumbnails/70.jpg)
The Moral of Bentley’s Example
Fast hardware cannot compensate for a slow
algorithm.