CLSR Chap2 Flashcards

1
Q

What indexes are compared during a single cycle of insertion sort?

A

The comparison is between the index being sorted and all previous indexes until the studied index is no longer greater than the previous.

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

In insertion sort, what determines whether a swap is performed or not?

A

In insertion sort, a swap is performed if the rightmost index checked is smaller than the leftmost index (assuming ascending order).

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

From what indexes of the array does the insertion sort algorithm need to loop through, and why?

A

Insertion sort needs to loop through array j=2 to Array.length because we need to loop through all elements of the array and we cannot swap the first element to the left.

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

What is the condition needed to loop through all previous iterations in code?

A

while i > 0 and A[i] > key …. then perform the swap

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

In insertion sort, how can we perform the swapping of two values in code?

A

A[i+1] = A[i], where i = j-1 is each the previous index.

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

What range of indexes of elements are in order while searching element at index j?

A

All indexes to the left of index j-1 are presumably in order already, or A[1…j-1].

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