1 of 107

Pick-Up Midterms!

Corrections due Friday 3/18!

2 of 107

+(0.5 x points) for corrections

Due Friday 3/18 in-class!

Use a pen for corrections - bring back to lecture Friday!

3 of 107

4 of 107

1. Welcome to COMP 285

Lecture 21: Midterm Overview

COMP - 285

Advanced Algorithms

Lecturer: Luis Perez (laperez@ncat.edu)

5 of 107

Today (Welcome back!)

  • Pick-up your graded midterm!
  • Review grade distribution
  • Review each question

5

6 of 107

Overall Distribution (Points)

A

> 74

A-

> 64

B+

> 54

B

> 41

B-

> 31

F, D, D+, C-,C,C+

7 of 107

Median: 40

8 of 107

Section 1 - Median: 5

9 of 107

10 of 107

f(n)

N = 1,000,000

n log n

~6,000,000

n2

~1,000,000,000,000

√n

~1,000

2022

2022

~1,000,007,003,022

11 of 107

<=

Big-O

=

Big-Theta

>=

Big-Omega

False

12 of 107

<=

Big-O

=

Big-Theta

>=

Big-Omega

13 of 107

Section 2

14 of 107

O(n)

O(n)

O(1)

Time Complexity: O(n2)

Space Complexity: O(1)

O(n)

O(1)

Space: O(n)

Time Complexity: O(n)

Space Complexity: O(n)

15 of 107

Section 3

16 of 107

17 of 107

1

2

5

10

14

An example

3

18 of 107

1

2

5

10

14

Brute Force

O(n)

19 of 107

Divide & Conquer

0

1

2

3

5

8

10

0

1

2

3

4

5

6

A[0] + n/2

A[n/2]

Case 1

A[n/2] = A[0] + n/2

20 of 107

Divide & Conquer

0

1

3

4

5

8

10

0

1

2

3

4

5

6

Case 2

A[n/2] > A[0] + n/2

21 of 107

Divide & Conquer

-1

0

1

2

3

4

5

0

1

2

3

4

5

6

Case 3

A[n/2] < A[0] + n/2

22 of 107

Divide & Conquer

Case 1

Case 2

23 of 107

Walk Through Examples

1

2

5

10

14

1

2

2

3

24 of 107

0

1

2

3

5

8

10

Walk Through Examples

4

8

10

3

5

3

5

3

25 of 107

0

1

3

4

5

8

10

Walk Through Examples

2

3

0

1

1

3

1

26 of 107

How Fast is It?

27 of 107

Section 4

28 of 107

Algorithm

Worst-Case Running Time

Comparison-Based

QuickSort

O(n log n)

Yes

MergeSort

O(n log n)

Yes

RadixSort

O(d(n + r))

No

Counting Sort

O(n + r)

No

Selection Sort

O(n2)

Yes

Insertion Sort

O(n2)

Yes

False

29 of 107

True

30 of 107

31 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 0

end = 1

sum = -10

32 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 0

end = 2

sum = -6

33 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 0

end = 3

sum = 5

34 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 0

end = 4

sum = -4

35 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 0

end = 5

sum = -3

36 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 1

end = 2

sum = 4

37 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 1

end = 3

sum = 15

38 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 1

end = 4

sum = 6

39 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 1

end = 5

sum = 7

40 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 2

end = 3

sum = 11

41 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 2

end = 4

sum = 2

42 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 2

end = 5

sum = 3

43 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 3

end = 4

sum = -9

44 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 3

end = 5

sum = -8

45 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 4

end = 5

sum = 1

46 of 107

Contiguous Subarray Sums

-10

4

11

-9

1

start = 5

end = 5

sum = 0

47 of 107

-10

4

11

-9

1

start = 5

end = 5

sum = 0

48 of 107

Brute Force

49 of 107

Brute Force - Pseudocode

O(n3)

O(n2)

50 of 107

-4

10

2

-20

5

6

-4

-4

10

2

-20

5

6

4

-4

10

2

-1

5

8

-4

51 of 107

-4

10

2

-20

5

6

-4

-4

10

2

-20

5

6

-4

-4

10

2

-20

5

6

-4

-4

10

2

-20

5

6

-4

52 of 107

-4

10

2

-20

5

6

-4

0

10

2

-20

5

6

0

-4

10

2

-20

5

6

-4

10

2

5

6

-4

10

2

-20

5

6

-4

12

11

-4

10

2

-20

5

6

-4

12

53 of 107

12

11

-4

10

2

-20

5

6

-4

-20

-20

54 of 107

12

11

-4

10

2

-20

5

6

-4

-18

-18

55 of 107

12

11

-4

10

2

-20

5

6

-4

-8

-8

56 of 107

12

11

-4

10

2

-20

5

6

-4

-8

-12

57 of 107

12

11

-4

10

2

-20

5

6

-4

-8

5

5

58 of 107

12

11

-4

10

2

-20

5

6

-4

-8

11

11

59 of 107

12

11

-4

10

2

-20

5

6

-4

-8

7

11

60 of 107

12

11

-4

10

2

-20

5

6

-4

-8

11

+

3

=

61 of 107

T(n) = 2T(n/2) + O(n)

O(n log n)

62 of 107

Section 5

63 of 107

64 of 107

65 of 107

66 of 107

67 of 107

68 of 107

Section 6

69 of 107

70 of 107

71 of 107

72 of 107

73 of 107

74 of 107

19

75 of 107

1

5

76 of 107

1

-3

6

8

33

77 of 107

1

-3

26

78 of 107

-3

79 of 107

80 of 107

81 of 107

82 of 107

7

83 of 107

Find Height

O(n)

84 of 107

Section 7

85 of 107

86 of 107

+

= 22

17

87 of 107

+

= 22

13

+

88 of 107

+

= 22

2

+

+

89 of 107

90 of 107

= 22?

=17?

= 17?

91 of 107

= 22?

=17?

= 17?

=11?

=11?

= 9?

= 9?

92 of 107

= 22?

=17?

= 17?

=11?

No!

= 9?

= 9?

=5?

=5?

=-4?

=-4?

=4?

=2?

93 of 107

= 22?

=17?

= 17?

=11?

No!

= 9?

= 9?

No!

=5?

No!

No!

=4?

=2?

=4?

=4?

=0?

=0?

=3?

=3?

94 of 107

= 22?

=17?

= 17?

=11?

No!

= 9?

= 9?

No!

=5?

No!

No!

=4?

=2?

No!

No!

Yes!

Yes!

No!

No!

95 of 107

= 22?

=17?

= 17?

=11?

No!

No!

= 9?

No!

No!

No!

Yes!

96 of 107

= 22?

=17?

= 17?

No!

No!

No!

Yes!

97 of 107

= 22?

No!

Yes!

98 of 107

Yes!

99 of 107

O(n) Runtime

100 of 107

Section 8

101 of 107

Node 0

Node 1

Node 2

Node 3

Node 0

Node 1

Node 2

Node 3

1

1

1

1

0

0

0

0

0

0

0

0

0

0

0

0

102 of 107

103 of 107

104 of 107

105 of 107

106 of 107

Midterm Corrections!

Due Friday 3/18!

107 of 107

How was the pace?

107