Lecture 4 Flashcards
What is Linear Search?
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.
What is Binary Search?
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.
Let’s say we’re searching for the value 50. Write some pseudocode for both Binary Search and Linear Search algorithms.
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
In CS, what does Order refer to, and how do we express it? What is the Order of Binary Search and Linear Search?
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.
What does Omega refer to in CS?
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.
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?
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”
What is Bubble Sorting?
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.
What is the O value of Bubble Sorting?
n², which is the worst possible. The processing power squares each time n increases.
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.
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.
What is Selection Sort?
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].
What is the O value of Selection Sort?
Same as Bubble Sort, n².
Which uses localized optimization, Bubble Sort or Selection Sort?
Bubble Sort. Selection Sort does not do any localized optimization.
What is an easy way to optimize Bubble Sort?
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.
What is Recursion? Explain using the Phone Book problem.
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.
What is Merge Sort? How does it work?
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.