Professional Documents
Culture Documents
int partition(int arr[], int low, int high, int& comparisons, int& swaps) {
int pivot = arr[high];
int i = (low - 1);
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]);
cout << "Number of comparisons for Dataset 1: " << comparisons1 << endl;
cout << "Number of swaps for Dataset 1: " << swaps1 << endl;
cout << "Number of comparisons for Dataset 2: " << comparisons2 << endl;
cout << "Number of swaps for Dataset 2: " << swaps2 << endl;
return 0;
}
Node(char d, int f) {
data = d;
freq = f;
left = right = nullptr;
}
};
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]);