Daa Assignments in Word

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

GROUP - A

CODE:COR13P/01: Write a C++ program to implement Binary Search?

#include <iostream>
using namespace std;

class BinarySearch {
private:
int* arr;
int size;
int comparisons;
public:
BinarySearch() : arr(nullptr), size(0), comparisons(0) {}
~BinarySearch() {
delete[] arr;
}
void inputArray() {
cout << "Enter the number of elements in the array: ";
cin >> size;
arr = new int[size];
cout << "Enter the elements of the array (sorted):\n";
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}
}
int binarySearch(int left, int right, int target) {
while (left <= right) {
int mid = left + (right - left) / 2;
++comparisons;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // Target not found
}
void searchElement() {
int target;
cout << "Enter the element to search: ";
cin >> target;
int result = binarySearch(0, size - 1, target);
if (result != -1) {
cout << "Element found at index: " << result << "\n";
} else {
cout << "Element not found in the array.\n";
}
cout << "Number of comparisons made: " << comparisons << "\n";
}
};

int main() {
BinarySearch bs;
bs.inputArray();
bs.searchElement();
return 0;
}

OUTPUT:
CODE:COR13P/02: Write a C++ program to implement Insertion Sort?

#include <iostream>
using namespace std;

class InsertionSort {
private:
int* array;
int size;
int comparisonCount;
public:
// Constructor to initialize the array and comparison count
InsertionSort(int arr[], int n) {
size = n;
array = new int[size];
for (int i = 0; i < size; ++i) {
array[i] = arr[i];
}
comparisonCount = 0;
}
// Destructor to deallocate the dynamically allocated array
~InsertionSort() {
delete[] array;
}
// Function to perform Insertion Sort
void sort() {
for (int i = 1; i < size; ++i) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1;
comparisonCount++; // Increment comparison count
}
array[j + 1] = key;
comparisonCount++; // Increment for the final comparison where the condition fails
}
}
void displayArray() const {
for (int i = 0; i < size; ++i) {
cout << array[i] << " ";
}
cout << endl;
}
int getComparisonCount() const {
return comparisonCount;
}
};
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
// Dynamically allocating the array
int* arr = new int[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
// Creating an object of InsertionSort class
InsertionSort sorter(arr, n);
// Performing the sorting operation
sorter.sort();
cout << "Sorted array: ";
sorter.displayArray();
cout << "Number of comparisons: " << sorter.getComparisonCount() << endl;
// Deallocating the dynamically allocated array
delete[] arr;
return 0;
}

OUTPUT:
CODE:COR13P/03: Write a C++ program to implement Quick Sort?

#include <iostream>
using namespace std;

class QuickSort {
private:
int* arr;
int size;
int comparisons;
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int partition(int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; ++j) {
++comparisons;
if (arr[j] < pivot) {
++i;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return (i + 1);
}
void quickSort(int low, int high) {
if (low < high) {
int pi = partition(low, high);
quickSort(low, pi - 1);
quickSort(pi + 1, high);
}
}
public:
QuickSort(int arr[], int size) : arr(arr), size(size), comparisons(0) {}
void sort() {
quickSort(0, size - 1);
}
void display() {
cout << "Sorted array: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
int getComparisons() {
return comparisons;
}
};
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int* arr = new int[size];
cout << "Enter the elements of the array: ";
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}
QuickSort qs(arr, size);
qs.sort();
qs.display();
cout << "Number of comparisons: " << qs.getComparisons() << endl;
delete[] arr;
return 0;
}

OUTPUT:
CODE:COR13P/04: Write a C++ program to implement Merge Sort?

#include <iostream>
using namespace std;

class MergeSort {
private:
int *arr; int size; int comparisonCount;
public:
MergeSort(int *a, int s)
arr = a; size = s; comparisonCount = 0;
// Merge two subarrays of arr[], First subarray is arr[l..m], Second subarray is arr[m+1..r]
void merge(int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
// Copy data to temporary arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
// Merge the temporary arrays back into arr[l..r]
i = 0; j = 0; k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
comparisonCount++; // Counting the comparison
k++;
}
while (i < n1) {
arr[k] = L[i];
i++; k++;
}
while (j < n2) {
arr[k] = R[j];
j++; k++;
}
}
void mergeSort(int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(l, m);
mergeSort(m + 1, r);
merge(l, m, r);
}
}
void display() {
cout << "Sorted array:";
for (int i = 0; i < size; i++)
cout << " " << arr[i];
cout << "\nNumber of comparisons: " << comparisonCount << endl;
}
};

int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
MergeSort ms(arr, n);
ms.mergeSort(0, n - 1);
ms.display();
return 0;
}

OUTPUT:
CODE:COR13P/05: Write a C++ program to implement Heap Sort?

#include <iostream>
using namespace std;

// Class to implement Heap Sort and count comparisons


class HeapSort {
private:
int comparisons;
// Function to heapify a subtree rooted with node i which is an index in arr[]
void heapify(int arr[], int n, int i) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left = 2*i + 1
int right = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (left < n && arr[left] > arr[largest])
largest = left;
// If right child is larger than largest so far
if (right < n && arr[right] > arr[largest])
largest = right;
// If largest is not root
if (largest != i) {
swap(arr[i], arr[largest]);
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
comparisons++;
}
}
// Main function to do heap sort
void heapSort(int arr[], int n) {
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
// Move current root to end
swap(arr[0], arr[i]);
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
public:
HeapSort() : comparisons(0) {}
// Function to perform heap sort and return number of comparisons
int sort(int arr[], int n) {
heapSort(arr, n);
return comparisons;
}
};

int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
// Create an object of HeapSort class
HeapSort heapSort;
// Perform heap sort and get number of comparisons
int comparisons = heapSort.sort(arr, n);
cout << "\nSorted array using Heap Sort:";
for (int i = 0; i < n; ++i) {
cout << " " << arr[i];
}
cout << endl;
cout << "Number of comparisons: " << comparisons << endl;
return 0;
}

OUTPUT:
CODE:COR13P/06: Write a C++ program to implement Count Sort?

#include <iostream>
using namespace std;

class CountSort {
private:
int *arr;
int size;
public:
CountSort(int n) {
size = n;
arr = new int[size];
}
~CountSort() {
delete[] arr;
}
void inputArray() {
cout << "Enter " << size << " elements:";
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}
}
void countSort() {
if (size <= 1) return;
// Find the maximum element in the array
int max = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] > max) {
max = arr[i];
}
}
// Create a count array to store the count of each unique element
int *count = new int[max + 1]();
for (int i = 0; i < size; ++i) {
count[arr[i]]++;
}
// Modify the count array to store the cumulative counts
for (int i = 1; i <= max; ++i) {
count[i] += count[i - 1];
}
// Create a temporary array to store the sorted output
int *output = new int[size];
for (int i = size - 1; i >= 0; --i) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
// Copy the sorted elements back to the original array
for (int i = 0; i < size; ++i) {
arr[i] = output[i];
}
// Free memory
delete[] count;
delete[] output;
}
void displayArray() {
cout << "Sorted array using Count Sort: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << "\n";
}
};

int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
CountSort cs(n);
cs.inputArray();
cs.countSort();
cs.displayArray();
return 0;
}
OUTPUT:
CODE:COR13P/07: Write a C++ program to implement Radix Sort?

#include <iostream>
using namespace std;

class RadixSort {
private:
int *arr;
int size;
public:
RadixSort(int n) {
size = n;
arr = new int[size];
}
void inputArray() {
cout << "Enter " << size << " elements:" << endl;
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}
}
void radixSort() {
// Find the maximum number to know number of digits
int max = getMax();
// Do counting sort for every digit. exp is 10^i where i is the current digit number
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSort(exp);
}
}
void countingSort(int exp) {
int output[size]; // output array
int count[10] = {0};
// Store count of occurrences in count[]
for (int i = 0; i < size; i++)
count[(arr[i] / exp) % 10]++;
// Change count[i] so that count[i] now contains actual position of this digit in output[]
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
// Build the output array
for (int i = size - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
// Copy the output array to arr[], so that arr[] contains sorted numbers according to current digit
for (int i = 0; i < size; i++)
arr[i] = output[i];
}
void display() {
cout << "Sorted array using Radix Sort: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
~RadixSort() {
delete[] arr;
}
private:
int getMax() {
int max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
};
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
RadixSort rs(n);
rs.inputArray();
rs.radixSort();
rs.display();
return 0;
}
OUTPUT:

You might also like