Sorting Algorithms Flashcards

1
Q

What is the idea behind Insertion Sort

A
  • Iteratively build a sorted portion of the array.
  • Insert the next unsorted element into its proper place in the already sorted part.
  • Insertion sort is in-place (requires no extra array).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the idea behind Selection Sort

A
  • Repeatedly find the minimum element from the unsorted portion and place it at the beginning.
  • Selection sort is in place
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the idea behind Quick Sort

A
  • Divide-and-conquer algorithm.
  • Select a pivot element and partition the array into elements less than and greater than the pivot.
  • Recursively sort the sub-arrays.
  • Can be implemented in-place but is not stable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the idea behind Merge Sort

A
  • Divide and conquer algorithm
  • Divide the array into two halves, recursively sort each half, and then merge the sorted halves
  • The standard merge sort is not in-place (requires O(n) extra space) but is stable
  • In-place versions exist.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the idea behind Tree Sort

A
  • Insert all elements into a balanced binary search tree (e.g. an AVL tree).
  • Perform an in-order traversal to get the sorted sequence.
  • Tree sort is not in-place.
  • It can be stable if insertion preserves the order of equal elements.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the idea behind Heap Sort?

A
  • Convert the array into a heap
  • Repeatedly extract the largest element and place it at the end of the array.
  • Heap sort is in-place but not stable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the concept of a divide-and-conquer sort?

A

Divide a problem into smaller sub-problems, solve them recursively and combine (conquer) the solutions

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

What are examples of a divide and conquer sort?

A
  • merge sort (most of the work in the combining step)
  • quick sort (most of the work in the dividing step)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are examples of classic sorting algorithms?

A
  • insertion sort (efficient for nearly sorted arrays)
  • selection sort (consistently O(n²) regardless of input order)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are examples of tree-based sorts

A
  • tree sort (balanced binary search tree)
  • heap sort (heap data structure)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the idea behind Bucket Sort?

A

Distribute elements into “buckets” based on their keys, then concatenate the buckets in order

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

What are is the Bucket Sort process?

A

For a sequence S of n items with integer keys in [0, d-1]:

  • Create an array B of d empty buckets.
  • For each item x in S: Determine its key k and remove x from S and append it to bucket B[k].
  • After all items are distributed, S becomes empty.
  • concatenate all items from bucket B[0] up to B[d-1] back into S in order.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an advantage of bucket sort

A

Efficient when the range d is small compared to n (e.g., 8- or 16-bit integers).

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

What are the limitations of bucket sort?

A
  • Not feasible for sorting large-key ranges (e.g., 32-bit integers) due to high space use (O(d)).
  • Not suitable for floating-point numbers (infinitely many possible values) or character strings (requires a unique bucket per key).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the idea behind Radix sort?

A
  • Sort keys by processing individual digits (or characters) instead of using one overall key.
  • Divides each key into digits; the number of possible digit values is called the radix (R).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the two types of Radix Sort?

A
  • Most Significant Digit First (MSD)
  • Least Significant Digit First (LSD)
17
Q

What is the approach of MSD Radix Sort?

A
  • Start by partitioning keys based on their most significant digit.
  • Recursively sort each partition by the next most significant digit.
  • Use R empty lists (buckets) for each digit value.
  • For small lists, a simpler sort (like insertion sort) may be used.
  • Finally, concatenate the sorted lists.
18
Q

What is the approach of LSD Radix Sort?

A
  • Process the keys by performing a series of stable bucket sorts on each digit, starting from the least significant digit and moving to the most significant.
  • For each digit position:
     Use R temporary lists. Place keys into lists based on the current digit. Concatenate the lists back into the input array.
19
Q

Between MSD and LSD, which is stable?

A

LSD is stable, maintaining the relative order of equal keys. MSD is not stable since its relative order of equal keys may change.