1 of 193

This is CS50 �Week 3

2 of 193

Kelly Ding (she/her)

Preceptor

kelly@cs50.harvard.edu

3 of 193

  • What are structs?
  • How does recursion work?
  • How can we search and sort efficiently?
  • What is Big O notation?

4 of 193

Part 1

Structs

5 of 193

How can we group different types of related data into a single unit?

6 of 193

typedef struct

{

string name;

int votes;

}

candidate;

7 of 193

typedef struct

{

string name;

int votes;

}

candidate;

8 of 193

typedef struct

{

string name;

int votes;

}

candidate;

9 of 193

typedef struct

{

string name;

int votes;

}

candidate;

10 of 193

candidate president;

11 of 193

type

candidate president;

12 of 193

candidate president;

name

13 of 193

candidate president;

president.name = "Alice";

president.votes = 10;

14 of 193

Questions?

15 of 193

Struct Exercise

Create a struct to represent a candidate in an election that minimally includes:

  • The candidate's name (as a string)
  • The candidate's number of votes (as an integer)

Add attributes to a candidate and print those out to the user.

$ ./struct

Alice has a 5 votes.

16 of 193

Structs and Functions Exercise

Create your own get_candidate function that prompts the user to input attributes for a candidate. You may rely on get_string, get_float, etc., and your function should return a candidate.

$ ./struct

Name: Alice

Votes: 5

Alice has a 5 votes.

17 of 193

What if we had multiple candidates?

18 of 193

name

votes

candidate candidates[3];

19 of 193

name

votes

candidates[0];

20 of 193

name

Alice

votes

candidates[0].name = "Alice";

21 of 193

name

Alice

votes

2

candidates[0].votes = 2;

22 of 193

Questions?

23 of 193

Arrays of Structs Exercise

Use your get_candidate function to create an array of three candidates, each of which should have attributes input by the user.

$ ./struct

Name: Alice

Votes: 5

Name: Bob

Votes: 10

Name: Charlie

Votes: 8

Candidate 1 is named Alice and has 5 votes.

Candidate 2 is named Bob and has 10 votes.

Candidate 3 is named Charlie and has 8 votes.

24 of 193

Part 2

Recursion

25 of 193

Recursion

Solving a problem by first solving a smaller version of the same problem.

Every recursive function should have:

  • Base case: when to stop running the function.
  • Recursive case: a call to the function to solve a smaller version of the problem.

26 of 193

Factorial Example

Write a recursive function, factorial, that takes an integer n, and prints its factorial.

The factorial of 5, for example, is 5 * 4 * 3 * 2 * 1 = 120.

27 of 193

Factorial

3! = 3 * 2 * 1 * 1

2! = 2 * 1 * 1

1! = 1 * 1

0! = 1

28 of 193

Factorial

3! = 3 * 2!

2! = 2 * 1!

1! = 1 * 0!

0! = 1

29 of 193

Factorial

f(3)

3 * f(2)

2 * f(1)

1 * f(0)

1

30 of 193

Factorial

f(3)

3 * f(2)

2 * f(1)

1 * f(0)

1

1

31 of 193

Factorial

f(3)

3 * f(2)

2 * f(1)

1 * 1

1

32 of 193

Factorial

f(3)

3 * f(2)

2 * 1

2

33 of 193

Factorial

f(3)

3 * 2

6

34 of 193

Factorial

6

35 of 193

Factorial

What’s the base case?

What’s the recursive case?

36 of 193

Questions?

37 of 193

Fibonacci Exercise

Write a recursive function, fib, that takes an integer n, and prints the nth Fibonacci number.

The Fibonacci sequence is a sequence in which each element is the sum of the two elements that precede it:

(0, 1, 1, 2, 3, 5, 8, 13, 21,... )

38 of 193

Part 3

Searching & Sorting

39 of 193

How would you search for 6?

?

?

?

?

?

?

?

?

40 of 193

?

?

?

?

?

1

?

?

41 of 193

?

?

?

?

1

8

?

?

42 of 193

?

?

?

?

1

8

3

?

43 of 193

?

?

?

?

1

8

3

6

44 of 193

Runtime analysis

45 of 193

?

?

?

?

?

?

?

?

How many checks are needed to search for 6 in this list?

46 of 193

?

?

?

?

?

?

?

?

What about this list?

?

?

?

?

?

?

?

?

?

?

?

?

47 of 193

O(n) — "upper bound" definition

I need to do as many as n steps for an input of size n.

48 of 193

O(n) — "scaling" definition

For every new item that gets added to my algorithm's input, the algorithm needs to do a fixed number of new steps. We say "our runtime scales linearly with the size of our input".

49 of 193

Questions?

50 of 193

Sorting

51 of 193

Selection Sort

52 of 193

5

3

4

8

2

1

7

6

min

53 of 193

5

3

4

8

2

1

7

6

5

min

54 of 193

5

3

4

8

2

1

7

6

3

min

55 of 193

5

3

4

8

2

1

7

6

2

min

56 of 193

5

3

4

8

2

1

7

6

1

min

57 of 193

1

3

4

8

2

5

7

6

min

58 of 193

1

3

4

8

2

5

7

6

3

min

59 of 193

1

3

4

8

2

5

7

6

2

min

60 of 193

1

2

4

8

3

5

7

6

min

61 of 193

1

2

4

8

3

5

7

6

4

min

62 of 193

1

2

4

8

3

5

7

6

3

min

63 of 193

1

2

3

8

4

5

7

6

min

64 of 193

1

2

3

8

4

5

7

6

8

min

65 of 193

1

2

3

8

4

5

7

6

4

min

66 of 193

1

2

3

4

8

5

7

6

min

67 of 193

1

2

3

4

8

5

7

6

8

min

68 of 193

1

2

3

4

8

5

7

6

5

min

69 of 193

1

2

3

4

5

8

7

6

min

70 of 193

1

2

3

4

5

8

7

6

8

min

71 of 193

1

2

3

4

5

8

7

6

7

min

72 of 193

1

2

3

4

5

8

7

6

6

min

73 of 193

1

2

3

4

5

6

7

8

min

74 of 193

1

2

3

4

5

6

7

8

7

min

75 of 193

1

2

3

4

5

6

7

8

8

min

76 of 193

1

2

3

4

5

6

7

8

77 of 193

Repeat for every element in our list, except last:

Assume the element at this position is the smallest

For every element to the right:

If less than our current smallest:

Update the current smallest

Swap the current smallest with the current element

78 of 193

For i from 0 to n - 2

Set min = i

For j from i + 1 to n - 1

If array[j] < array[min]

Update min = j

Swap array[i] with array[min]

79 of 193

O(n2) — "upper bound" definition

I need to do as many as n2 steps if my input size is n.

80 of 193

Ω(n2) — "lower bound" definition

In the best case, I need to do approximately n2 steps if my input size is n.

81 of 193

Bubble Sort

82 of 193

5

3

4

8

2

1

7

6

Are these out of order?

83 of 193

3

5

4

8

2

1

7

6

84 of 193

3

5

4

8

2

1

7

6

85 of 193

3

4

5

8

2

1

7

6

86 of 193

3

4

5

8

2

1

7

6

87 of 193

3

4

5

8

2

1

7

6

88 of 193

3

4

5

2

8

1

7

6

89 of 193

3

4

5

2

8

1

7

6

90 of 193

3

4

5

2

1

8

7

6

91 of 193

3

4

5

2

1

8

7

6

92 of 193

3

4

5

2

1

7

8

6

93 of 193

3

4

5

2

1

7

8

6

94 of 193

3

4

5

2

1

7

6

8

95 of 193

3

4

5

2

1

7

6

8

First pass complete!

96 of 193

3

4

5

2

1

7

6

8

97 of 193

3

4

2

5

1

7

6

8

98 of 193

3

4

2

1

5

7

6

8

99 of 193

3

4

2

1

5

6

7

8

100 of 193

3

4

2

1

5

6

7

8

101 of 193

3

2

4

1

5

6

7

8

102 of 193

3

2

1

4

5

6

7

8

103 of 193

3

2

1

4

5

6

7

8

104 of 193

2

3

1

4

5

6

7

8

105 of 193

2

1

3

4

5

6

7

8

106 of 193

2

1

3

4

5

6

7

8

107 of 193

1

2

3

4

5

6

7

8

108 of 193

1

2

3

4

5

6

7

8

109 of 193

1

2

3

4

5

6

7

8

110 of 193

Repeat for every element in our list, except last:

Look at each element from first to second-to-last:

If current and next elements out of order:

Swap them

If no two elements were swapped:

Quit

111 of 193

Repeat n - 1 times

Set swaps to none

For j from 0 to n - 2

If j'th and j + 1'th elements out of order

Swap them

If swaps = none

Quit

112 of 193

O(n2) — "upper bound" definition

I need to do as many as n2 steps if my input size is n.

113 of 193

O(n2) — "scaling" definition

For every new item that gets added to my input, I need to do approximately n new steps.

114 of 193

Ω(n) — "lower bound" definition

In the best case, I need to do approximately n steps if my input size is n.

115 of 193

Merge Sort

116 of 193

5

3

4

8

2

1

7

6

117 of 193

5

3

4

8

2

1

7

6

118 of 193

5

3

4

8

2

1

7

6

119 of 193

2

1

7

6

5

3

4

8

120 of 193

2

1

7

6

5

3

4

8

121 of 193

2

1

7

6

4

8

5

3

122 of 193

2

1

7

6

4

8

5

3

123 of 193

2

1

7

6

4

8

5

3

124 of 193

2

1

7

6

4

8

5

3

125 of 193

2

1

7

6

4

8

5

3

126 of 193

2

1

7

6

4

8

5

3

127 of 193

2

1

7

6

4

8

5

3

128 of 193

2

1

7

6

4

8

5

3

129 of 193

2

1

7

6

4

8

5

3

130 of 193

2

1

7

6

4

8

5

3

131 of 193

2

1

7

6

5

3

4

8

132 of 193

2

1

7

6

5

3

4

8

133 of 193

2

1

7

6

5

3

4

8

134 of 193

2

1

7

6

5

3

4

8

135 of 193

2

1

7

6

5

3

4

8

136 of 193

2

1

7

6

5

3

4

8

137 of 193

2

1

7

6

5

3

8

4

138 of 193

2

1

7

6

5

3

4

8

139 of 193

2

1

7

6

5

3

4

8

140 of 193

2

1

7

6

5

3

4

8

141 of 193

2

1

7

6

5

3

4

8

142 of 193

2

1

7

6

5

3

4

8

143 of 193

2

1

7

6

3

5

8

4

144 of 193

2

1

7

6

5

3

4

8

145 of 193

2

1

7

6

3

4

8

5

146 of 193

2

1

7

6

3

4

8

5

147 of 193

2

1

7

6

5

3

4

8

148 of 193

2

1

7

6

5

3

4

8

149 of 193

5

3

4

8

2

1

7

6

150 of 193

5

3

4

8

2

1

7

6

151 of 193

5

3

4

8

7

6

2

1

152 of 193

5

3

4

8

7

6

2

1

153 of 193

5

3

4

8

7

6

2

1

154 of 193

5

3

4

8

7

6

2

1

155 of 193

5

3

4

8

7

6

2

1

156 of 193

5

3

4

8

7

6

2

1

157 of 193

5

3

4

8

7

6

2

1

158 of 193

5

3

4

8

7

6

2

1

159 of 193

5

3

4

8

7

6

2

1

160 of 193

5

3

4

8

7

6

2

1

161 of 193

5

3

4

8

2

1

7

6

162 of 193

5

3

4

8

2

1

7

6

163 of 193

5

3

4

8

2

1

7

6

164 of 193

5

3

4

8

2

1

7

6

165 of 193

5

3

4

8

2

1

7

6

166 of 193

5

3

4

8

2

1

7

6

167 of 193

5

3

4

8

2

1

7

6

168 of 193

5

3

4

8

2

1

6

7

169 of 193

5

3

4

8

2

1

6

7

170 of 193

5

3

4

8

2

1

7

6

171 of 193

5

3

4

8

2

1

7

6

172 of 193

5

3

4

8

2

1

7

6

173 of 193

5

3

4

8

2

1

7

6

174 of 193

5

3

4

8

2

1

7

6

175 of 193

5

3

4

8

2

1

7

6

176 of 193

5

3

4

8

2

1

7

6

177 of 193

5

3

4

8

2

1

7

6

178 of 193

5

3

4

8

2

1

7

6

179 of 193

5

3

4

8

2

1

7

6

180 of 193

5

3

4

8

2

1

7

6

181 of 193

5

3

4

8

2

1

7

6

182 of 193

5

3

4

8

2

1

7

6

183 of 193

3

4

8

2

1

7

6

5

184 of 193

3

4

8

2

1

7

6

5

185 of 193

5

3

4

8

2

1

7

6

186 of 193

5

3

4

2

1

7

6

8

187 of 193

5

3

4

2

1

7

6

8

188 of 193

5

3

4

8

2

1

7

6

189 of 193

5

3

4

8

2

1

7

6

190 of 193

Split the list into two halves

Sort the left half using merge sort

Sort the right half using merge sort

Merge the two halves into a single sorted list:

Take the smaller first element from either half

191 of 193

O(n log n) — "upper bound" definition

I need to do as many as n log n steps to find my solution.

192 of 193

Runtime Analysis

Algorithm

O

Ω

Merge Sort

O(n log n)

Ω(n log n)

Selection Sort

O(n²)

Ω(n²)

Bubble Sort

O(n²)

Ω(n)

193 of 193

This is CS50 �Week 3