1 of 211

CS50 for JDs

Algorithms, Data Structures

2 of 211

abstraction

3 of 211

4 of 211

5 of 211

6 of 211

7 of 211

seminars

8 of 211

Innovative Algorithms and Their Applications

Fri, Jan 6, 9:00 AM – 11:00 AM EST in WCC Room 1010, �with Catherine Deskur

9 of 211

shorts

10 of 211

input → 

→ output

11 of 211

algorithms

12 of 211

13 of 211

14 of 211

15 of 211

16 of 211

17 of 211

18 of 211

linear search

19 of 211

For each door from left to right

If number is behind door

Answer is true

Answer is false

20 of 211

For each door from left to right

If number is behind door

Answer is true

Else

Answer is false

21 of 211

binary search

22 of 211

If number behind middle door

Return true

Else if number < middle door

Search left half

Else if number > middle door

Search right half

23 of 211

If no doors

If number behind middle door

Return true

Else if number < middle door

Search left half

Else if number > middle door

Search right half

24 of 211

If no doors

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

25 of 211

size of problem

time to solve

26 of 211

size of problem

time to solve

O(n)

O(n/2)

O(log2n)

27 of 211

size of problem

time to solve

O(n)

O(n/2)

O(log2n)

28 of 211

size of problem

time to solve

O(n)

O(n)

O(log n)

29 of 211

O(n2)

O(n log n)

O(n)

O(log n)

O(1)

30 of 211

O(n2)

O(n log n)

O(n) linear search

O(log n) binary search

O(1)

31 of 211

Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)

32 of 211

Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1) linear search, binary search

33 of 211

input → 

→ output

34 of 211

unsorted → 

→ output

35 of 211

unsorted → 

→ sorted

36 of 211

6 3 8 5 2 7 4 1

37 of 211

selection sort

38 of 211

For i from 0 to n-1

Find smallest item between i'th item and last item

Swap smallest item with i'th item

39 of 211

40 of 211

(n – 1)

41 of 211

(n – 1) + (n – 2)

42 of 211

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

43 of 211

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

44 of 211

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

n(n – 1)/2

45 of 211

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

n(n – 1)/2

(n2 – n)/2

46 of 211

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

n(n – 1)/2

(n2 – n)/2

n2/2 – n/2

47 of 211

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

n(n – 1)/2

(n2 – n)/2

n2/2 – n/2

O(n2)

48 of 211

O(n2) selection sort

O(n log n)

O(n)

O(log n)

O(1)

49 of 211

Ω(n2) selection sort

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)

50 of 211

6 3 8 5 2 7 4 1

51 of 211

bubble sort

52 of 211

Repeat n-1 times

For i from 0 to n-2

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

Swap them

53 of 211

54 of 211

(n – 1) × (n – 1)

55 of 211

(n – 1) × (n – 1)

n2 – 1n – 1n + 1

56 of 211

(n – 1) × (n – 1)

n2 – 1n – 1n + 1

n2 – 2n + 1

57 of 211

(n – 1) × (n – 1)

n2 – 1n – 1n + 1

n2 – 2n + 1

O(n2)

58 of 211

O(n2) bubble sort

O(n log n)

O(n)

O(log n)

O(1)

59 of 211

Ω(n2) bubble sort

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)

60 of 211

bubble sort

61 of 211

Repeat n-1 times� For i from 0 to n-2� If i'th and i+1'th elements out of order� Swap them

62 of 211

Repeat until no swaps� For i from 0 to n-2� If i'th and i+1'th elements out of order� Swap them

63 of 211

O(n2) bubble sort

O(n log n)

O(n)

O(log n)

O(1)

64 of 211

Ω(n2)

Ω(n log n)

Ω(n) bubble sort

Ω(log n)

Ω(1)

65 of 211

66 of 211

recursion

67 of 211

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

68 of 211

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

69 of 211

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

70 of 211

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

71 of 211

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

72 of 211

73 of 211

74 of 211

75 of 211

76 of 211

77 of 211

78 of 211

merge sort

79 of 211

If only one item

Return

Else

Sort left half of items

Sort right half of items

Merge sorted halves

80 of 211

If only one item

Return

Else

Sort left half of items

Sort right half of items

Merge sorted halves

81 of 211

7

4

5

2

6

3

8

1

82 of 211

7

4

5

2

6

3

8

1

83 of 211

7

4

5

2

6

3

8

1

84 of 211

7

4

5

2

6

3

8

1

85 of 211

7

4

5

2

6

3

8

1

86 of 211

7

4

5

2

6

3

8

1

87 of 211

7

5

2

6

3

8

1

4

88 of 211

5

2

6

3

8

1

4

7

89 of 211

5

2

6

3

8

1

4

7

90 of 211

5

2

6

3

8

1

4

7

91 of 211

5

2

6

3

8

1

4

7

92 of 211

5

2

6

3

8

1

4

7

93 of 211

5

6

3

8

1

4

7

2

94 of 211

6

3

8

1

4

7

2

5

95 of 211

6

3

8

1

4

7

2

5

96 of 211

6

3

8

1

4

7

5

2

97 of 211

6

3

8

1

7

5

2

4

98 of 211

6

3

8

1

7

2

4

5

99 of 211

6

3

8

1

2

4

5

7

100 of 211

6

3

8

1

2

4

5

7

101 of 211

6

3

8

1

2

4

5

7

102 of 211

6

3

8

1

2

4

5

7

103 of 211

6

3

8

1

2

4

5

7

104 of 211

6

3

8

1

2

4

5

7

105 of 211

6

8

1

3

2

4

5

7

106 of 211

8

1

3

6

2

4

5

7

107 of 211

8

1

3

6

2

4

5

7

108 of 211

8

1

3

6

2

4

5

7

109 of 211

8

1

3

6

2

4

5

7

110 of 211

8

1

3

6

2

4

5

7

111 of 211

8

1

3

6

1

2

4

5

7

112 of 211

1

3

6

1

8

2

4

5

7

113 of 211

1

3

6

1

8

2

4

5

7

114 of 211

1

3

6

8

2

4

5

7

1

115 of 211

1

6

8

2

4

5

7

1

3

116 of 211

1

8

2

4

5

7

1

3

6

117 of 211

1

8

2

4

5

7

1

3

6

8

118 of 211

1

8

2

4

5

7

1

3

6

8

119 of 211

1

8

2

4

5

7

3

6

8

1

120 of 211

1

8

4

5

7

3

6

8

1

2

121 of 211

1

8

4

5

7

6

8

1

2

3

122 of 211

1

8

5

7

6

8

1

2

3

4

123 of 211

1

8

7

6

8

1

2

3

4

5

124 of 211

1

8

7

8

1

2

3

4

5

6

125 of 211

1

8

8

1

2

3

4

5

6

7

126 of 211

1

8

1

2

3

4

5

6

7

8

127 of 211

128 of 211

1

7

4

5

2

6

3

8

1

129 of 211

1

8

7

4

5

2

6

3

8

1

4

7

2

5

3

6

1

8

130 of 211

1

8

7

4

5

2

6

3

8

1

4

7

2

5

3

6

1

8

2

4

5

7

1

3

6

8

131 of 211

1

8

1

2

3

4

5

6

7

8

7

4

5

2

6

3

8

1

4

7

2

5

3

6

1

8

2

4

5

7

1

3

6

8

132 of 211

O(n2)

O(n log n) merge sort

O(n)

O(log n)

O(1)

133 of 211

Ω(n2)

Ω(n log n) merge sort

Ω(n)

Ω(log n)

Ω(1)

134 of 211

135 of 211

136 of 211

data structures

137 of 211

bool Boolean value

float floating-point value

int integer

str string

...

138 of 211

dict

list

range

set

tuple

...

139 of 211

140 of 211

141 of 211

142 of 211

143 of 211

144 of 211

145 of 211

arrays

146 of 211

147 of 211

148 of 211

149 of 211

150 of 211

151 of 211

1

2

3

152 of 211

1

2

3

153 of 211

1

2

3

154 of 211

?

?

?

?

?

?

?

?

?

1

2

3

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

155 of 211

1

2

3

156 of 211

1

2

3

1

157 of 211

1

2

3

1

2

158 of 211

1

2

3

1

2

3

159 of 211

1

2

3

160 of 211

1

2

3

4

161 of 211

O(n2)

O(n log n)

O(n)

O(log n)

O(1)

162 of 211

O(n2)

O(n log n)

O(n) insert

O(log n) search

O(1)

163 of 211

linked lists

164 of 211

165 of 211

1

0x123

166 of 211

1

0x123

2

0x456

167 of 211

1

0x123

2

0x456

3

0x789

168 of 211

1

0x123

2

0x456

3

0x789

169 of 211

1

0x123

0x456

2

0x456

3

0x789

170 of 211

1

0x123

0x456

2

0x456

0x789

3

0x789

171 of 211

1

2

3

172 of 211

dict

list

range

set

tuple

...

173 of 211

trees

174 of 211

binary search trees

175 of 211

1

2

3

4

5

6

7

176 of 211

1

2

3

4

5

6

7

177 of 211

1

2

3

4

5

6

7

178 of 211

1

2

3

4

5

6

7

179 of 211

4

2

6

1

3

5

7

180 of 211

4

2

6

1

3

5

7

181 of 211

hash tables

182 of 211

183 of 211

0

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

184 of 211

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

185 of 211

186 of 211

Albus

187 of 211

Albus

Zacharias

188 of 211

Albus

Hermione

Zacharias

189 of 211

Albus

Ginny

Hermione

Ron

Zacharias

Fred

Severus

Petunia

Draco

James

Cedric

Luna

Neville

Kingsley

Minerva

Vernon

190 of 211

Albus

Ginny

Hermione

Ron

Zacharias

Fred

Severus

Petunia

Draco

James

Cedric

Luna

Neville

Kingsley

Minerva

Vernon

Harry

191 of 211

Albus

Ginny

Hermione

Ron

Zacharias

Fred

Severus

Petunia

Draco

James

Cedric

Luna

Neville

Kingsley

Minerva

Vernon

Harry

Hagrid

192 of 211

Albus

Ginny

Hermione

Ron

Zacharias

Fred

Severus

Petunia

Draco

James

Cedric

Luna

Neville

Kingsley

Minerva

Vernon

Harry

Hagrid

Sirius

Remus

George

Lily

Lucius

193 of 211

input → 

→ output

194 of 211

hash function

195 of 211

Albus → 

→ 0

196 of 211

Zacharias → 

→ 25

197 of 211

Haa

Hab

Hac

Had

Hae

Haf

Hag

...

Haq

Har

Has

...

Heq

Her

Hes

Hermione

Harry

Hagrid

198 of 211

O(n2)

O(n log n)

O(n)

O(log n)

O(1)

199 of 211

O(n2)

O(n log n)

O(n) search

O(log n)

O(1)

200 of 211

O(n2)

O(n log n)

O(n) search

O(log n)

O(1) insert

201 of 211

Albus

Ginny

Hermione

Ron

Zacharias

Fred

Severus

Petunia

Draco

James

Cedric

Luna

Neville

Kingsley

Minerva

Vernon

Harry

Hagrid

Sirius

Remus

George

Lily

Lucius

Lavender

202 of 211

dictionaries

203 of 211

dict

list

range

set

tuple

...

204 of 211

205 of 211

queues

206 of 211

stacks

207 of 211

208 of 211

Lab 0

209 of 211

Assignment 2

210 of 211

Office Hours

211 of 211

CS50 for JDs

Algorithms, Data Structures