Algorithms and Data Structures Flashcards

1
Q

What is the main problem addressed in Insertion Sort?

A

Given a sequence, find a permutation such that the numbers are comparable.

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

What is the initial state of the left hand in the Insertion Sort algorithm?

A

Initially empty, all cards are on the table.

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

What does the right hand do in the Insertion Sort algorithm?

A

Picks up cards and puts them in the correct position in the left hand.

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

What is maintained in the left hand during Insertion Sort?

A

A sorted sequence.

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

What is the fundamental characteristic of an incremental algorithm?

A

It builds up a solution incrementally.

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

What is a loop invariant?

A

A condition that is always true at a specific point in the loop.

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

What does the correctness of an algorithm depend on?

A

It halts with the output required by the problem for every input instance.

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

What is runtime complexity related to in sorting algorithms?

A

The number of comparisons made.

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

What does space complexity measure?

A

The memory needed in relation to the input size.

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

What is assumed about the space required to store a number?

A

It takes constant space.

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

What are the three ingredients needed to prove the correctness of an algorithm?

A
  • Initialisation
  • Maintenance
  • Termination
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the role of initialisation in proving correctness?

A

To show that the invariant is true at the start of the first pass.

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

What does maintenance ensure in the context of loop invariants?

A

That the invariant is true at each pass of the loop.

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

What does termination demonstrate in proving correctness?

A

That the violated loop condition results in correctness.

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

What is the significance of the loop invariant in Insertion Sort?

A

It ensures that the elements are sorted after the algorithm completes.

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

Fill in the blank: An algorithm is ______ if it halts with the output required by the problem.

A

correct

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

True or False: The actual execution time of an algorithm is solely determined by runtime complexity.

A

False

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

What is the output of a factorial function when n is 0?

A

1

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

What is the base case for the factorial function?

A

If n is 0, return 1.

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

What happens if the loop in the factorial function is never entered?

A

It may return an error or an undefined value.

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

What type of algorithms are defined as finite sequences of instructions?

A

Algorithms

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

What is the main concept behind the Divide and Conquer algorithm?

A

It involves dividing a problem into smaller instances, conquering those sub-problems recursively, and then combining their solutions.

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

What are the three main steps of the Divide and Conquer strategy?

A
  • Divide
  • Conquer
  • Combine
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

In the context of Merge Sort, what does ‘Divide’ refer to?

A

Dividing an instance into smaller instances of the same problem.

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

In Merge Sort, what does ‘Conquer’ involve?

A

Recursively solving the sub-instances until a direct solution is possible.

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

What does ‘Combine’ mean in the context of Merge Sort?

A

Combining the partial solutions to form the solution for the original instance.

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

What is the first step in the MergeSort function?

A

Check if the array has one or zero elements.

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

What is the default call for MergeSort?

A

MergeSort(A, 1, A.length)

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

What does the Merge function do in Merge Sort?

A

It combines two sorted arrays into one sorted array.

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

What is the time complexity for the merging process in Merge Sort?

A

The merging process is not constant time.

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

True or False: Merge Sort can be implemented using recursion.

A

True

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

What are ‘sentinels’ in the context of Merge Sort?

A

Sentinels are used to simplify the merging process by preventing out-of-bounds errors.

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

Fill in the blank: The Merge function creates a new array of size ______.

A

[size1 + size2]

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

What is the base case for the MergeSort recursive function?

A

When the array has one or zero elements.

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

What happens during the ‘Combine’ step of Merge Sort?

A

The sorted sub-arrays are merged into a single sorted array.

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

How does the Merge function determine which element to add next?

A

By comparing the front elements of the two arrays being merged.

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

What is the significance of the indices i, j, and k in the Merge function?

A

They are used to track the current position in the left array, right array, and the merged array respectively.

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

True or False: Merge Sort is an example of an iterative sorting algorithm.

A

False

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

What is the overall time complexity of Merge Sort?

A

O(n log n)

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

What role does the recursive nature of Merge Sort play in its efficiency?

A

It allows the algorithm to break down the sorting process into manageable parts, leading to efficient sorting.

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

Fill in the blank: Merge Sort is particularly useful for ______.

A

large datasets

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

What happens if the input array for Merge Sort is already sorted?

A

It still performs O(n log n) operations, as it does not take advantage of existing order.

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

What type of sorting algorithm is Merge Sort classified as?

A

A comparison-based sorting algorithm.

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

What is the primary function of MergeSort?

A

To sort an array using the divide and conquer approach.

MergeSort divides the array into halves, sorts each half, and then merges them back together.

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

What is the base case for the MergeSort function?

A

If the array has one or zero elements, it is already sorted.

This condition prevents further recursion.

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

What does the Merge function do in the MergeSort algorithm?

A

Merges two sorted subarrays into a single sorted array.

It combines the smallest elements from each subarray.

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

In the Merge function, what is the significance of the variables L and R?

A

L represents the left subarray and R represents the right subarray.

These are used to track elements during the merging process.

48
Q

What is the time complexity of the MergeSort algorithm?

A

O(n log n).

This complexity arises from the division of the array and the merging process.

49
Q

True or False: MergeSort is an in-place sorting algorithm.

A

False.

MergeSort requires additional space for the temporary arrays used during the merge process.

50
Q

What method is used to prove the correctness of the Merge function?

A

Loop invariants.

This technique ensures that the smallest elements are correctly merged.

51
Q

Fill in the blank: MergeSort uses _______ to handle the sorting of subarrays.

A

recursion.

Each call to MergeSort processes smaller portions of the array.

52
Q

What is the purpose of the recursion tree in MergeSort?

A

To visualize the recursive calls and their corresponding subarrays.

The tree structure helps in understanding the divide and conquer approach.

53
Q

In the context of MergeSort, what does the induction hypothesis state?

A

MergeSort is correct for arrays of length k or less.

This is used to prove that it holds for arrays of length k+1.

54
Q

What happens during the termination of the for-loop in the Merge function?

A

The merged array is complete and sorted.

At this point, all elements from both subarrays have been processed.

55
Q

List the steps involved in the Merge function.

A
  • Create temporary arrays for L and R
  • Compare elements from L and R
  • Copy the smallest element back to the main array
  • Handle remaining elements

These steps ensure that the merging process is efficient and correct.

56
Q

What is the initial condition for the Merge function when first called?

A

At first pass, k = l.

This condition helps in tracking the current position in the array.

57
Q

What does the ‘if’ condition in MergeSort check for?

A

It checks if the subarray has more than one element.

This condition controls the recursive calls.

58
Q

True or False: MergeSort can be implemented iteratively.

A

True.

Although it is commonly implemented using recursion, an iterative version is also possible.

59
Q

What are the two cases considered in the maintenance of the Merge function?

A
  • The current element of L is smaller
  • The current element of R is smaller

These cases determine which element to copy next.

60
Q

What is the role of sentinels in the Merge function?

A

They help handle edge cases where one subarray may be exhausted before the other.

Sentinels act as placeholders to simplify comparisons.

61
Q

What are the three fundamental questions in algorithm analysis?

A

Correctness, Running time (Time complexity), Space complexity

62
Q

In runtime analysis of sorting, what does ‘n’ represent?

A

The length of the ‘array’

63
Q

What is the main focus when analyzing running time in sorting algorithms?

A

We only count comparisons

64
Q

What does the running time of an algorithm depend on?

A

The input characteristics, such as sorted or unsorted order

65
Q

What are the extreme cases to consider when analyzing running time?

A

Best case, Worst case

66
Q

What is the worst-case scenario for Merge Sort when ‘n’ is a power of 2?

A

Let C be the largest number of comparisons that Merge Sort needs to sort n numbers

67
Q

How do we characterize the behaviors of Insertion Sort and Merge Sort?

A

By comparing the number of comparisons each algorithm makes

68
Q

What is the definition of ‘big-Oh’ notation?

A

Big-Oh of g is the set of all functions that grow at most as fast as g

69
Q

What is an example of a function that grows quadratically?

A

f(n) = n^2

70
Q

When classifying functions, what should you identify?

A

The additive term with the largest growth

71
Q

What should you remove from the remaining term when classifying functions?

A

Constant terms

72
Q

What does it mean if a function grows ‘exactly’ quadratically?

A

It grows at the same rate as n^2

73
Q

What is the typical order of growth for functions used in algorithm analysis?

A

1 (constant), logarithmic, linear, linearithmic, quadratic, cubic, exponential

74
Q

True or False: Merge sort is a member of the set of functions that grow quadratically.

75
Q

Fill in the blank: The running time of an algorithm is proportional to the number of _______.

A

comparisons

76
Q

What is the rule of thumb for classifying functions based on growth?

A

Identify the additive term with the largest growth, remove other terms, and remove constants from the remaining term

77
Q

What is the definition of a data structure?

A

Concept to store and arrange data, so that they can be found and changed efficiently.

78
Q

What is the difference between an abstract data type and its implementation?

A

Abstract data type is the interface of a structure; implementation is how the features are realized.

79
Q

What is a priority queue?

A

Abstract data type that manages elements of a set, where each element has a priority.

80
Q

What operations are supported by a priority queue?

A
  • Insert(element, priority) * FindMax() * ExtractMax() * IncreaseKey(index, priority)
81
Q

What is the naive implementation for inserting an element into an array for a priority queue?

A

Increase size of array, assign new element to the end.

82
Q

What is the time complexity for the FindMax() operation in a naive implementation?

83
Q

True or False: The ExtractMax() operation in a naive implementation has a time complexity of O(1).

84
Q

What does a max-heap property ensure?

A

For every node, its value is greater than or equal to the values of its children.

85
Q

What is the definition of a heap?

A

An array corresponding to a binary tree, where all levels are full except the last one, which is filled left to right.

86
Q

What is a min-heap property?

A

For every node, its value is less than or equal to the values of its children.

87
Q

What is the goal when transforming an array into a max-heap?

A

To compute a max-heap in O(n) time.

88
Q

What is the elementary operation used to maintain the heap property?

A

Heapify or MaxHeapify.

89
Q

What does the MaxHeapify function do?

A

It sifts a node down if it is too small compared to its children.

90
Q

What is the time complexity of the MaxHeapify operation?

91
Q

Fill in the blank: A heap is an array corresponding to a binary tree, for which all levels are full except the last one, which is filled ______.

A

left to right.

92
Q

What is the expected outcome of working bottom-up from the leaves in a heap?

A

To exploit tree structure for faster heap construction.

93
Q

What is the requirement of a task management application using a priority queue?

A
  • Add new tasks * Find/delete task of highest priority * Increase the priority of task(s)
94
Q

What is the purpose of the MaxHeapify function?

A

To maintain the max-heap property of a subtree rooted at a given index.

95
Q

What is the time complexity of the MaxHeapify operation?

A

O(log n) due to the height of the heap.

96
Q

What is the local strategy used in heap operations?

97
Q

What is the global strategy used in building a max heap?

A

Bottom-up.

98
Q

What does the BuildMaxHeap function do?

A

It transforms an arbitrary array into a max-heap.

99
Q

What is the runtime complexity of the BuildMaxHeap function?

100
Q

What is a priority queue?

A

An abstract data type that manages elements where each element has a priority.

101
Q

What does the FindMax function do in a priority queue?

A

Returns the maximum element in the priority queue.

102
Q

What is the operation of ExtractMax in a priority queue?

A

Removes and returns the maximum element from the priority queue.

103
Q

What does the IncreaseKey operation do?

A

Increases the priority of a given element in the priority queue.

104
Q

What is the Insert operation in a priority queue?

A

Adds a new element with a specified priority to the priority queue.

105
Q

What is the main idea behind HeapSort?

A

To use a heap to sort an array by repeatedly extracting the maximum element.

106
Q

What is the first step in the HeapSort algorithm?

A

Build a max heap from the input array.

107
Q

What happens during the ExtractMax operation in HeapSort?

A

The largest element is removed and placed at the end of the array.

108
Q

What is the worst-case runtime of HeapSort?

A

O(n log n).

109
Q

What is the average runtime of HeapSort?

A

O(n log n).

110
Q

True or False: HeapSort is a stable sorting algorithm.

111
Q

Fill in the blank: The runtime of MaxHeapify depends on the __________ of the heap.

112
Q

What is the runtime complexity of the IncreaseKey operation?

113
Q

What is an elementary operation in the context of heaps?

A

A single swap or comparison during heap operations.

114
Q

What does the term ‘in situ’ mean regarding sorting algorithms?

A

The algorithm sorts the array without needing additional storage.

115
Q

What is a geometric series in relation to heap runtime analysis?

A

It is used to analyze the total cost of building the heap.

116
Q

What is the significance of the theorem stating a heap with n elements can be computed in __________ time?