Sort algorithms Flashcards

1
Q

The key steps for Bubble Sort are:

A
  1. Iterate through each pair of elements (i, i+1)
  2. Compare them; swap them if not in the desired order
    (largest/smallest element should be at end of array after first iteration)
  3. Repeat for subsequent iterations, excluding the most recently sorted value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The key steps for Insertion Sort are:

A
  1. Assume the first element is sorted.
  2. Iterate through each subsequent element (in the unsorted subarray): this is the target.
  3. Determine the most appropriate place in sorted subarray to insert the target element
  4. Carry out the insertion using pop/insert
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The key steps for Merge Sort are:

A
  1. Handle the base case (empty/single-element array)
  2. Split the array into left and right subarrays
  3. Recursively sort the left and right subarrays
  4. Pass left and right subarrays to merge function to obtain sorted array

Merge function

  1. Initialise empty array (arr)
  2. Check if left or right subarrays are empty; if either is empty, return arr + left + right
  3. If both are not empty, compare first elements of left & right subarray.
  4. Pop the smaller/greater first element into arr; repeat from Step 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The key steps for Quick Sort are:

A
  1. Handle the base case (empty/single-element array)
  2. Select pivot (can use last element)
  3. Initialise less-than (lt) and greater-than-equal (gte) subarrays
  4. Iterate through all elements except pivot; if element less than pivot, append to lt; if element greater than or equal pivot, append to gte.
  5. Recursively sort lt and gte
  6. Recombine lt, gte, and pivot into a single array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which two sorts are in-place?

A
  1. Bubble Sort
  2. Insertion Sort

[Merge Sort and Quick Sort are both out-of-place]

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

What is the time complexity for an Insertion Sort?

A

O(n^2)

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

Under what circumstances is the quicksort most suboptimal?

A

When the provided list is nearly sorted or already sorted.

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

What is the time complexity for a Quick Sort?

A

O(n log n)

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

What does it mean when a sorting algorithm is said to be ‘stable’?

A

A stable sorting algorithm ensures that elements with identical sorting properties still maintain their original order.

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

What is the key determining factor that affects the execution time of a sort algorithm?

A

The size of the given list.

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