Sami-Lab 5 Report
Sami-Lab 5 Report
Lab Report
Data Structure(DS)
/*
Submitted to: Maam Laiba Sohail
Submitted by: Sami Ur Rehman
Date Performed: Nov 16 2023
ID: B22F1008SE092
Lab no: 05
*/
Conclusion
In this lab, we implemented three fundamental sorting algorithms in C++—bubble sort, selection sort, and
insertion sort—each applied to arrays. The bubble sort algorithm involved iteratively comparing and
swapping adjacent elements, gradually moving the largest elements to their correct positions. Selective
sorting, on the other hand, entailed selecting the minimum element from the unsorted portion and placing
it at the beginning, progressively organizing the array. Lastly, insertion sort efficiently sorted the array by
iteratively placing each element in its appropriate position relative to the already sorted subset. Through
this hands-on exploration, we gained valuable insights into the workings of these sorting algorithms, their
complexities.
---------------------------------------------------------------------
LAB TASKS
TASK 01
Program:
#include<iostream>
#include<String>
using namespace std;
int main()
{
int n;
1
IT & Computer Science Department
Pak-Austria Fachhochschule Institute of Applied Science and Technology,
Haripur, Pakistan
2
IT & Computer Science Department
Pak-Austria Fachhochschule Institute of Applied Science and Technology,
Haripur, Pakistan
TASK 02
Program:
#include<iostream>
#include<String>
using namespace std;
int main()
{
int n;
cout<<"Enter Size of array : ";
cin>>n;
cout<<endl;
int arr[n];
cout<<"Enter Array Elements"<<endl;
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
for(int i=0; i<n-1; i++)
{
for(int j=0; j<n-i-1; j++)
{
if(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
3
IT & Computer Science Department
Pak-Austria Fachhochschule Institute of Applied Science and Technology,
Haripur, Pakistan
}
}
for (int i = 0; i < n; i++)
{
cout<<arr[i] << " ";
}
return 0;
}
--------------------------------------------------
TASK 03
Program:
#include<iostream>
#include<chrono>
using namespace std;
int main()
{
auto start = std::chrono::high_resolution_clock::now();
int n;
cout<<"Enter Size of array : ";
cin>>n;
cout<<endl;
int arr[n];
cout<<"Enter Array Elements"<<endl;
for (int i = 0; i < n; i++)
{
cin>>arr[i];
4
IT & Computer Science Department
Pak-Austria Fachhochschule Institute of Applied Science and Technology,
Haripur, Pakistan
}
for(int i=0; i<n-1; i++)
{
int min_index = i;
for(int j= i + 1; j<n; j++)
{
if(arr[j] < arr[min_index])
{
min_index = j;
}
}
int temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
for (int i = 0; i < n; i++)
{
cout<<arr[i] << " ";
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop-start);
cout<<endl;
cout<<"Time Taken by Duration "<<duration.count() << " microseconds" <<endl;
return 0;
}
--------------------------------------------------------------
TASK 04
5
IT & Computer Science Department
Pak-Austria Fachhochschule Institute of Applied Science and Technology,
Haripur, Pakistan
Program:
#include<iostream>
#include<chrono>
using namespace std;
int main()
{
auto start = std::chrono::high_resolution_clock::now();
int n;
cout<<"Enter Size of array : ";
cin>>n;
cout<<endl;
int arr[n];
cout<<"Enter Array Elements"<<endl;
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
for(int i=0; i<n-1; i++)
{
for(int j=0; j<n-i-1; j++)
{
if(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
6
IT & Computer Science Department
Pak-Austria Fachhochschule Institute of Applied Science and Technology,
Haripur, Pakistan
}
}
cout<< "Bubble Sort" << endl;
for (int i = 0; i < n; i++)
{
cout<<arr[i] << " ";
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop-start);
cout<<endl;
cout<<"Time Taken by Duration "<<duration.count() << " microseconds" <<endl;
return 0;
}
TASK 05
Program:
#include<iostream>
#include<chrono>
using namespace std;
int main()
{
auto start = std::chrono::high_resolution_clock::now();
int n;
cout<<"Enter Size of array : ";
cin>>n;
cout<<endl;
int arr[n];
cout<<"Enter Array Elements"<<endl;
7
IT & Computer Science Department
Pak-Austria Fachhochschule Institute of Applied Science and Technology,
Haripur, Pakistan
8
IT & Computer Science Department
Pak-Austria Fachhochschule Institute of Applied Science and Technology,
Haripur, Pakistan
return 0;
}