0% found this document useful (0 votes)
6 views

DSA POST LAB REPORT

Uploaded by

ahtisham0100
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DSA POST LAB REPORT

Uploaded by

ahtisham0100
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Task 1

#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}

void selectionSort(int arr[], int n) {


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;
}
}
swap(arr[i], arr[minIndex]);
}
}

void insertionSort(int arr[], int n) {


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;
}
}

void merge(int arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
int* L = new int[n1];
int* R = new int[n2];

for (int i = 0; i < n1; i++)


L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}

delete[] L;
delete[] R;
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

void printArray(int arr[], int n) {


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

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;

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}

void selectionSort(int arr[], int n) {


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;
}
}
swap(arr[i], arr[minIndex]);
}
}

void insertionSort(int arr[], int n) {


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;
}
}

void generateRandomArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
arr[i] = rand() % 100 + 1;
}
}

void measureSortTime(void (*sortFunc)(int[], int), int arr[], int n) {


auto start = chrono::high_resolution_clock::now();
sortFunc(arr, n);
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> duration = end - start;
cout << "Time taken: " << duration.count() << " seconds" << endl;
}

int main() {
srand(static_cast<unsigned>(time(0)));
int sizes[] = {100, 1000, 10000, 100000, 1000000};

for (int size : sizes) {


cout << "Array size: " << size << endl;

int* arrBubble = new int[size];


int* arrSelection = new int[size];
int* arrInsertion = new int[size];

generateRandomArray(arrBubble, size);
copy(arrBubble, arrBubble + size, arrSelection);
copy(arrBubble, arrBubble + size, arrInsertion);

cout << "Bubble Sort: ";


measureSortTime(bubbleSort, arrBubble, size);

cout << "Selection Sort: ";


measureSortTime(selectionSort, arrSelection, size);

cout << "Insertion Sort: ";


measureSortTime(insertionSort, arrInsertion, size);

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;

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}

void selectionSort(int arr[], int n) {


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;
}
}
swap(arr[i], arr[minIndex]);
}
}

void insertionSort(int arr[], int n) {


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;
}
}

void generateRandomArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
arr[i] = rand() % 100 + 1;
}
}

void measureSortTime(void (*sortFunc)(int[], int), int arr[], int n) {


auto start = chrono::high_resolution_clock::now();
sortFunc(arr, n);
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> duration = end - start;
cout << "Time taken: " << duration.count() << " seconds" << endl;
}

int main() {
srand(static_cast<unsigned>(time(0)));
int sizes[] = {100, 1000, 10000, 100000, 1000000};

for (int size : sizes) {


cout << "Array size: " << size << endl;
int* arrRandom = new int[size];
generateRandomArray(arrRandom, size);

int* arrAscending = new int[size];


int* arrDescending = new int[size];
copy(arrRandom, arrRandom + size, arrAscending);
sort(arrAscending, arrAscending + size);
copy(arrAscending, arrAscending + size, arrDescending);
reverse(arrDescending, arrDescending + size);

cout << "Bubble Sort on Random: ";


measureSortTime(bubbleSort, arrRandom, size);

cout << "Selection Sort on Random: ";


measureSortTime(selectionSort, arrRandom, size);

cout << "Insertion Sort on Random: ";


measureSortTime(insertionSort, arrRandom, size);

cout << "Bubble Sort on Ascending: ";


measureSortTime(bubbleSort, arrAscending, size);

cout << "Selection Sort on Ascending: ";


measureSortTime(selectionSort, arrAscending, size);

cout << "Insertion Sort on Ascending: ";


measureSortTime(insertionSort, arrAscending, size);

cout << "Bubble Sort on Descending: ";


measureSortTime(bubbleSort, arrDescending, size);

cout << "Selection Sort on Descending: ";


measureSortTime(selectionSort, arrDescending, size);

cout << "Insertion Sort on Descending: ";


measureSortTime(insertionSort, arrDescending, size);

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.

You might also like