1 of 177

This is CS5❤️

2 of 177

Think.

Pair.

Share.

3 of 177

  • What are structs? Why need them?
  • How do we define and use our own functions?
  • What is Big O notation?

4 of 177

Structs

5 of 177

6 of 177

typedef struct

{

string name;

int votes;

}

candidate;

7 of 177

typedef struct

{

string name;

int votes;

}

candidate;

8 of 177

typedef struct

{

string name;

int votes;

}

candidate;

9 of 177

typedef struct

{

string name;

int votes;

}

candidate;

10 of 177

candidate president;

11 of 177

candidate president;

president.name = "Alyssa";

president.votes = 10;

12 of 177

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 probability of winning (as a float)

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

13 of 177

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.

14 of 177

Array of Structs

15 of 177

name

Alice

Bob

Charlie

votes

2

1

3

16 of 177

name

Alice

Bob

Charlie

votes

2

1

3

candidates[0];

17 of 177

name

Alice

Bob

Charlie

votes

2

1

3

candidates[0];

18 of 177

name

Alice

Bob

Charlie

votes

2

1

3

candidates[0].name;

19 of 177

name

Alice

Bob

Charlie

votes

2

1

3

candidates[0].votes;

20 of 177

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.

21 of 177

22 of 177

Searching

23 of 177

Think.

Pair.

Share.

• When might it make sense to sort before searching?

• When would it make sense to simply search?

24 of 177

Linear Search

25 of 177

Runtime analysis

26 of 177

O(N) — "worst case" definition

In the worst case, I need to do approximately N steps for an input of size N.

27 of 177

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".

28 of 177

Ω(1) — "best case" definition

In the best case, for instance where the element is found at the first index.

29 of 177

30 of 177

Sorting

31 of 177

  1. Bubble Sort
  2. Selection Sort
  3. Merge Sort

Some Sorting Algorithms

32 of 177

Bubble Sort

33 of 177

5

3

4

8

2

1

7

6

34 of 177

3

5

4

8

2

1

7

6

35 of 177

3

4

5

8

2

1

7

6

36 of 177

3

4

5

2

8

1

7

6

37 of 177

3

4

5

2

1

8

7

6

38 of 177

3

4

5

2

1

7

8

6

39 of 177

3

4

5

2

1

7

6

8

40 of 177

3

4

5

2

1

7

6

8

41 of 177

3

4

2

5

1

7

6

8

42 of 177

3

4

2

1

5

7

6

8

43 of 177

3

4

2

1

5

6

7

8

44 of 177

3

4

2

1

5

6

7

8

45 of 177

3

2

4

1

5

6

7

8

46 of 177

3

2

1

4

5

6

7

8

47 of 177

3

2

1

4

5

6

7

8

48 of 177

2

3

1

4

5

6

7

8

49 of 177

2

1

3

4

5

6

7

8

50 of 177

2

1

3

4

5

6

7

8

51 of 177

1

2

3

4

5

6

7

8

52 of 177

1

2

3

4

5

6

7

8

53 of 177

1

2

3

4

5

6

7

8

54 of 177

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

55 of 177

Repeat n - 1 times

For j from 0 to n - 2

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

Swap them

56 of 177

Runtime analysis

57 of 177

O(N2) — "worst case" definition

In the worst case, say when elements are in reverse order, I need to do approximately N2 steps if my input size is N.

58 of 177

O(N2) — "scaling" definition

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

59 of 177

Ω(N) — "best case" definition

In the best case, say when all elements are already sorted, I need to do approximately N steps if my input size is N.

60 of 177

61 of 177

Selection Sort

62 of 177

5

3

4

8

2

1

7

6

63 of 177

5

3

4

8

2

1

7

6

64 of 177

5

3

4

8

2

1

7

6

65 of 177

5

3

4

8

2

1

7

6

66 of 177

5

3

4

8

2

1

7

6

67 of 177

1

3

4

8

2

5

7

6

68 of 177

1

3

4

8

2

5

7

6

69 of 177

1

3

4

8

2

5

7

6

70 of 177

1

2

4

8

3

5

7

6

71 of 177

1

2

4

8

3

5

7

6

72 of 177

1

2

4

8

3

5

7

6

73 of 177

1

2

3

8

4

5

7

6

74 of 177

1

2

3

8

4

5

7

6

75 of 177

1

2

3

8

4

5

7

6

76 of 177

1

2

3

4

8

5

7

6

77 of 177

1

2

3

4

8

5

7

6

78 of 177

1

2

3

4

8

5

7

6

79 of 177

1

2

3

4

5

8

7

6

80 of 177

1

2

3

4

5

8

7

6

81 of 177

1

2

3

4

5

8

7

6

82 of 177

1

2

3

4

5

8

7

6

83 of 177

1

2

3

4

5

6

7

8

84 of 177

1

2

3

4

5

6

7

8

85 of 177

1

2

3

4

5

6

7

8

86 of 177

1

2

3

4

5

6

7

8

87 of 177

Runtime analysis

88 of 177

O(N2) — "worst case" definition

In the worst case, I need to do approximately N2 steps if my input size is N.

89 of 177

Ω(N2) — "best case" definition

Even in the best case, I still need to do approximately N2 steps if my input size is N.

90 of 177

91 of 177

Merge Sort

92 of 177

5

3

4

8

2

1

7

6

93 of 177

5

3

4

8

2

1

7

6

94 of 177

5

3

4

8

2

1

7

6

95 of 177

2

1

7

6

5

3

4

8

96 of 177

2

1

7

6

5

3

4

8

97 of 177

2

1

7

6

4

8

5

3

98 of 177

2

1

7

6

4

8

5

3

99 of 177

2

1

7

6

4

8

5

3

100 of 177

2

1

7

6

4

8

5

3

101 of 177

2

1

7

6

4

8

5

3

102 of 177

2

1

7

6

4

8

5

3

103 of 177

2

1

7

6

4

8

5

3

104 of 177

2

1

7

6

4

8

5

3

105 of 177

2

1

7

6

4

8

5

3

106 of 177

2

1

7

6

4

8

5

3

107 of 177

2

1

7

6

5

3

4

8

108 of 177

2

1

7

6

5

3

4

8

109 of 177

2

1

7

6

5

3

4

8

110 of 177

2

1

7

6

5

3

4

8

111 of 177

2

1

7

6

5

3

4

8

112 of 177

2

1

7

6

5

3

4

8

113 of 177

2

1

7

6

5

3

4 8

114 of 177

2

1

7

6

5

3

4

8

115 of 177

2

1

7

6

5

3

4

8

116 of 177

2

1

7

6

5

3

4

8

117 of 177

2

1

7

6

5

3

4

8

118 of 177

2

1

7

6

5

3

4

8

119 of 177

2

1

7

6

3

5 4

8

120 of 177

2

1

7

6

5

3

4

8

121 of 177

2

1

7

6

3

4

8

5

122 of 177

2

1

7

6

3

4

8

5

123 of 177

2

1

7

6

5

3

4

8

124 of 177

2

1

7

6

5

3

4

8

125 of 177

5

3

4

8

2

1

7

6

126 of 177

5

3

4

8

2

1

7

6

127 of 177

5

3

4

8

7

6

2

1

128 of 177

5

3

4

8

7

6

2

1

129 of 177

5

3

4

8

7

6

2

1

130 of 177

5

3

4

8

7

6

2

1

131 of 177

5

3

4

8

7

6

2

1

132 of 177

5

3

4

8

7

6

2

1

133 of 177

5

3

4

8

7

6

2

1

134 of 177

5

3

4

8

7

6

2

1

135 of 177

5

3

4

8

7

6

2

1

136 of 177

5

3

4

8

7

6

2

1

137 of 177

5

3

4

8

2

1

7

6

138 of 177

5

3

4

8

2

1

7

6

139 of 177

5

3

4

8

2

1

7

6

140 of 177

5

3

4

8

2

1

7

6

141 of 177

5

3

4

8

2

1

7

6

142 of 177

5

3

4

8

2

1

7

6

143 of 177

5

3

4

8

2

1

7

6

144 of 177

5

3

4

8

2

1

6

7

145 of 177

5

3

4

8

2

1

6

7

146 of 177

5

3

4

8

2

1

7

6

147 of 177

5

3

4

8

2

1

7

6

148 of 177

5

3

4

8

2

1

7

6

149 of 177

5

3

4

8

2

1

7

6

150 of 177

5

3

4

8

2

1

7

6

151 of 177

5

3

4

8

2

1

7

6

152 of 177

5

3

4

8

2

1

7

6

153 of 177

5

3

4

8

2

1

7

6

154 of 177

5

3

4

8

2

1

7

6

155 of 177

5

3

4

8

2

1

7

6

156 of 177

5

3

4

8

2

1

7

6

157 of 177

5

3

4

8

2

1

7

6

158 of 177

5

3

4

8

2

1

7

6

159 of 177

3

4

8

2

1

7

6

5

160 of 177

3

4

8

2

1

7

6

5

161 of 177

5

3

4

8

2

1

7

6

162 of 177

5

3

4

2

1

7

6

8

163 of 177

5

3

4

2

1

7

6

8

164 of 177

5

3

4

8

2

1

7

6

165 of 177

5

3

4

8

2

1

7

6

166 of 177

Runtime analysis

167 of 177

5

3

4

8

2

1

7

6

5

3

4

8

2

1

7

6

5

3

4

8

5

3

2

1

7

6

2

1

7

6

4

8

168 of 177

O(N log2(N)) — "worst case" definition

In the worst case, I need to do about log2(N) steps to find my solution, for N elements.

169 of 177

O(N log2(N)) — "scaling" definition

I don't need to take another step/level in my algorithm until I double my input.

170 of 177

Ω(N log2(N)) — "best case" definition

In the best case as well, I need to do about log2(N) steps/levels to find my solution, for N elements.

171 of 177

Time Complexities of the 3 sorting algorithms

Algorithm

Time Complexity

Best case

Worst case

Bubble sort

Ω(N)

O(N2)

Selection sort

Ω(N2)

O(N2)

Merge sort

Ω(N logN)

O(N logN)

172 of 177

  • How can we identify/distinguish sorting algorithms from nothing but their binary?

173 of 177

Sort

174 of 177

.txt file used

Real Time Taken for Each Algorithm (in seconds)

sort1

sort2

sort3

sorted 5000

sorted 10000

sorted 50000

reversed 5000

reversed 10000

reversed 50000

random 5000

random 10000

random 50000

175 of 177

Plurality

176 of 177

Watch Shorts

Office Hours

177 of 177

This was CS5❤️