Ahsan Naseer Lab Report 5
Ahsan Naseer Lab Report 5
Date 16-11-2023
Task 1:
#include<iostream>
#include<ctime>
cout<<arr[i]<<" ";
}
cout<<"\n";
if(Arr[j]<Arr[j+1]){
Arr[j]=Arr[j+1];
Arr[j+1]=swap;
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);
OUTPUT:
TASK 2:
#include <iostream>
#include <ctime>
#include <cstdlib>
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;
}
return 0;
}
OUTPUT:
TASK NO 3
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <chrono>
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;
}
return 0;
}
TASK 4
#include <iostream>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <ctime>
int main() {
const int arraySize = 10000;
int bubbleArr[arraySize];
int selectionArr[arraySize];
generateRandomArray(bubbleArr, arraySize);
copy(begin(bubbleArr), end(bubbleArr), begin(selectionArr));
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>
int main() {
const int arraySizes[] = {100, 500, 1000, 5000, 10000};
cout << "Bubble Sort (" << size << " elements): ";
cout << "Time taken: " << bubbleSortElapsedTime << " microseconds" << endl;
TASK 6
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <chrono>
arr[j + 1] = key;
}
}
while (j >= 0 && arr[j] < key) { // Change the comparison to sort in descending order
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
}
int main() {
const int arraySizes[] = {100, 500, 1000, 5000, 10000};
cout << "Original Insertion Sort (" << size << " elements): ";
TASK 7:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <chrono>
TASK 8
#include <iostream>
#include <cstdlib>
#include <ctime>
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
}
selectionSort(randomArray, arraySize);
return 0;
}