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

Ahsan Naseer Lab Report 5

The document contains the code and output for 7 tasks related to sorting algorithms. In Task 1, bubble sort is implemented to sort an integer array. In Task 2, bubble sort is improved with swap function. Task 3 measures the time taken by selection sort using chrono. Task 4 compares the time taken by bubble and selection sort on a large array. Task 5 sorts arrays in descending order using bubble and selection sort. Task 6 sorts arrays using insertion sort and measures time. Task 7 implements a hybrid sort combining bubble and selection sort based on a threshold.

Uploaded by

cigila6437
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Ahsan Naseer Lab Report 5

The document contains the code and output for 7 tasks related to sorting algorithms. In Task 1, bubble sort is implemented to sort an integer array. In Task 2, bubble sort is improved with swap function. Task 3 measures the time taken by selection sort using chrono. Task 4 compares the time taken by bubble and selection sort on a large array. Task 5 sorts arrays in descending order using bubble and selection sort. Task 6 sorts arrays using insertion sort and measures time. Task 7 implements a hybrid sort combining bubble and selection sort based on a threshold.

Uploaded by

cigila6437
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

LAB REPORT 5

Name Ahsan Naseer

Reg. No. B22F0141SE082

Lab Data Structure and Algorithms

Date 16-11-2023

Task 1:
#include<iostream>

#include<ctime>

using namespace std;

int printArray(int arr[], int size)

cout<<"Before Bubble Sort: ";

for(int i=0; i<size; i++){

cout<<arr[i]<<" ";

}
cout<<"\n";

double bubble_sort(int Arr[], int size)

clock_t start= clock();

for(int i=1; i<size;i++)

for (int j=0; j<size-i; j++)

if(Arr[j]<Arr[j+1]){

int swap= Arr[j];

Arr[j]=Arr[j+1];

Arr[j+1]=swap;

clock_t end= clock();

return double(end - start)/CLOCKS_PER_SEC;

printArrayB(int Arr[], int size)

cout<<"After Sorting and Swapping in Descending Order: ";

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

cout<<Arr[i]<<" ";

int main()
{

int size= 7;

int Array[size]={3,2,9,11,24,56,23};

printArray(Array, size);

bubble_sort(Array, size);

printArrayB(Array, size);

cout<<"\nTime taken by algo: "<<bubble_sort(Array, size);

OUTPUT:

TASK 2:
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

void swap(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}

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


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

int main() {
// Test with a small array
const int smallArraySize = 5;
int smallArray[smallArraySize];
srand(time(nullptr));
for (int i = 0; i < smallArraySize; ++i) {
smallArray[i] = rand() % 100;
}

cout << "Original array: ";


printArray(smallArray, smallArraySize);
clock_t startTime = clock();
bubbleSort(smallArray, smallArraySize);
clock_t endTime = clock();

cout << "Sorted array: ";


printArray(smallArray, smallArraySize);

double elapsedTime = double(endTime - startTime) / CLOCKS_PER_SEC;


cout << "Time taken: " << elapsedTime << " seconds" << endl;

return 0;
}

OUTPUT:

TASK NO 3
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <chrono>

using namespace std;


using namespace chrono;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

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


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

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


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

int main() {
// Test with a small array
const int smallArraySize = 5;
int smallArray[smallArraySize];
// Fill the array with random values
srand(time(nullptr));
for (int i = 0; i < smallArraySize; ++i) {
smallArray[i] = rand() % 100;
}

cout << "Original array: ";


printArray(smallArray, smallArraySize);

// Measure the start time


auto startTime = high_resolution_clock::now();

// Sort the array using Selection Sort


selectionSort(smallArray, smallArraySize);

// Measure the end time


auto endTime = high_resolution_clock::now();

cout << "Sorted array: ";


printArray(smallArray, smallArraySize);

// Calculate and print the time taken in microseconds


auto elapsedTime = duration_cast<microseconds>(endTime - startTime).count();
cout << "Time taken: " << elapsedTime << " microseconds" << endl;

return 0;
}

TASK 4
#include <iostream>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <ctime>

using namespace std;


using namespace chrono;

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 generateRandomArray(int arr[], int n) {
srand(time(0));
for (int i = 0; i < n; ++i) {
arr[i] = rand();
}
}

int main() {
const int arraySize = 10000;
int bubbleArr[arraySize];
int selectionArr[arraySize];

generateRandomArray(bubbleArr, arraySize);
copy(begin(bubbleArr), end(bubbleArr), begin(selectionArr));

auto startBubble = high_resolution_clock::now();


bubbleSort(bubbleArr, arraySize);
auto endBubble = high_resolution_clock::now();
duration<double> bubbleTime = endBubble - startBubble;

auto startSelection = high_resolution_clock::now();


selectionSort(selectionArr, arraySize);
auto endSelection = high_resolution_clock::now();
duration<double> selectionTime = endSelection - startSelection;

cout << "Bubble Sort Time: " << bubbleTime.count() << " seconds\n";
cout << "Selection Sort Time: " << selectionTime.count() << " seconds\n";

return 0;
}

TASK 5
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <chrono>

using namespace std;


using namespace chrono;

void swap(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}

void bubbleSortDescending(int arr[], int size) {


for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] < arr[j + 1]) { // Change the comparison to sort in descending order
swap(arr[j], arr[j + 1]);
}
}
}
}

void selectionSortDescending(int arr[], int size) {


for (int i = 0; i < size - 1; ++i) {
int maxIndex = i;
for (int j = i + 1; j < size; ++j) {
if (arr[j] > arr[maxIndex]) { // Change the comparison to sort in descending order
maxIndex = j;
}
}
swap(arr[i], arr[maxIndex]);
}
}

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


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

int main() {
const int arraySizes[] = {100, 500, 1000, 5000, 10000};

for (int size : arraySizes) {


int *array = new int[size];

// Fill the array with random values


srand(time(nullptr));
for (int i = 0; i < size; ++i) {
array[i] = rand() % 1000;
}
// Measure the time for Bubble Sort
auto bubbleSortStartTime = high_resolution_clock::now();
bubbleSortDescending(array, size);
auto bubbleSortEndTime = high_resolution_clock::now();
auto bubbleSortElapsedTime = duration_cast<microseconds>(bubbleSortEndTime -
bubbleSortStartTime).count();

cout << "Bubble Sort (" << size << " elements): ";
cout << "Time taken: " << bubbleSortElapsedTime << " microseconds" << endl;

// Restore the array to its original unsorted state


srand(time(nullptr));
for (int i = 0; i < size; ++i) {
array[i] = rand() % 1000;
}

// Measure the time for Selection Sort


auto selectionSortStartTime = high_resolution_clock::now();
selectionSortDescending(array, size);
auto selectionSortEndTime = high

TASK 6

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <chrono>

using namespace std;


using namespace chrono;
void insertionSort(int arr[], int size) {
for (int i = 1; i < size; ++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 insertionSortDescending(int arr[], int size) {


for (int i = 1; i < size; ++i) {
int key = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] < key) { // Change the comparison to sort in descending order
arr[j + 1] = arr[j];
--j;
}

arr[j + 1] = key;
}
}

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


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

int main() {
const int arraySizes[] = {100, 500, 1000, 5000, 10000};

for (int size : arraySizes) {


int *array = new int[size];

// Fill the array with random values


srand(time(nullptr));
for (int i = 0; i < size; ++i) {
array[i] = rand() % 1000;
}

// Measure the time for original Insertion Sort


auto insertionSortStartTime = high_resolution_clock::now();
insertionSort(array, size);
auto insertionSortEndTime = high_resolution_clock::now();
auto insertionSortElapsedTime = duration_cast<microseconds>(insertionSortEndTime -
insertionSortStartTime).count();

cout << "Original Insertion Sort (" << size << " elements): ";

TASK 7:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <chrono>

using namespace std;


using namespace chrono;

void swap(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}

// Modified Bubble Sort with a threshold to switch to Selection Sort


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

// Switch to Selection Sort if array size is less than the threshold


if (size - i - 1 <= threshold) {
for (int k = size - i - 1; k > 0; --k) {
int maxIndex = 0;
for (int l = 1; l <= k; ++l) {
if (arr[l] > arr[maxIndex]) {
maxIndex = l;
}
}
swap(arr[maxIndex], arr[k]);
}
break;
}
}
}

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


for (int i = 0; i < size; ++i) {
cout

TASK 8
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

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 printArray(const int arr[], int n) {
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
const int arraySize = 10;
int randomArray[arraySize];

srand(time(0));
for (int i = 0; i < arraySize; ++i) {
randomArray[i] = rand() % 100; // Generates random integers between 0 and 99
}

cout << "Original Array: ";


printArray(randomArray, arraySize);

selectionSort(randomArray, arraySize);

cout << "Sorted Array: ";


printArray(randomArray, arraySize);

return 0;
}

You might also like