Lecture 4 Flashcards

1
Q

What is Linear Search?

A

Linear Search begins at 0, and searches a dataset - often an array - for a specific value, incrementing by one each time.

We have to recall that computers cannot ‘see’ an array all at once - they have to examine each of it’s discrete values one by one. Linear Search lets us do this by counting up from 0 to n-1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Binary Search?

A

We saw an example of Binary Search in Week 0’s Phone Book example: Binary Search divides the dataset size by 2 repeatedly until it finds the value being searched for.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Let’s say we’re searching for the value 50. Write some pseudocode for both Binary Search and Linear Search algorithms.

A

Linear Search:
for i in n, i = 0, i < n, i++
If value is 50,
return true

return false

Binary Search:
If no terms, return false
If value = 50, return true
Else, divide by 2. If 50 < middle item
 Repeat in left half of array
Else, if 50 > middle item
  Repeat in right half of array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In CS, what does Order refer to, and how do we express it? What is the Order of Binary Search and Linear Search?

A

We express the efficiency of algorithms through the use of Order (O). Order is the UPPER BOUND. If you’re using linear search to search an array of 100 values, your upper bound is 100. You might get lucky and find the value in your first try, but engineers are obsessed with the upper bound of any processing cost.

Thinking back to our phone book problem, we saw a graph showing that binary search and linear search had fundamentally different processing costs: binary search is logarithmic to the number of values (log n), whereas linear search is equal to the number of values itself (n).

So, Binary search would have an O of log n, and Linear search would have an O of n.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does Omega refer to in CS?

A

Just as Order (O) refers to the UPPER BOUND of any instruction set, the Omega (Ω) refers to the LOWER BOUND - the ideal case, or how much processing power we’ll use if we get really lucky.

The Ω of both Linear Search and Binary Search is 1 - we might find the value on the first try.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Structure?

Let’s say I want to make a struct to hold a person’s name and number. What’s the syntax both to define the struct, and to enter one set of values for the first person?

A

In C, and most languages, a Structure, or Struct, is a customizable data type. It can hold various parameters for an abstract item or data type.

In C, we define a Struct using;

typedef struct 
{
  string name
  string number
}
person; // this is the struct TYPE not name

person people[4]; //people is the name of this INSTANCE of the struct.
people[0].name = “EMMA”;
people[0].number = “212-731-8665”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Bubble Sorting?

A

A means of sorting a dataset through local optimization.

Let’s say we have an array of the following, which we want sorted in ascending order:
7, 1, 3, 4, 9, 2, 0, 5

Bubble Sort will take the first value, 7, and the second value, 1, and compare them. It will basically say, hey, for this to be sorted, are you both in the right order? And clearly they are not, so it will swap the two values. This is local optimization.

It will then compare numbers 2 and 3, etc. This is how the appropriate values ‘bubble up’ over time in the dataset.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the O value of Bubble Sorting?

A

n², which is the worst possible. The processing power squares each time n increases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain how Bubble Sort’s O value has implications for Binary Search.

Explain how each approach has tradeoffs for efficiency, and when it might be wise to use Binary Search + Bubble Sort.

A

Binary Search is on its own more efficient than Linear Search, but it has a big problem - that presupposes that the array is sorted, because Binary Search doesn’t work with an unordered array.

When we add the processing cost of Bubble Sort in here, Binary Search + Bubble Sort may be quite less efficient than Linear Search on its own, but it depends on the context.

If you’re building an app which will only need to search once, Linear Search is the clear winner. But if you’re building a function that will search a dataset over and over and over, then it might be worth it to spend the extra cost on Bubble Sort once, to repeatedly save lots of processing power with Binary Search.

Managing these tradeoffs is what great engineers do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Selection Sort?

A

Let’s say we have the following array;

7, 1, 3, 4, 9, 2, 0, 5

Selection sort will begin at value 0, and basically say, ‘is this the smallest value I’ve seen so far?’ - and it will store the value of the smallest number in a var.

So it will see 7, store that, then see 1, store that, and it will skip storing anything more til it gets to zero, which it stores. Once it reaches the end of the array, it swaps the smallest value it found with the i’th value (in this case array[0]), and then do the same from array[1].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the O value of Selection Sort?

A

Same as Bubble Sort, n².

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which uses localized optimization, Bubble Sort or Selection Sort?

A

Bubble Sort. Selection Sort does not do any localized optimization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an easy way to optimize Bubble Sort?

A

A basic bubble sort algo will tell the algorithm to go through an entire array, even if it’s already ordered.

A simple way to optimize is to tell your algorithm ‘repeat until no swap’ (in pseudocode), which means if you’re handed a fully ordered array, it will only go through it once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Recursion? Explain using the Phone Book problem.

A

Recursion is when a function calls itself.

In the phone book problem, once we open the book, look at the page, and determine if the name we’re searching for is indeed on the page, we basically say, ‘ok, search again’. But when we say this, we’re essentially calling the function again, because we’re going to open to a new page (whatever we were on before, /2) look at the page, see if the name is there, and so on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Merge Sort? How does it work?

A

Merge sort is a recursive sort algorithm, which uses the idea of ‘divide and conquer’.

The first step in merge sort is recursively breaking the larger array/DB down into it’s smallest components. It keeps dividing by 2 over and over until the entire array is broken down into lists of 1, and it cannot divide any more.

Then the merging begins. Starting from the left, it compares the first value in the left list to the first value in the right list, and sorts them. Then it does the same with the next pair of values.

At the next step, we now have 2 lists of 2 values each. It does the same again - compares the first value of the left list to the first value of the right list, and sorts them. Then it compares the second value of the left list to the second of the right.

And it keeps doing this until we return to a fully sorted array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a tip off that an algo is going to be on the order of log n or n log n (logarithmic)?

A

Any algorithm that divides repeatedly (like merge sort or binary search) is going to be logarithmic.

17
Q

What is the O value of Merge Sort

A

n log n. Far more efficient than selection sort and bubble sort.

18
Q

What is argc? What is argv?

A

When we run a command line app in C, we can supply strings, or arguments, after the program name. These strings are stored in the argv[] array, and can be used in our code.

Argc is the Count of Arguments, including the program name. So if I have 3 total arguments after the program name, argc = 4.

./plurality Donald Joe Bernie Jill
argv[] = [“Donald”, “Joe”, “Bernie”, “Jill”]
argc = 5

19
Q

How can we syntactically check if a boolean function returns false for a given value, using an if statement?

A

In plurality, we encounter this for the first time. We have a global function, vote, which takes 1 arg, name, and returns a true if this name matches a candidate in the election or false if it does not.

Earlier in our code, we can check for this by saying. What this exclamation point is saying is essentially if the following is not true.

if (!vote(name)) {


}