0% found this document useful (0 votes)
17 views5 pages

Ap 4

Uploaded by

Vaibhav Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Ap 4

Uploaded by

Vaibhav Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 1.

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

Problem 1 (Quick Sort)


1. Aim:
Choose some pivot element, p, and partition your unsorted array, arr, into three smaller
arrays: left, right, and equal, where each element in left p. and each element in equal = p.
Example arr = [5,7,4,3,8] In this challenge, the pivot will always be at arr[0], so the pivot is
5. arr is divided into left = {4,3). equal = {5}, and right = {7,8}. Putting them all together,
you get {4, 3, 5, 7, 8). There is a flexible checker that allows the elements of left and right to
be in any order. For example. (3, 4, 5, 8, 7} is valid as well.

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;

vector<int> quickSort(vector<int>& arr) {


int pivot = arr[0];
vector<int> left, equal, right;

for (int x : arr) {


if (x < pivot) {
left.push_back(x);
} else if (x == pivot) {
equal.push_back(x);
} else {
right.push_back(x);
}
}

// Concatenate left, equal, and right


left.insert(left.end(), equal.begin(), equal.end());
left.insert(left.end(), right.begin(), right.end());

return left;
}

int main() {
vector<int> arr = {4, 5, 3, 7, 2};
vector<int> sorted_arr = quickSort(arr);

for (int x : sorted_arr) {


cout << x << " ";
}

return 0;
}

5. Output:

6. Time Complexity: O(n)

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>

std::vector<int> missingNumbers(const std::vector<int>& arr, const std::vector<int>& brr) {


std::unordered_map<int, int> arr_count, brr_count;

// Count frequencies in arr


for (int num : arr) {
arr_count[num]++;
}

// Count frequencies in brr


for (int num : brr) {
brr_count[num]++;
}

// Find numbers missing or with different frequencies


std::vector<int> missing;
for (const auto& [num, count] : brr_count) {
if (arr_count[num] < count) {
missing.push_back(num);
}
}

// Sort missing numbers


std::sort(missing.begin(), missing.end());

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];
}

std::vector<int> result = missingNumbers(arr, brr);

for (int num : result) {


std::cout << num << " ";
}
std::cout << std::endl;

return 0;
}
4. Output:

5. Time Complexity: O(n + m + k log k)


6. Learning Outcomes:
• Learned about Quick Sort.
• Learned about vector, unordered_map header files.
• Learned different approaches to compare two arrays lists.
• Learned to read and handle input values, and new data.

You might also like