1.2-2

n > 0 = =

  • here we can test a variety of n values to see where the switch happens and since we’re using base 2 we can test with base 2 values(i.e. )

    1. which holds true becase
    1. which doesn’t hold true because
  • this means the n value we want is between 32 and 64.

  • at : (insertion), (merge) and which holds true

  • at : (insertion) (merge) and which doesn’t hold true.

  • the value of n where insertion sort beats merge sort is

1.2-3

=

  • here we have to experiment with different values of n
  1. : which is false
  2. : which is also false
  3. : which is also false
  4. : which holds true
  • so the smallest value of such that an algorithm whose running time is runs faster than an algorithm whose running time is on the same machine is

1.1-1

  • my real world example that requires sorting is usually when I’m playing Old Maid(a card game) which requires that I have a standard deck of planning cards but sometimes we mix our cards which forces me to sort them.
  • one that requires finding the shortest distance between 2 points is whenever I need directions from my apartment to campus or going from my bedroom to the mailbox at my apartment.

2.1-1

  1. - first element already sorted for us
  2. - same for the second element
  3. - same for the third element
  4. - her we insert 26 at the front as it’s the smallest element we’ve seen
  5. - here we insert 41 after the 41 we had already seen
  6. - here we insert 58 between 41 and 59

2.1-2

Loop Invariant: At the start of each iteration , the variable sum equals the sum of the first elements of the array

Before the loop starts (), sum = 0, which is the sum of zero elements.

We also knwo that each iteration adds to sum, so it eventually becomes the sum of the first elements.

When the loop ends (), sum contains the sum of all elements in .

2.1-4

for i = 1 to n
    if A[i] == x
        return i
        
return NIL
  • Loop Invariant: At the start of each iteration , the value does not appear in .
  • Before the first iteration (), is empty, so could not have appeared yet.
  • If , then once we check , still does not appear in , so the invariant holds true for the next iteration.
  • If the loop gets to , it returns the index we want. If the loop ends without getting , then does not appear in , so returning NIL is ok.

Prove from the definitions of set union, intersection, complement and equality that

Let’s let be any element.

To show the two sets are equal, we have to ensure tht they contain the same elements.

Let be any element.

Since both sets contain the same elements,

Prove by induction on n

Base case n = 1 :

This is true.

Inductive step:

Let’s assume the statement holds for n = k :

For n = k+1:

Using De Morgan’s Law:

Then using inductive hypothesis:

Therefore, the statement holds for all .