sorting-algorithms-flashcards

1
Q

What is Selection Sort’s time complexity (best and worst case)?

A

O(n²) for both best and worst case - always needs to scan entire array multiple times

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

How does Selection Sort work?

A

Find minimum element in unsorted portion, swap it with first unsorted position, repeat

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

What are Selection Sort’s main advantages?

A

Simple to implement, performs well on small arrays, minimal memory usage (in-place)

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

What are Selection Sort’s main disadvantages?

A

Slow for large datasets, doesn’t adapt to partially sorted arrays, always O(n²)

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

What is Bubble Sort’s time complexity (best and worst case)?

A

Best case O(n) when already sorted, worst case O(n²)

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

How does Bubble Sort work?

A

Repeatedly step through the list, compare adjacent elements and swap if needed, until no swaps needed

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

What are Bubble Sort’s main advantages?

A

Simple to implement, best case O(n) on nearly sorted data, detects when array is sorted

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

What are Bubble Sort’s main disadvantages?

A

Very inefficient for large unsorted arrays, many swap operations, poor cache performance

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

What is Insertion Sort’s time complexity (best and worst case)?

A

Best case O(n) when nearly sorted, worst case O(n²)

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

How does Insertion Sort work?

A

Build sorted array one item at a time, insert each element into its correct position in sorted portion

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

What are Insertion Sort’s main advantages?

A

Efficient for small data, great for nearly sorted data, adaptive algorithm, in-place sorting

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

What are Insertion Sort’s main disadvantages?

A

Inefficient for large unsorted datasets, requires shifting elements during insertion

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

What is Merge Sort’s time complexity (best and worst case)?

A

O(n log n) for both best and worst case - consistently efficient

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

How does Merge Sort work?

A

Divide array into halves, sort recursively, merge sorted halves back together

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

What are Merge Sort’s main advantages?

A

Stable sort, guaranteed O(n log n), great for linked lists, predictable performance

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

What are Merge Sort’s main disadvantages?

A

Requires O(n) extra space, overkill for small arrays, not in-place

17
Q

What is Quick Sort’s time complexity (best and worst case)?

A

Best/Average case O(n log n), worst case O(n²) but rare with good pivot

18
Q

How does Quick Sort work?

A

Choose pivot, partition elements around pivot (smaller left, larger right), recursively sort partitions

19
Q

What are Quick Sort’s main advantages?

A

Usually fastest in practice, in-place sorting, good cache performance, widely used

20
Q

What are Quick Sort’s main disadvantages?

A

Unstable sort, worst case O(n²), recursive stack space needed, performance depends on pivot

21
Q

What is Heap Sort’s time complexity (best and worst case)?

A

O(n log n) for both best and worst case - consistently efficient

22
Q

How does Heap Sort work?

A

Build max heap from array, repeatedly extract max element and maintain heap property

23
Q

What are Heap Sort’s main advantages?

A

In-place sorting, guaranteed O(n log n), constant extra space, good for priority queues

24
Q

What are Heap Sort’s main disadvantages?

A

Unstable sort, slower in practice than quicksort, poor cache performance, complex implementation

25
Q

Which sorting algorithms are stable?

A

Merge Sort, Bubble Sort, and Insertion Sort maintain relative order of equal elements

26
Q

Which sorting algorithms are in-place?

A

Selection Sort, Bubble Sort, Insertion Sort, Quick Sort, and Heap Sort use O(1) extra space

27
Q

For small arrays (<50 elements) which sorts are best?

A

Insertion Sort or Selection Sort due to low overhead and good cache performance

28
Q

For nearly sorted data which algorithm is best?

A

Insertion Sort - achieves O(n) time complexity on nearly sorted arrays