CS50 Week 4 Flashcards

1
Q

What is the O of merge sort?

A

nlog(n)

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

What is the \Omega of merge sort?

A

nlog(n)

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

When the O and \Omega of an algorithm are the same, how is that denoted?

A

The algorithm’s runtime can be described as Theta(n)

n is the appropriate runtime.

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

What is the pseudocode implementation of merge sort?

A

On input of n elements:
If n < 2
Return;

Else:
    Sort left half of elements;
    Sort right half of elements;
    Merge sorted halves;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the tradeoff when considering merge sort?

A

You trade efficiency for speed. Specifically, more memory is required of merge sort, because a second list is needed for intermediate storage.

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

Why is merge sort Theta nlog(n)?

A

The log(n) portion is the breakdown of he list into halves.

the n constant reflects merging the lists together.

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

What is the basic concept of recursion?

A

Using a function to call itself using modified arguments, until a base case is reached that returns without calling itself.

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

When a variable is passed to a function, what is really passed?

A

A copy of that variable.

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

What is the syntax for a pointer?

A

type*

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

What is the syntax for dereferencing?

A

*type

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

What does dereferencing mean?

A

Go to the address stored in the pointer.

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

What is the syntax for passing a memory address to a function?

A

&x

where x is the variable.

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

What is the string type, as contained in the CS50 library?

A

A char*

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

How are strings compared to each other?

A

strcmp()

it is in string.h

It returns 0 if two strings are identical

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

What function allocates memory, and how does it work?

A

malloc(n)

malloc will allocate memory of n bytes and return the address of the first byte in memory?

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

How many bytes are needed to copy a string?

A

One more than the number of characters in the string. We must allocate space for the \0.

17
Q

What is a convenient way to store a number of a given data type

A

type* pointer = malloc(n * sizeof(type))

for example:

char* pointer = malloc(6 * sizeof(char))

18
Q

What happens to memory created by malloc when the function ends?

A

Nothing, it stays around in the heap. That’s why it needs to be freed.