sorting algorithms Flashcards

(19 cards)

1
Q

What is the main idea behind Shellsort?

A

Use decreasing stride lengths to partially sort the array, ending with a final insertion sort.

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

How does Shellsort improve over insertion sort?

A

It allows distant elements to move earlier in the process, reducing the number of shifts needed later.

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

What determines Shellsort’s performance?

A

The gap (stride) sequence used.

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

What is the worst-case time complexity of Shellsort with Shell’s original gap sequence?

A

O(n²)

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

What is the worst-case time complexity of Shellsort with improved sequences?

A

O(n^{3/2})

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

Why is Shellsort more efficient than insertion sort?

A

Because it reduces disorder before the final insertion pass.

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

What does Shellsort preserve while sorting?

A

It preserves the permutation of the original array.

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

What is MergeSort’s core strategy?

A

Divide the array into halves, sort each recursively, then merge the results.

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

What does the merge step do in MergeSort?

A

It combines two sorted arrays into one sorted array by comparing and appending the smallest elements.

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

Why is the merge step critical to MergeSort?

A

Because the correctness of MergeSort depends on merging two sorted halves correctly.

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

What is the loop invariant in the Merge operation?

A

At each step, the merged result contains the smallest i elements from both arrays in sorted order.

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

How does MergeSort achieve O(n log n) time complexity?

A

Each level of recursion does O(n) work, and there are log n levels.

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

What is MergeSort’s recurrence relation?

A

T(n) = 2T(n/2) + n

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

What is the time complexity of MergeSort?

A

Θ(n log n)

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

Why is MergeSort better than quadratic algorithms for large n?

A

It handles large input sizes efficiently — much faster than O(n²).

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

How long would O(n²) take to sort 1 billion items?

A

Approximately 316 years (at 10⁸ ops/sec)

17
Q

How long would MergeSort take for 1 billion items?

A

About 5 minutes (at 10⁸ ops/sec)

18
Q

What type of algorithm is MergeSort?

A

A divide-and-conquer algorithm.

19
Q

What are the benefits of using a loop invariant in MergeSort?

A

It helps prove that the merge step (and thus the full algorithm) maintains sorted order.