Assignment No-1
Assignment No-1
Write programs in C/C++/ Java to sort a list of n numbers in ascending order using
selection sort, insertion sort, heap sort, radix sort. Determine the time required to sort
and compare on basis of time complexity for different values of n.
→
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm> // for std::swap, std::max_element
#include <cmath> // for log10
// Selection Sort
void selectionSort(std::vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
std::swap(arr[i], arr[minIndex]);
}
}
// Insertion Sort
void insertionSort(std::vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
}
// Heap Sort
void heapify(std::vector<int>& arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
std::swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(std::vector<int>& arr) {
int n = arr.size();
// Build the heap
for (int i = n / 2 - 1; i >= 0; --i) {
heapify(arr, n, i);
}
// Sort by extracting elements from the heap
for (int i = n - 1; i >= 0; --i) {
std::swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
// Radix Sort
void countSort(std::vector<int>& arr, int exp) {
int n = arr.size();
std::vector<int> output(n);
int count[10] = {0};
// Counting occurrences of digits at current exp
for (int i = 0; i < n; ++i) {
int index = (arr[i] / exp) % 10;
count[index]++;
}
// Accumulate counts
for (int i = 1; i < 10; ++i) {
count[i] += count[i - 1];
}
// Build the output array using the accumulated counts
for (int i = n - 1; i >= 0; --i) {
int index = (arr[i] / exp) % 10;
output[--count[index]] = arr[i];
}
// Copy output array back to original array
for (int i = 0; i < n; ++i) {
arr[i] = output[i];
}
}
void radixSort(std::vector<int>& arr) {
int maxVal = *std::max_element(arr.begin(), arr.end());
for (int exp = 1; maxVal / exp > 0; exp *= 10) {
countSort(arr, exp);
}
}
// Helper Functions
void fillWithRandom(std::vector<int>& arr, int range) {
for (int& num : arr) {
num = std::rand() % range;
}
}
void testSort(void (*sortFunction)(std::vector<int>&), const std::string& sortName, int n, int range) {
std::vector<int> arr(n);
fillWithRandom(arr, range);
clock_t start = clock();
sortFunction(arr);
clock_t end = clock();
double timeTaken = double(end - start) / CLOCKS_PER_SEC;
std::cout << "Time taken for " << sortName << " with " << n << " elements: "
<< timeTaken << " seconds." << std::endl;
}
int main() {
std::srand(std::time(nullptr)); // Seed for random generator
int range = 10000; // Range for random numbers
// Test with different values of `n`
std::vector<int> testValues = {1000, 5000, 10000};
std::cout << "Testing selection sort:\n";
for (int n : testValues) {
testSort(selectionSort, "selection sort", n, range);
}
std::cout << "\nTesting insertion sort:\n";
for (int n : testValues) {
testSort(insertionSort, "insertion sort", n, range);
}
std::cout << "\nTesting heap sort:\n";
for (int n : testValues) {
testSort(heapSort, "heap sort", n, range);
}
std::cout << "\nTesting radix sort:\n";
for (int n : testValues) {
testSort(radixSort, "radix sort", n, range); }
return 0;
}
2. Write a program in C/C++/ Java to implement Strassen‟s Matrix multiplication
→
#include <iostream>
#include <vector>
#include <cmath>
if (n == 1) {
return {{A[0][0] * B[0][0]}};
}
return 0;
}
3. Write a program in C/C++/ Java to from a given vertex in a weighted connected
graph, find shortest paths to other vertices using Dijikstra‟s algorithm
→
#include <iostream>
#include <vector>
#include <queue>
#include <limits> // for std::numeric_limits
#include <utility> // for std::pair
// Dijkstra's Algorithm
std::vector<int> dijkstra(const AdjacencyList& graph, int source) {
int n = graph.size();
std::vector<int> distances(n, std::numeric_limits<int>::max());
std::vector<bool> visited(n, false);
distances[source] = 0;
pq.push({0, source});
while (!pq.empty()) {
int currentDistance = pq.top().first;
int currentVertex = pq.top().second;
pq.pop();
if (visited[currentVertex]) {
continue;
}
visited[currentVertex] = true;
return distances;
}
std::cout << "Shortest paths from vertex " << sourceVertex << ":\n";
for (int i = 0; i < n; ++i) {
std::cout << "To vertex " << i << ": " << shortestPaths[i] << std::endl;
}
return 0;
}
4. Write a program in C/C++/ Java to implement Knapsack problems using Greedy
method
→
#include <iostream>
#include <vector>
#include <algorithm>
return totalValue;
}
// Main function to demonstrate the Fractional Knapsack with the Greedy method
int main() {
// Example items with weight and value
std::vector<Item> items = {
{10, 60}, // 10 kg, 60 value
{20, 100}, // 20 kg, 100 value
{30, 120}, // 30 kg, 120 value
};
std::cout << "Maximum value achievable with a capacity of " << capacity << " is " << maxValue << "."
<< std::endl;
return 0;
}
5. Write a program in C/C++/ Java to implement optimal binary search tree and also
calculate the best case and worst case complexity.
→
#include <iostream>
#include <vector>
#include <limits>
#include <iomanip> // for std::setw
std::vector<std::string> bst;
optimalBST(keys, probabilities, frequencies, bst);
return 0;
}