0% found this document useful (0 votes)
58 views4 pages

MCSL-216 Q1& Q2

The document implements Huffman coding to compress data. It defines a Node structure for the Huffman tree and a priority queue to build the tree using frequencies. It then assigns Huffman codes to characters by traversing the tree and outputs the codes and average bits per character.

Uploaded by

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

MCSL-216 Q1& Q2

The document implements Huffman coding to compress data. It defines a Node structure for the Huffman tree and a priority queue to build the tree using frequencies. It then assigns Huffman codes to characters by traversing the tree and outputs the codes and average bits per character.

Uploaded by

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

#include <iostream>

using namespace std;

void swap(int& a, int& b) {


int temp = a;
a = b;
b = temp;
}

int partition(int arr[], int low, int high, int& comparisons, int& swaps) {
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j < high; j++) {


comparisons++;
if (arr[j] <= pivot) {
i++;
swap(arr[i], arr[j]); // Swap elements if arr[j] is less than or equal to the pivot.
swaps++;
}
}

swap(arr[i + 1], arr[high]); // Swap the pivot element into its correct position.
swaps++;
return (i + 1);
}

void quickSort(int arr[], int low, int high, int& comparisons, int& swaps) {
if (low < high) {
int pi = partition(arr, low, high, comparisons, swaps); // Get the pivot index.

// Recursively sort the subarrays to the left and right of the pivot.
quickSort(arr, low, pi - 1, comparisons, swaps);
quickSort(arr, pi + 1, high, comparisons, swaps);
}
}

int main() {
int elements1[] = {12, 20, 22, 16, 25, 18, 8, 10, 6, 15};
int elements2[] = {6, 8, 10, 12, 15, 16, 18, 20, 22, 25};
int n = sizeof(elements1) / sizeof(elements1[0]);

int comparisons1 = 0, swaps1 = 0;


int comparisons2 = 0, swaps2 = 0;

cout << "Dataset 1 (Unsorted): ";


for (int i = 0; i < n; i++) {
cout << elements1[i] << " ";
}
cout << endl;

quickSort(elements1, 0, n - 1, comparisons1, swaps1); // Sort Dataset 1.

cout << "Dataset 1 (Sorted): ";


for (int i = 0; i < n; i++) {
cout << elements1[i] << " ";
}
cout << endl;

cout << "Number of comparisons for Dataset 1: " << comparisons1 << endl;
cout << "Number of swaps for Dataset 1: " << swaps1 << endl;

cout << "Dataset 2 (Unsorted): ";


for (int i = 0; i < n; i++) {
cout << elements2[i] << " ";
}
cout << endl;

quickSort(elements2, 0, n - 1, comparisons2, swaps2); // Sort Dataset 2.

cout << "Dataset 2 (Sorted): ";


for (int i = 0; i < n; i++) {
cout << elements2[i] << " ";
}
cout << endl;

cout << "Number of comparisons for Dataset 2: " << comparisons2 << endl;
cout << "Number of swaps for Dataset 2: " << swaps2 << endl;

return 0;
}

[Running] cd "c:\Users\Preetam Kumar\Documents\preetam\IGNOU\Assignments\MCSL-216\Q1\" && g++


Quicksort.cpp -o Quicksort && "c:\Users\Preetam Kumar\Documents\preetam\IGNOU\Assignments\
MCSL-216\Q1\"Quicksort
Dataset 1 (Unsorted): 12 20 22 16 25 18 8 10 6 15
Dataset 1 (Sorted): 6 8 10 12 15 16 18 20 22 25
Number of comparisons for Dataset 1: 25
Number of swaps for Dataset 1: 19
Dataset 2 (Unsorted): 6 8 10 12 15 16 18 20 22 25
Dataset 2 (Sorted): 6 8 10 12 15 16 18 20 22 25
Number of comparisons for Dataset 2: 45
Number of swaps for Dataset 2: 54
#include <iostream>
#include <queue>
#include <map>
using namespace std;

// Define a structure to represent nodes in the Huffman tree.


struct Node {
char data;
int freq;
Node* left;
Node* right;

Node(char d, int f) {
data = d;
freq = f;
left = right = nullptr;
}
};

// Custom comparison function for the priority queue.


struct CompareNodes {
bool operator()(Node* left, Node* right) {
return left->freq > right->freq;
}
};

// Function to build the Huffman tree.


Node* buildHuffmanTree(char data[], int freq[], int n) {
priority_queue<Node*, vector<Node*>, CompareNodes> pq;
for (int i = 0; i < n; i++) {
Node* node = new Node(data[i], freq[i]);
pq.push(node);
}
while (pq.size() > 1) {
Node* left = pq.top();
pq.pop();
Node* right = pq.top();
pq.pop();

Node* parent = new Node('\0', left->freq + right->freq);


parent->left = left;
parent->right = right;

pq.push(parent);
}
return pq.top();
}
// Function to assign Huffman codes.
void assignHuffmanCodes(Node* root, string code, map<char, string>& huffmanCodes) {
if (!root) return;
if (root->data != '\0') {
huffmanCodes[root->data] = code;
}
assignHuffmanCodes(root->left, code + "0", huffmanCodes);
assignHuffmanCodes(root->right, code + "1", huffmanCodes);
}

int main() {
char data[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
int freq[] = {15, 25, 5, 7, 10, 13, 9};
int n = sizeof(data) / sizeof(data[0]);

Node* root = buildHuffmanTree(data, freq, n);

map<char, string> huffmanCodes;


assignHuffmanCodes(root, "", huffmanCodes);

cout << "Huffman Codes:" << endl;


for (int i = 0; i < n; i++) {
cout << data[i] << ": " << huffmanCodes[data[i]] << endl;
}
int totalBits = 0;
int totalFrequency = 0;
for (int i = 0; i < n; i++) {
totalBits += huffmanCodes[data[i]].size() * freq[i];
totalFrequency += freq[i];
}
double averageBitsPerCharacter = static_cast<double>(totalBits) / totalFrequency;
cout << "Average Bits per Character: " << averageBitsPerCharacter << endl;
return 0;
}

[Running] cd "c:\Users\Preetam Kumar\Documents\preetam\IGNOU\Assignments\MCSL-216\Q2\" && g++


tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\Preetam Kumar\Documents\preetam\IGNOU\
Assignments\MCSL-216\Q2\"tempCodeRunnerFile
Huffman Codes:
A: 00
B: 11
C: 1000
D: 1001
E: 011
F: 101
G: 010
Average Bits per Character: 2.66667

You might also like