Week 11- Sorting Flashcards

1
Q

What is the Divide and Conquer (D&C) paradigm in algorithm design?

A

Divide and Conquer (D&C) is an algorithm design paradigm that recursively breaks a problem down into smaller, more manageable sub-problems of the same type, solves each sub-problem, and then combines the results.

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

What is the precondition for applying the Divide and Conquer approach effectively?

A

The precondition is often that the data is sorted. This helps the algorithm break down problems more efficiently and combine solutions, as seen in algorithms like Merge Sort or Binary Search.

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

How does the Divide and Conquer strategy work?

A

Divide: Split the original problem into smaller sub-problems.

Conquer: Solve each sub-problem recursively.

Combine: Merge the solutions of the sub-problems to form the solution to the original problem.

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

Why is Divide and Conquer effective for sorted data?

A

Divide and Conquer works efficiently on sorted data because it allows algorithms to split the problem space (such as searching or sorting) in a way that minimizes unnecessary work, leveraging the sorted order to reduce complexity (e.g., Binary Search or Merge Sort).

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

What is Quicksort?

A

Quicksort is a Divide and Conquer algorithm developed by Tony Hoare in 1961. It efficiently sorts an array by recursively partitioning it into smaller sub-arrays.

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

What is the first step of the Quicksort algorithm?

A

Select an element from the array, called the Pivot. The pivot is used to partition the array into smaller sub-arrays.

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

What happens in Step 2 of the Quicksort algorithm?

A

Partition the array by moving all elements smaller than the Pivot to the left of the Pivot, and all elements greater than the Pivot to the right. This creates two sub-arrays.

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

What is the final step of the Quicksort algorithm?

A

Apply Step 1 and Step 2 recursively to each sub-array (left and right) until the entire array is sorted.

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

What is Heapsort?

A

Heapsort is a comparison-based sorting algorithm developed by John William Joseph Williams in 1964. It sorts an array by using a binary heap data structure, specifically a Max Heap or Min Heap.

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

What are the key concepts required to understand Heapsort?

A

The key concepts required for Heapsort include:

  • Priority Queue
  • Complete Balanced Tree
  • Heap
  • Heapify
  • Build a Max Heap
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a Priority Queue (PQ)?

A

A Priority Queue (PQ) is an abstract data type where each element has a priority associated with it. Elements are processed based on their priority rather than their insertion order.

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

How is the priority of an element in a Priority Queue (PQ) determined?

A

The priority is determined by the value of the element. For example, in a Max Priority Queue, the largest value has the highest priority, while in a Min Priority Queue, the smallest value has the highest priority.

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

What are the three key operations that a Priority Queue (PQ) must support?

A

A Priority Queue (PQ) must support at least three operations:

is_empty: Check if the queue is empty.

insert: Add an element with an associated priority.

pull: Remove and return the element with the highest priority from the queue.

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

What does the is_empty operation do in a Priority Queue (PQ)?

A

The is_empty operation checks whether the Priority Queue (PQ) is empty and returns true if no elements are in the queue, or false if there are elements.

17
Q

What does the insert operation do in a Priority Queue (PQ)?

A

The insert operation adds an element to the Priority Queue (PQ) and assigns it a priority. The queue maintains the priority order based on the element’s value.

18
Q

What does the pull operation do in a Priority Queue (PQ)?

A

The pull operation removes and returns the element with the highest priority in the Priority Queue (PQ), which is either the largest or smallest element depending on the type of priority queue (Max or Min).

19
Q

What is a Complete Binary Tree?

A

A Complete Binary Tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

20
Q

What are the key properties of a Complete Binary Tree?

A

The key properties of a Complete Binary Tree are:

All levels, except possibly the last, are completely filled.

Nodes at the last level are filled from left to right, without any gaps.

21
Q

How does a Complete Binary Tree differ from a regular binary tree?

A

In a Complete Binary Tree, every level must be filled from left to right, while in a regular binary tree, there can be any arrangement of nodes at any level, including gaps.

22
Q

What is the significance of a Complete Binary Tree in data structures like heaps?

A

Complete Binary Trees are used in heaps because their structure ensures efficient operations (like insertion and removal of the root) with a predictable tree shape, ensuring O(log n) time complexity for key operations.

23
Q

What is a Binary Heap?

A

A Binary Heap is a Complete Binary Tree that satisfies the heap property (also called heap invariant). It is used to efficiently implement priority queues.

24
Q

What is the heap property in a Binary Heap?

A

The heap property states that the key/value stored in each node is either:

Greater than or equal to (>=) the keys/values in its children (Max-Heap).

Less than or equal to (<=) the keys/values in its children (Min-Heap).

25
What is the difference between a Max-Heap and a Min-Heap?
- Max-Heap: In a Max-Heap, the key of each node is greater than or equal to its children, ensuring the largest element is at the root. - Min-Heap: In a Min-Heap, the key of each node is less than or equal to its children, ensuring the smallest element is at the root.
26
Why is a Binary Heap useful in data structures like priority queues?
A Binary Heap provides an efficient way to implement a priority queue, where the largest (or smallest) element can be accessed quickly, and operations like insertion and deletion of the root can be done in O(log n) time.
27
What is the first step in building a Max Heap?
Find the last parent node in the tree and its children. The last parent node is the node just before the leaves of the tree, i.e., the last non-leaf node.
28
What is the second step in building a Max Heap?
Within the subtree rooted at this parent node, find the largest element and swap it with the root node of the subtree. After the swap, repeat the process on the swapped node to maintain the heap property.
29
What is the final step in building a Max Heap?
Repeat Step 1 and Step 2 for each parent node, working upwards from the last parent node to the root of the tree. This ensures the entire tree satisfies the Max Heap property.
30
Why is the process of building a Max Heap efficient?
The process works efficiently because starting from the last parent node ensures each swap is done in a way that minimizes reworking the already-heapified subtrees, resulting in a time complexity of O(n) for building the heap.
31
What is the first step in the Heapify process?
Start from the root node of the tree. This is the node that needs to be heapified to maintain the heap property.
32
What should be done if the right child index is smaller than or equal to the size of the tree during Heapify?
If the right child index is valid (i.e., smaller than or equal to the size of the tree), compare the values of the left child and right child. Swap the larger of the two children with the root node if its value is greater than the root node.
33
What happens after swapping during the Heapify process?
After swapping, repeat the Heapify process on the swapped node. This ensures the heap property is maintained for the entire tree, not just the root.
34
How can you build a Max Heap structure using Heapify?
To build a Max Heap, apply the Heapify process reversely starting from the last parent node to the root. This ensures each subtree satisfies the heap property, ultimately resulting in a Max Heap.
35
What is the first step in the Heapsort algorithm?
Swap the root node with the last node in the tree (or array). This places the largest element at the root.
36
What is the second step in Heapsort after swapping the root node with the last node?
"Cut" (remove) the last node (which was swapped) and place it in the last empty space of the array. This effectively reduces the heap size by 1.
37
What is the third step in the Heapsort algorithm?
Heapify the tree. This re-establishes the heap property after removing the largest element and reducing the heap size.
38