ITA Unit 2 Answer
ITA Unit 2 Answer
Unit II - Divide-and-Conquer
Divide and conquer methodology – Merge sort – Quick sort – Binary search –
Multiplication of Large Integers – Strassen’s Matrix Multiplication.
Course Outcome – CO2: Design algorithms to solve problems using divide and
conquer approach.
PART – A
Q. No Question
1 What role does recursion play in the Merge Sort algorithm?
Recursion in Merge Sort:
• It divides the Aay into two halves recursively until each subArray has
one element.
• Then, it merges the sorted subArrays back into a single sorted Array.
• This ensures an efficient sorting process with a time complexity of
O(n log n).
Given the Array [3, 8, 5, 2, 7, 6, 4] and pivot = 4, determine the position of the
2.
pivot after one partitioning step.
Pivot in Quick Sort:
• The given Aay is [3, 8, 5, 2, 7, 6, 4] with pivot = 4.
• After partitioning, elements smaller than 4 move left, and larger ones
move right.
• [3, 2, 5, 8, 7, 6, 4]. [3, 2, 4, 8, 7, 6, 5].
• The pivot 4 ends up at index 2 with the Array as [3, 2, 4, 8, 7, 6, 5].
State the time and space complexity of Quick Sort in the best case and the
3
worst case.
Complexity of Quick Sort:
• Best case occurs when the pivot divides the Aay evenly, giving O(n log
n) complexity.
• Worst case happens when the pivot is always the smallest or largest,
leading to O(n²) complexity.
• Choosing a good pivot (like median-of-three) helps improve
efficiency.
At which step will the binary search find the key 23 in the sorted Array [12,
18, 23, 34, 45, 56, 67].
4
Binary Search (Key = 23):
• The sorted Array is [12, 18, 23, 34, 45, 56, 67].
• First, mid = 34 (index 3), so search moves to the left half.
• Second, mid = 23 (index 2), so key 23 is found in step 2.
Can Binary Search be applied to a dynamic Array whose size changes during
5
execution? Why or why not?
Binary Search on Dynamic Arrays:
• Binary search requires a sorted Array for efficient searching.
• If elements are inserted or deleted dynamically, sorting must be done
again.
• Thus, applying binary search directly on a changing Array is
inefficient.
PART – B
Q.No Question
Write merge sort algorithm and implement it to sort the following Array of
integers in ascending order:
5, 2, 8, 3, 1, 6, 4
Merge Sort:
Merge sort is a sorting algorithm that follows the divide-and-
conquer approach. It works by recursively dividing the input Array into
smaller subArrays and sorting those subAarrys then merging them back
together to obtain the sorted Array.
The process of merge sort is to divide the Aay into two halves, sort each half,
and then merge the sorted halves back together. This process is repeated
1 i)
until the entire Array is sorted.
A[] = {5, 2, 8, 3, 1, 6, 4}
Q.No Question
Split the Array :
Merge {2, 5} and {3, 8} → {2, 3, 5, 8} Merge {1, 6} and {4} → {1, 4, 6}
[5, 2, 8, 3, 1, 6, 4]
/ \
[5, 2, 8, 3] [1, 6, 4]
/ \ / \
[5, 2] [8, 3] [1, 6] [4]
/ \ / \ / \
[5] [2] [8] [3] [1] [6]
\ / \ / \ /
[2, 5] [3, 8] [1, 6]
\ / \
[2, 3, 5, 8] [1, 4, 6]
\ /
[1, 2, 3, 4, 5, 6, 8]
Q.No Question
Write about the principle behind the Quick Sort, a Divide and Conquer
algorithm, to sort an Aay, A[] in ascending order. Given an Array, A[], with
starting index low and ending index high, Write the functions partition()
and quickSort(). Use the First element as the pivot so that all elements less
than or equal to the pivot come before it, and elements greater than the pivot
follow it.
QuickSort is a sorting algorithm based on the Divide and Conquer that picks
an element as a pivot and partitions the given Array around the picked pivot
by placing the pivot in its correct position in the sorted Array.
It is based on positioning the element in its sorted position. An element is in
sorted position if all the elements to left of it is less and all the elements to
right of it is greater than pivot element.
QuickSort works on the principle of divide and conquer, breaking down the
problem into smaller sub-problems.
if (i < j) {
// Swap elements to correct position
swap(&A[i], &A[j]);
}
}
// Swap pivot element
swap(&A[l], &A[j]);
3 -3 5 2 6 8 -6 1
Pivot j
i
L H
i j
3 -3 1 2 6 8 -6 5
i
i j
3 -3 1 2 -6 8 6 5
i
i j
j i
-6 -3 1 2 3 8 6 5
8 6 5
-6 -3 1 2
Piv H
Piv H i
i l j
j
i
j
5 6 8
-6 -3 1 2
QuickSort is a sorting algorithm based on the Divide and Conquer that picks
an element as a pivot and partitions the given Array around the picked pivot
by placing the pivot in its correct position in the sorted Array.
It is based on positioning the element in its sorted position. An element is in
3 i) sorted position if all the elements to left of it is less and all the elements to
right of it is greater than pivot element.
QuickSort works on the principle of divide and conquer, breaking down the
problem into smaller sub-problems.
if (i < j) {
// Swap elements to correct position
swap(&A[i], &A[j]);
}
}
// Swap pivot element
swap(&A[low], &A[j]);
e d c a b f
Piv i j
l h
i
i
i
i
j
b d c a e f
Q.No Question
b d c a
piv i j
b a c d
j>i swap
j i
pivot
a b c d
Modify the binary search algorithm to find the position of the first
occurrence of a value that may appear more than once in the ordered list.
Check that your algorithm is still O(log n). Find the index of the first
occurrence of the target element (2) using binary search:
1, 2, 2, 2, 3, 4, 5
Also Find the total number of occurrences of a target element in a sorted
Array using Binary Search.
1, 2, 2, 2, 3, 4, 5
First occurrence index=2
Index 0 1 2 3 4 5 6
element 1 2 2 2 3 4 5
Mid
low high
Res=3
low high
Mid
Res=1
5 i)
Q.No Question
Q.No Question
PART – C
Q. No Question
A logistics company is organizing a shipment of packages to different cities.
Each package has a unique delivery priority score, and the company wants
to Aange the packages in descending order of priority for efficient delivery.
The number of packages is large, and the system must process the sorting
operation with minimal computational overhead.
Problem:
Given a large dataset of package priority scores, sort the scores . recommend
a suitable sort.
Objective:
1 i)
Implement the sort to efficiently sort the packages' priority scores in
descending order and analyze its performance in terms of time complexity.
packages_priority_scores = [56, 78, 45, 90, 12, 65, 83]
output : [90, 83, 78, 65, 56, 45, 12]
Implementation
#include <stdio.h>
if (i < j) {
swap(&A[i], &A[j]); // Swap elements to correct position
}
}
// Main function
int main() {
int scores[] = {56, 78, 45, 90, 12, 65, 83};
int n = sizeof(scores) / sizeof(scores[0]);
printf("Original Array:\n");
printArray(scores, n);
return 0;
}
Learning Resources:https://www.techinterviewhandbook.org/algorithms/sorting-
searching/