1 of 224

2 of 224

This is CS50

  • If you didn't get a rubber duck 🦆 last week, ask Yuliia during break or after class!
  • Today's snacks, Hello Panda Chocolate Biscuits. 🐼 🍫
  • Do say hi or ask questions during break or after class!
    • What do today's binary bulbs spell (once sorted)?

3 of 224

4 of 224

5 of 224

6 of 224

n

7 of 224

n/2

n

8 of 224

n/2

n

log2 n

9 of 224

  1. Stand up and think of the number 1.

10 of 224

  • Stand up and think of the number 1.
  • Pair off with someone standing, add their number to yours, and remember the sum.

11 of 224

  • Stand up and think of the number 1.
  • Pair off with someone standing, add their number to yours, and remember the sum.
  • One of you should then sit down.

12 of 224

  • Stand up and think of the number 1.
  • Pair off with someone standing, add their number to yours, and remember the sum.
  • One of you should then sit down.
  • If still standing, go back to step 2.

13 of 224

14 of 224

15 of 224

16 of 224

n

17 of 224

n/2

n

18 of 224

n/2

n

log2 n

19 of 224

20 of 224

21 of 224

22 of 224

23 of 224

1

5

10

20

50

100

500

24 of 224

25 of 224

[0]

[1]

[2]

[3]

[4]

[5]

[6]

26 of 224

[0]

[1]

[2]

[3]

[4]

[5]

[6]

27 of 224

searching

28 of 224

input → 

→ output

29 of 224

→ 

→ output

30 of 224

bool

→ 

31 of 224

algorithm

32 of 224

33 of 224

34 of 224

linear search

35 of 224

For each door from left to right

If 50 is behind door

Return true

Return false�

36 of 224

For each door from left to right

If 50 is behind door

Return true

Else

Return false

37 of 224

For each door from left to right

If 50 is behind door

Return true

Return false�

38 of 224

For i from 0 to n-1

If 50 is behind doors[i]

Return true

Return false�

39 of 224

40 of 224

binary search

41 of 224

If 50 is behind middle door

Return true

Else if 50 < middle door

Search left half

Else if 50 > middle door

Search right half

42 of 224

If no doors left

If 50 is behind middle door

Return true

Else if 50 < middle door

Search left half

Else if 50 > middle door

Search right half

43 of 224

If no doors left

Return false

If 50 is behind middle door

Return true

Else if 50 < middle door

Search left half

Else if 50 > middle door

Search right half

44 of 224

If no doors left

Return false

If 50 is behind doors[middle]

Return true

Else if 50 < doors[middle]

Search doors[0] through doors[middle - 1]

Else if 50 > doors[middle]

Search doors[middle + 1] through doors[n - 1]

45 of 224

running time

46 of 224

size of problem

time to solve

47 of 224

size of problem

time to solve

O(n)

O(n/2)

O(log2n)

48 of 224

size of problem

time to solve

O(n)

O(n/2)

O(log2n)

49 of 224

size of problem

time to solve

O(n)

O(n)

O(log2n)

50 of 224

size of problem

time to solve

O(n)

O(n)

O(log n)

51 of 224

size of problem

time to solve

O(n)

O(log n)

52 of 224

O

53 of 224

O(n2)

O(n log n)

O(n)

O(log n)

O(1)

54 of 224

O(n2)

O(n log n)

O(n) linear search

O(log n)

O(1)

55 of 224

O(n2)

O(n log n)

O(n) linear search

O(log n) binary search

O(1)

56 of 224

Ω

57 of 224

Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)

58 of 224

Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1) linear search

59 of 224

Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1) linear search, binary search

60 of 224

Θ

61 of 224

Θ(n2)

Θ(n log n)

Θ(n)

Θ(log n)

Θ(1)

62 of 224

asymptotic notation

63 of 224

linear search

64 of 224

string.h

65 of 224

66 of 224

strcmp

67 of 224

data structures

68 of 224

person people[]

69 of 224

string name;

string number;

70 of 224

typedef struct

{

string name;

string number;

}

person;

71 of 224

typedef struct

{

string name;

string number;

} person;

72 of 224

sorting

73 of 224

input → 

→ output

74 of 224

unsorted → 

→ output

75 of 224

unsorted → 

→ sorted

76 of 224

→ 

→ sorted

7 2 5 4 1 6 0 3

77 of 224

→ 

7 2 5 4 1 6 0 3

0 1 2 3 4 5 6 7

78 of 224

7 2 5 4 1 6 0 3

79 of 224

80 of 224

selection sort

81 of 224

[0]

[1]

[2]

...

[n-3]

[n-2]

[n-1]

82 of 224

For i from 0 to n-1

Find smallest number between numbers[i] and numbers[n-1]

Swap smallest number with numbers[i]

83 of 224

7 2 5 4 1 6 0 3

84 of 224

7 2 5 4 1 6 0 3

[i]

85 of 224

7 2 5 4 1 6 0 3

[i]

[i]

[n-1]

86 of 224

7 2 5 4 1 6 0 3

[i]

[i]

[n-1]

87 of 224

7 2 5 4 1 6 0 3

[i]

[i]

[n-1]

88 of 224

7 2 5 4 1 6 0 3

[i]

[i]

[n-1]

89 of 224

7 2 5 4 1 6 0 3

[i]

[i]

[n-1]

90 of 224

7 2 5 4 1 6 0 3

[i]

[i]

[n-1]

91 of 224

0 2 5 4 1 6 7 3

[i]

[i]

[n-1]

92 of 224

0 2 5 4 1 6 7 3

[i]

[i]

[n-1]

93 of 224

0 2 5 4 1 6 7 3

[i]

[i]

[n-1]

94 of 224

0 2 5 4 1 6 7 3

[i]

[n-1]

95 of 224

(n – 1)

96 of 224

(n – 1) + (n – 2)

97 of 224

(n – 1) + (n – 2) + (n – 3)

98 of 224

(n – 1) + (n – 2) + (n – 3) + ... + 1

99 of 224

(n – 1) + (n – 2) + (n – 3) + ... + 1

n(n – 1)/2

100 of 224

(n – 1) + (n – 2) + (n – 3) + ... + 1

n(n – 1)/2

(n2 – n)/2

101 of 224

(n – 1) + (n – 2) + (n – 3) + ... + 1

n(n – 1)/2

(n2 – n)/2

n2/2 – n/2

102 of 224

(n – 1) + (n – 2) + (n – 3) + ... + 1

n(n – 1)/2

(n2 – n)/2

n2/2 – n/2

O(n2)

103 of 224

O(n2)

O(n log n)

O(n)

O(log n)

O(1)

104 of 224

O(n2) selection sort

O(n log n)

O(n)

O(log n)

O(1)

105 of 224

For i from 0 to n-1

Find smallest number between numbers[i] and numbers[n-1]

Swap smallest number with numbers[i]

106 of 224

[0]

[1]

[2]

...

[n-3]

[n-2]

[n-1]

107 of 224

Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)

108 of 224

Ω(n2) selection sort

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)

109 of 224

Θ(n2)

Θ(n log n)

Θ(n)

Θ(log n)

Θ(1)

110 of 224

Θ(n2) selection sort

Θ(n log n)

Θ(n)

Θ(log n)

Θ(1)

111 of 224

bubble sort

112 of 224

[0]

[1]

[2]

...

[n-3]

[n-2]

[n-1]

113 of 224

Repeat n times

For i from 0 to n-2

If numbers[i] and numbers[i+1] out of order

Swap them

114 of 224

Repeat n-1 times

For i from 0 to n-2

If numbers[i] and numbers[i+1] out of order

Swap them

115 of 224

7 2 5 4 1 6 0 3

116 of 224

7 2 5 4 1 6 0 3

[i] [i+1]

117 of 224

2 7 5 4 1 6 0 3

[i] [i+1]

118 of 224

2 7 5 4 1 6 0 3

[i] [i+1]

119 of 224

2 5 7 4 1 6 0 3

[i] [i+1]

120 of 224

2 5 7 4 1 6 0 3

[i] [i+1]

121 of 224

2 5 4 7 1 6 0 3

[i] [i+1]

122 of 224

2 5 4 7 1 6 0 3

[i] [i+1]

123 of 224

2 5 4 1 7 6 0 3

[i] [i+1]

124 of 224

2 5 4 1 7 6 0 3

[i] [i+1]

125 of 224

2 5 4 1 6 7 0 3

[i] [i+1]

126 of 224

2 5 4 1 6 7 0 3

[i] [i+1]

127 of 224

2 5 4 1 6 0 7 3

[i] [i+1]

128 of 224

2 5 4 1 6 0 7 3

[i] [i+1]

129 of 224

2 5 4 1 6 0 3 7

[i] [i+1]

130 of 224

2 5 4 1 6 0 3 7

[i] [i+1]

131 of 224

Repeat n-1 times

For i from 0 to n-2

If numbers[i] and numbers[i+1] out of order

Swap them

132 of 224

Repeat n-1 times n-1

For i from 0 to n-2

If numbers[i] and numbers[i+1] out of order

Swap them

133 of 224

Repeat n-1 times n-1

For i from 0 to n-2 n-1

If numbers[i] and numbers[i+1] out of order

Swap them

134 of 224

Repeat n-1 times n-1

For i from 0 to n-2 n-1

If numbers[i] and numbers[i+1] out of order 1

Swap them

135 of 224

Repeat n-1 times n-1

For i from 0 to n-2 n-1

If numbers[i] and numbers[i+1] out of order 4

Swap them

136 of 224

Repeat n-1 times n-1

For i from 0 to n-2 n-1

If numbers[i] and numbers[i+1] out of order

Swap them

137 of 224

(n – 1) × (n – 1)

138 of 224

(n – 1) × (n – 1)

n2 – 1n – 1n + 1

139 of 224

(n – 1) × (n – 1)

n2 – 1n – 1n + 1

n2 – 2n + 1

140 of 224

(n – 1) × (n – 1)

n2 – 1n – 1n + 1

n2 – 2n + 1

O(n2)

141 of 224

O(n2)

O(n log n)

O(n)

O(log n)

O(1)

142 of 224

O(n2) bubble sort

O(n log n)

O(n)

O(log n)

O(1)

143 of 224

Repeat n-1 times

For i from 0 to n-2

If numbers[i] and numbers[i+1] out of order

Swap them

144 of 224

Repeat n-1 times

For i from 0 to n-2

If numbers[i] and numbers[i+1] out of order

Swap them

If no swaps

Quit

145 of 224

Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)

146 of 224

Ω(n2)

Ω(n log n)

Ω(n) bubble sort

Ω(log n)

Ω(1)

147 of 224

recursion

148 of 224

If no doors left

Return false

If number behind middle door

Return true

Else if number < middle door

Search left half

Else if number > middle door

Search right half

149 of 224

If no doors left

Return false

If number behind middle door

Return true

Else if number < middle door

Search left half

Else if number > middle door

Search right half

150 of 224

1 Pick up phone book

2 Open to middle of phone book

3 Look at page

4 If person is on page

5 Call person

6 Else if person is earlier in book

7 Open to middle of left half of book

8 Go back to line 3

9 Else if person is later in book

10 Open to middle of right half of book

11 Go back to line 3

12 Else

13 Quit

151 of 224

1 Pick up phone book

2 Open to middle of phone book

3 Look at page

4 If person is on page

5 Call person

6 Else if person is earlier in book

7 Open to middle of left half of book

8 Go back to line 3

9 Else if person is later in book

10 Open to middle of right half of book

11 Go back to line 3

12 Else

13 Quit

152 of 224

1 Pick up phone book

2 Open to middle of phone book

3 Look at page

4 If person is on page

5 Call person

6 Else if person is earlier in book

7 Open to middle of left half of book

8 Go back to line 3

9 Else if person is later in book

10 Open to middle of right half of book

11 Go back to line 3

12 Else

13 Quit

153 of 224

1 Pick up phone book

2 Open to middle of phone book

3 Look at page

4 If person is on page

5 Call person

6 Else if person is earlier in book

7 Search left half of book

8

9 Else if person is later in book

10 Search right half of book

11

12 Else

13 Quit

154 of 224

1 Pick up phone book

2 Open to middle of phone book

3 Look at page

4 If person is on page

5 Call person

6 Else if person is earlier in book

7 Search left half of book

8 Else if person is later in book

9 Search right half of book

10 Else

11 Quit

155 of 224

156 of 224

157 of 224

158 of 224

159 of 224

160 of 224

161 of 224

162 of 224

merge sort

163 of 224

Sort left half of numbers

Sort right half of numbers

Merge sorted halves

164 of 224

If only one number

Quit

Else

Sort left half of numbers

Sort right half of numbers

Merge sorted halves

165 of 224

If only one number

Quit

Else

Sort left half of numbers

Sort right half of numbers

Merge sorted halves

166 of 224

1 3 4 6 0 2 5 7

167 of 224

If only one number

Quit

Else

Sort left half of numbers

Sort right half of numbers

Merge sorted halves

168 of 224

6 3 4 1 5 2 7 0

169 of 224

6

3

4

1

5

2

7

0

170 of 224

6

3

4

1

5

2

7

0

171 of 224

5

2

7

0

6

3

4

1

172 of 224

5

2

7

0

4

1

6

3

173 of 224

5

2

7

0

4

1

3

6

174 of 224

5

2

7

0

4

1

6

3

175 of 224

5

2

7

0

4

1

3

6

176 of 224

5

2

7

0

4

1

3

6

177 of 224

5

2

7

0

3

6

4

1

178 of 224

5

2

7

0

3

6

1

4

179 of 224

5

2

7

0

3

6

4

1

180 of 224

5

2

7

0

3

6

1

4

181 of 224

5

2

7

0

3

6

1

4

182 of 224

5

2

7

0

1

3

6

4

183 of 224

5

2

7

0

1

3

6

4

184 of 224

5

2

7

0

1

3

4

6

185 of 224

5

2

7

0

1

3

4

6

186 of 224

1

3

4

6

5

2

7

0

187 of 224

1

3

4

6

7

0

5

2

188 of 224

1

3

4

6

7

0

2

5

189 of 224

1

3

4

6

7

0

5

2

190 of 224

1

3

4

6

7

0

2

5

191 of 224

1

3

4

6

7

0

2

5

192 of 224

1

3

4

6

2

5

7

0

193 of 224

1

3

4

6

2

5

0

7

194 of 224

1

3

4

6

2

5

7

0

195 of 224

1

3

4

6

2

5

0

7

196 of 224

1

3

4

6

2

5

0

7

197 of 224

1

3

4

6

0

2

5

7

198 of 224

1

3

4

6

0

2

5

7

199 of 224

1

3

4

6

0

2

5

7

200 of 224

1

3

4

6

0

2

5

7

201 of 224

0

1

3

4

6

2

5

7

202 of 224

0

1

3

4

6

2

5

7

203 of 224

0

1

2

3

4

6

5

7

204 of 224

0

1

2

3

4

6

5

7

205 of 224

0

1

2

3

4

6

5

7

206 of 224

0

1

2

3

4

5

6

7

207 of 224

0

1

2

3

4

5

6

7

208 of 224

0

1

2

3

4

5

6

7

209 of 224

0

1

2

3

4

5

6

7

210 of 224

O(n2)

O(n log n)

O(n)

O(log n)

O(1)

211 of 224

6

3

4

1

5

2

7

0

6

3

4

1

6

3

6

3

4

1

4

1

5

2

7

0

5

2

5

2

7

0

7

0

212 of 224

6

3

4

1

5

2

7

0

6

3

4

1

6

3

6

3

4

1

4

1

5

2

7

0

5

2

5

2

7

0

7

0

213 of 224

log2 n

214 of 224

log2 8

215 of 224

log2 23

216 of 224

3

217 of 224

6

3

4

1

5

2

7

0

6

3

4

1

6

3

6

3

4

1

4

1

5

2

7

0

5

2

5

2

7

0

7

0

218 of 224

n log2 n

219 of 224

n log n

220 of 224

O(n2)

O(n log n) merge sort

O(n)

O(log n)

O(1)

221 of 224

Ω(n2)

Ω(n log n) merge sort

Ω(n)

Ω(log n)

Ω(1)

222 of 224

Θ(n2)

Θ(n log n) merge sort

Θ(n)

Θ(log n)

Θ(1)

223 of 224

224 of 224