Loop invariants Flashcards

(19 cards)

1
Q

What is the main idea behind selection sort?

A

Repeatedly find the smallest element and move it to the front of the unsorted portion.

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

What is the time complexity of selection sort?

A

O(n²)

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

What is a loop invariant?

A

A logical condition that remains true before, during, and after each loop iteration.

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

Why is a loop invariant useful?

A

It helps prove the correctness of an algorithm.

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

What is the loop invariant for selection sort?

A

At iteration i, the first i elements are sorted and smaller than all remaining elements.

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

What is the loop invariant for insertion sort?

A

At iteration i, the first i elements are sorted and the array is a permutation of the original.

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

What does insertion sort do at each iteration?

A

Inserts the current element into its correct position among the previously sorted elements.

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

When is insertion sort most efficient?

A

When the input array is already or nearly sorted.

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

What is the best-case time complexity of insertion sort?

A

O(n)

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

What is the worst-case time complexity of insertion sort?

A

O(n²)

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

What causes the worst-case performance in insertion sort?

A

If the array is in reverse order — each insertion requires shifting many elements.

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

How does selection sort find the next element to place?

A

By scanning the unsorted portion for the minimum value.

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

Is selection sort stable?

A

No — it can swap equal elements out of order.

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

Is insertion sort stable?

A

Yes — it preserves the order of equal elements.

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

Why is insertion sort considered simple and intuitive?

A

It mimics how humans sort items like cards — inserting new items one at a time.

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

How can you formally prove a sorting algorithm is correct?

A

By defining and validating a loop invariant.

17
Q

What does the invariant guarantee at the end of insertion sort?

A

The entire array is sorted.

18
Q

Why is selection sort not ideal for large datasets?

A

Because its O(n²) time complexity scales poorly as n increases.

19
Q

What is a potential downside of using timing-based measurements for complexity?

A

Timing is affected by hardware, system load, and other noise — it’s not reliable.