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
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. Merge left and right subarrays into sorted array

Merge function

  1. Initialise new array
  2. Compare first elements of left & right subarray.
  3. Pop the smaller/greater first element into array from (1); repeat from Step 2
  4. Concatenate remaining elements to the end of array from (1)
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

What is the time complexity for an Insertion Sort?

A

O(n²)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
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
7
Q

Under what circumstances is the quicksort least efficient?

A

When the provided list is nearly sorted or already sorted (pivot is not median value).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
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
9
Q

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

A

The size of the given array.

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

What is the time complexity for a Bubble Sort?

A

O(n²)

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

What is the time complexity for a Merge Sort?

A

O(n log n)

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

memory space (in-place vs out-of-place)

A
  1. an in-place implementation does not need additional data structures in its implementation
  2. an out-of-place implementation initializes a new array (or other data structure) fro storing sorted elements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

linear vs recursion

A
  1. linear algorithm is usually faster than recursive algorithm since recursion incurs extra overhead through function calls
  2. however its iterative nature requires all processing to be done on a single process thread with no opportunity for multiple threads to speed up the work

while recursive function by nature consists many self contained tasks which can be divided among many threads

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

bubble sort optimisation

A
  1. skips sorted elements
  2. final iteration can be skipper since only first element is unsorted
  3. no swaps are made subsequent iterations may be skipped
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

insertion sort optimisation

A
  1. target should be moved to the insertion point as efficiently as possible
  2. terminate index search once an insertion index is found
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

optimisation for merge sort

A
  1. minimisation of modification
    additional indices used to track position of elements in left and right array
17
Q

optimisation of quicksort

A

median item as pivot