CLRS - Chapter 1 & 2 Flashcards

1
Q

What is an algorithm?

A

An algorithm in any well-defined computational procedure that takes some value, or set of values as input and produces some value, or set of values, as output

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

How do we formally define the sorting problem?

A

Input: a sequence of n numbers

Output: a permutation (reordering) of the input sequence such that a1’ <= a2’ <= … <= an’

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

When an algorithm is said to be correct?

A

An algorithm is said to be correct if, for every input instance, it halts with the correct output.

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

What is a data structure?

A

A data structure is a way to store and organize data in order to facilitate access and modifications.

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

In which cases insertion sort is said to be efficient?

A

When the sorting involves a small number of elements. , because the constant is usually way lower than in merge sort or quick sort. Since it sorts IN PLACE.

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

Which line is missing from the insertion sort algorithm below?

INSERTION-SORT(A):
   for i=1 to A.length - 1:
      key = A[i]
      j = i - 1
      while j >= 0 and A[j] > key:
         A[j + 1] = A[j]
         j = j - 1
A

A[j + 1] = key

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

Which line is missing from the insertion sort algorithm below?

INSERTION-SORT(A):
   for i=1 to A.length - 1:
      key = A[i]
      j = i - 1
      while j >= 0 and A[j] > key:
     j = j - 1
  A[j + 1] = key
A

A[j + 1] = A[j]

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

What is the loop invariant of Insertion Sort?

A

At the start of each iteration of the for loop, the subarray A[0 .. i - 1] consists of the elements originally in A[0 .. i - 1], but in sorted order.

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

What is the best-case running time of Insertion Sort (ignoring leading constants and lower order terms)?

A

O(n)

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

What are the three properties that must be proved for a loop invariant?

A

Initialization, Maintenance and Termination

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

What is the meaning of algorithm analysis?

A

Analyzing an algorithm has come to mean predicting the resources that the algorithm requires. Ocasionally, resources such as memory, communication bandwidth, or computer hardware are of primary concern, but most often it is computational time that we want to measure.

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

Does the running time of insertion sort depend on the input?

A

True

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

What is the “running time” of an algorithm?

A

Is the number of primitive operations, instructions or “steps” executed.

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

What are the three reasons why we are mostly concerned about the worst-case running time of an algorithm?

A

+ It gives us and upper bound. A guarantee that the algorithm won’t take any longer
+ It occurs fairly often (searching a database)
+ Often as bad as the average-case (insertion sort)

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

What are the three steps of the Divide-and-conquer approach of algorithm design:

A

Divide, Conquer and Combine

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

Describe the three steps of the Divide-and-conquer approach in merge sort:

A

+ Divide the n-element sequence to be sorted in two subsequences of n/2 elements each
+ Conquer: Sort the two subsequences recursively using merge sort ( base case when n = 1)
+ Combine: Merge the two sorted subsequences using merge procedure, to produce the desired output.