0% found this document useful (0 votes)
58 views13 pages

Tayyab Khan DSA Lab Report 1

The document contains a lab report submitted by Tayyab Khan to his lab instructor Laiba Sohail. The report includes 7 programs analyzing different sorting algorithms like bubble sort, selection sort, and insertion sort. It tests the time complexity of each algorithm by sorting arrays of various sizes and measuring the time taken. The last program combines bubble sort and selection sort in a hybrid algorithm to sort an array.

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)
58 views13 pages

Tayyab Khan DSA Lab Report 1

The document contains a lab report submitted by Tayyab Khan to his lab instructor Laiba Sohail. The report includes 7 programs analyzing different sorting algorithms like bubble sort, selection sort, and insertion sort. It tests the time complexity of each algorithm by sorting arrays of various sizes and measuring the time taken. The last program combines bubble sort and selection sort in a hybrid algorithm to sort an array.

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/ 13

Pak-Austria Fachhochschule

Institude of Applied sciences And Technology

Name: Tayyab khan


Lab instructor: Laiba Sohail Registration # B22F0411SE055
Department: IT and Cs (Software engineering-22) Section Blue
Lab Report # 01 (Data Structure and Algorithm lab)
Submitted Date: 16th Nov , 2023.

Program # 01 :
#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]);
}
}
}
}
int main() {
int arr[6] = {33, 26, 9, 18, 67, 42};
cout << "Simple Array display: ";
for (int i = 0; i < 6; i++) {
cout << arr[i] << ", ";
}

1
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
bubbleSort(arr, 6);
cout << "\nArray in Descending Order: ";
for (int i = 0; i < 6; i++) {
cout << arr[i] << ", ";
}
return 0;
}

Program # 02 :
#include<iostream>
#include<ctime>
using namespace std;
void BubbleSort(int a[], const int arr_size){
int pass = 1;
bool exchanges;
do {
exchanges = false;
for (int i = 0; i < arr_size-pass; i++)
if (a[i] < a[i+1]) {
int tmp = a[i];
a[i] = a[i+1];
a[i+1] = tmp;
exchanges = true;
}
pass++;
} while (exchanges);
}

2
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
int main(){
int n;
cout<<"Enter the size of array: ";
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
arr[i] = i+1;
}
cin.ignore();
cout<<"\nEnter for bubble sorting of the array...\n";
cin.get();
clock_t start = clock();
BubbleSort(arr,n);
clock_t end = clock();
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
double elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
cout << "\n\n\nTime complexity : " << elapsed_time << " seconds" << endl;
return 0;
}

Program # 03 :
#include<iostream>
#include<ctime>
using namespace std;
void selectionSort(int arr[], const int arr_size) {

3
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
for (int i = 0; i < arr_size - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < arr_size; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
if (maxIndex != i) {
swap(arr[i], arr[maxIndex]);
}
}
}
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
cout << "\nEnter for selection sorting of the array...\n";
cin.ignore();
cin.get();
clock_t start = clock();
selectionSort(arr, n);
clock_t end = clock();
cout << "Sorted Array: ";

4
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
double elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
cout << "\n\nTime complexity: " << elapsed_time << " seconds" << endl;
return 0;
}

Program # 04 :
#include <iostream>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <ctime>
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])
std::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;

5
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
std::swap(arr[i], arr[minIndex]);
}
}
int main() {
const int arraySize = 10000;
int arrBubble[arraySize], arrSelection[arraySize];
std::srand(std::time(0));
std::generate_n(arrBubble, arraySize, std::rand);
std::copy(arrBubble, arrBubble + arraySize, arrSelection);
auto startBubble = std::chrono::high_resolution_clock::now();
bubbleSort(arrBubble, arraySize);
auto endBubble = std::chrono::high_resolution_clock::now();
cout << "Bubble Sort Time: " << std::chrono::duration<double>(endBubble -
startBubble).count() << " seconds\n";
auto startSelection = std::chrono::high_resolution_clock::now();
selectionSort(arrSelection, arraySize);
auto endSelection = std::chrono::high_resolution_clock::now();
cout << "Selection Sort Time: " << std::chrono::duration<double>(endSelection -
startSelection).count() << " seconds\n";
return 0;
}

Program # 05 :
#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++) {

6
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
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 maxIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
swap(arr[i], arr[maxIndex]);
}
}
int main() {
int arr[] = {5, 2, 8, 12, 1};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
cout << "Bubble Sort (Descending Order): ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
selectionSort(arr, n);

7
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
cout << "Selection Sort (Descending Order): ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

Program # 06 :
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
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() % 1000; // Generate random numbers between 0 and 999

8
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
}
}
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 originalInsertionArr[arraySize];
generateRandomArray(originalInsertionArr, arraySize);
cout << "Original Array:\n";
printArray(originalInsertionArr, arraySize);
clock_t startOriginalInsertion = clock();
insertionSort(originalInsertionArr, arraySize);
clock_t endOriginalInsertion = clock();
cout << "\nOriginal Insertion Sorted Array:\n";
printArray(originalInsertionArr, arraySize);
cout << "Original Insertion Sort Time: " << double(endOriginalInsertion -
startOriginalInsertion) / CLOCKS_PER_SEC << " seconds\n";
return 0;
}

Program # 07 :
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
int temp = *a;

9
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
*a = *b;
*b = temp;
}
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 hybridSort(int arr[], int n) {
bubbleSort(arr, n / 2);
selectionSort(arr + n / 2, n - n / 2);
int i = 0, j = n / 2, k = 0;

10
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
int temp[n];
while (i < n / 2 && j < n) {
if (arr[i] < arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i < n / 2) {
temp[k++] = arr[i++];
}
while (j < n) {
temp[k++] = arr[j++];
}
for (int i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {9, 5, 1, 8, 3, 7, 2, 6, 4};
int n = sizeof(arr) / sizeof(arr[0]);

11
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
cout << "Original Array: ";
printArray(arr, n);
hybridSort(arr, n);
cout << "Sorted Array: ";
printArray(arr, n);
return 0;
}

Program # 07 :
#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;
}
}
std::swap(arr[i], arr[minIndex]);
}
}
int main() {
const int size = 10;
int arr[size];

12
Pak-Austria Fachhochschule
Institude of Applied sciences And Technology
srand(time(0));
for (int i = 0; i < size; i++) {
arr[i] = rand() % 100;
}
cout << "Unsorted Array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
selectionSort(arr, size);
cout << "Sorted Array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

13

You might also like