Ap 4
Ap 4
4
Student Name: Vaibhav Sharma UID: 22BCS12459
Branch: BE – CSE Section/Group: 22BCS_ML-901 - A
th
Semester: 5 Date of Performance: 05/08/24
Subject Name: Advanced Programming Lab-1
Subject Code: 22CSP-314
2. Objective:
Partition an array around a pivot element by dividing it into three sub-arrays: elements less
than the pivot, elements equal to the pivot, and elements greater than the pivot, and then
combine these sub-arrays into a single sorted array.
3. Algorithm:
• Initialize Vectors: Create three vectors: left, equal, and right to store elements less than,
equal to, and greater than the pivot, respectively.
• Iterate Through the Array: Loop through each element in arr: If the element is less
than the pivot, add it to left. If the element is equal to the pivot, add it to equal. If the
element is greater than the pivot, add it to right.
• Combine Results: Concatenate the left, equal, and right vectors into a single result
• Return the Result: Return the combined vector as the final partitioned array.
4. Source Code:
#include <iostream>
#include <vector>
using namespace std;
return left;
}
int main() {
vector<int> arr = {4, 5, 3, 7, 2};
vector<int> sorted_arr = quickSort(arr);
return 0;
}
5. Output:
Problem 2 ()
a. Aim:
To identify which elements in the second array (original list) are missing from the first array
(modified list) based on frequency discrepancies, and return these missing numbers sorted
in ascending order.
b. Objective:
To implement a function that identifies and returns a sorted list of unique integers from the
second array (brr) whose frequency in the second array is greater than its frequency in the
first array (arr). The function should handle discrepancies in frequencies and ensure that each
missing number is included only once in the final sorted list.
3. Algorithm:
• Count Frequencies: Use two frequency counters to count the occurrences of each
number in both arrays.
• Compare Frequencies: Compare the frequency of each number in the second array
with the frequency in the first array.
• Identify Missing Numbers: Collect numbers where the frequency in the second array
is greater than in the first array.
• Sort and Return: Sort the collected numbers in ascending order and return them.
3. Source Code:
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
return missing;
}
int main() {
int n, m;
std::cin >> n;
std::vector<int> arr(n);
for (int i = 0; i < n; ++i) {
std::cin >> arr[i];
}
std::cin >> m;
std::vector<int> brr(m);
for (int i = 0; i < m; ++i) {
std::cin >> brr[i];
}
return 0;
}
4. Output: