CLRS - Chapter 1 & 2 Flashcards
What is an algorithm?
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 do we formally define the sorting problem?
Input: a sequence of n numbers
Output: a permutation (reordering) of the input sequence such that a1’ <= a2’ <= … <= an’
When an algorithm is said to be correct?
An algorithm is said to be correct if, for every input instance, it halts with the correct output.
What is a data structure?
A data structure is a way to store and organize data in order to facilitate access and modifications.
In which cases insertion sort is said to be efficient?
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.
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[j + 1] = key
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[j + 1] = A[j]
What is the loop invariant of Insertion Sort?
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.
What is the best-case running time of Insertion Sort (ignoring leading constants and lower order terms)?
O(n)
What are the three properties that must be proved for a loop invariant?
Initialization, Maintenance and Termination
What is the meaning of algorithm analysis?
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.
Does the running time of insertion sort depend on the input?
True
What is the “running time” of an algorithm?
Is the number of primitive operations, instructions or “steps” executed.
What are the three reasons why we are mostly concerned about the worst-case running time of an algorithm?
+ 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)
What are the three steps of the Divide-and-conquer approach of algorithm design:
Divide, Conquer and Combine