DSA POST LAB REPORT
DSA POST LAB REPORT
#include <iostream>
using namespace std;
delete[] L;
delete[] R;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
// Bubble Sort
int arrBubble[n];
copy(begin(arr), end(arr), begin(arrBubble));
bubbleSort(arrBubble, n);
cout << "Sorted array (Bubble Sort): ";
printArray(arrBubble, n);
// Selection Sort
int arrSelection[n];
copy(begin(arr), end(arr), begin(arrSelection));
selectionSort(arrSelection, n);
cout << "Sorted array (Selection Sort): ";
printArray(arrSelection, n);
// Insertion Sort
int arrInsertion[n];
copy(begin(arr), end(arr), begin(arrInsertion));
insertionSort(arrInsertion, n);
cout << "Sorted array (Insertion Sort): ";
printArray(arrInsertion, n);
// Merge Sort
int arrMerge[n];
copy(begin(arr), end(arr), begin(arrMerge));
mergeSort(arrMerge, 0, n - 1);
cout << "Sorted array (Merge Sort): ";
printArray(arrMerge, n);
return 0;
}
Task 2
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <chrono>
using namespace std;
int main() {
srand(static_cast<unsigned>(time(0)));
int sizes[] = {100, 1000, 10000, 100000, 1000000};
generateRandomArray(arrBubble, size);
copy(arrBubble, arrBubble + size, arrSelection);
copy(arrBubble, arrBubble + size, arrInsertion);
delete[] arrBubble;
delete[] arrSelection;
delete[] arrInsertion;
cout << endl;
}
return 0;
}
Task 3
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <algorithm>
using namespace std;
int main() {
srand(static_cast<unsigned>(time(0)));
int sizes[] = {100, 1000, 10000, 100000, 1000000};
delete[] arrRandom;
delete[] arrAscending;
delete[] arrDescending;
cout << endl;
}
return 0;
}
The comparison of running times for Bubble Sort, Selection Sort, and Insertion Sort reveals that
Insertion Sort shows the most variation based on the input structure. On random arrays, all three
algorithms exhibit similar performance, with Bubble Sort and Selection Sort being consistently
slower due to their \(O(n^2)\) complexity. However, Insertion Sort excels on ascending sorted
arrays, running in \(O(n)\) time, while it performs poorly on descending arrays, reverting to \
(O(n^2)\) behavior as every element must be repositioned. Conversely, Bubble Sort and Selection
Sort maintain their poor performance regardless of input structure, highlighting the significance of
input order in determining sorting efficiency. This emphasizes the need to choose sorting algorithms
based on expected data characteristics and size.