You are on page 1of 10

Data Structure and Algorithms

By: Faizan Mustafa

20th Feb 2022



DSA

To: Mam Reema
Q#1
Take array of random numbers size 1000 with random function
i. Apply linear search on unsorted array and check time
ii. Sort the array
iii. Apply linear search on sorted array and check time
iv. Apply binary search on sorted array and check time

Ans:
#include <iostream>
#include<stdlib.h>
#include<chrono>
#include<time.h>
#include<ctime>
using namespace std;
using namespace std::chrono;
int main()
{

clock_t time_req;
int sz;
int first=0;
int last=sz-1;
int middle;
cout<<"Enter size of Array: ";
cin>>sz;
int arr[sz], num;
int num1;
int index;
int index3;

//time_req = clock();
clock_t start_time = clock();
for(int i=0;i<sz;i++){
arr[i]=rand()%1000;}
cout<<"Element in random array are:";
for(int i=0;i<sz;i++)
cout<<"["<<arr[i]<<"]";
clock_t end_time1 = clock();
clock_t result = end_time1 - start_time;
cout<<"\n it took "<<result<<" milliseconds"<<endl;

//linear Search on usorted array

DSA Assignment PAGE 2


clock_t start_time2 = clock();
cout<<"\nEnter a Number for Linear Search: ";
cin>>num;
for(int i=0; i<10; i++)
{
if(arr[i]==num)
{
index = i;
break;
}
}
cout<<"Found at Index : "<<index;
cout<<endl;

clock_t end_time3 = clock();


clock_t result2 = end_time3 - start_time2;
cout<<"\n it took "<<result2<<" milliseconds"<<endl;

//Sorting of an array in ascending order


clock_t start_time3 = clock();
cout<<"performing Sorting operations on Array"<<endl;
for(int i=0; i<sz; i++)
{
for(int j=i+1; j<sz; j++)
{
if(arr[j] < arr[i])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
cout<<"The new Sorted form of Array is: ";
for(int i=0; i<sz; i++)
{
cout<<"["<<arr[i]<<"] ";
}
clock_t end_time4 = clock();
clock_t result3 = end_time4 - start_time3;
cout<<"\n it took "<<result2<<" milliseconds"<<endl;

DSA Assignment PAGE 3


//Again linear search on Sorted Array for checking the time
auto start = chrono::steady_clock::now();

int num2;
cout<<"\nEnter a Number for Linear Search Again: ";
cin>>num2;
for(int i=0; i<10; i++)
{
if(arr[i]==num2)
{
index3 = i;
break;
}
}
cout<<"Found at Index : "<<index3;
cout<<endl;
auto end = chrono::steady_clock::now();
cout<<chrono::duration_cast<chrono::milliseconds>(end-start).count()<<"
milliseconds"<<endl;

// Binary Search

auto start2 = chrono::steady_clock::now();


time_req = clock();

first=0;
last=sz-1;
cout<<"Enter Number for Binary search: ";
cin>>num1;
middle = (first+last)/2;
while(first <= last)
{
if(arr[middle]<num1)
first = middle+1;
else if(arr[middle]==num1)
{
cout<<"\nThe number, "<<num1<<" found at Index "<<middle;
break;
}
else
last = middle-1;
middle = (first+last)/2;

DSA Assignment PAGE 4


}
if(first>last)
cout<<"\nThe number"<<num1<<" is not found in given Array";
cout<<endl;

time_req = clock() - time_req;


cout << "it took " << (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;

auto end2 = chrono::steady_clock::now();


cout<<chrono::duration_cast<chrono::milliseconds>(end2-start2).count()<<"
milliseconds"<<endl;

return 0;
}

Q#2
1...(Selection sort)
i. Apply selectionsort on unsorted array & check time
ii. Apply selectionsort on already sorted array & check time
2...(Insertion sort)
i. Apply insertionsort on unsorted array & check time
ii. Apply insertionsort on already sorted array & check time
Ans:
#include<iostream>
#include<stdlib.h>
#include<chrono>
#include<time.h>
#include<ctime>
using namespace std;

int main()
{
int i,j,n,loc,temp,min,a[30],b[30];
clock_t time_req;
time_req = clock();
cout<<"Enter the number of elements:";
cin>>n;
for(int i=0;i<n;i++){
a[i]=rand()%100;}
cout<<"Orignal array elements are:";
for(int i=0;i<n;i++)

DSA Assignment PAGE 5


{
cout<<"["<<a[i]<<"]";
}

for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}

temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"\nSorted Arraay is as follows\n";
for(i=0;i<n;i++)
{
cout<<"["<<a[i]<<"] ";
}
time_req = clock() - time_req;
cout << "\n it took " << (float)time_req/CLOCKS_PER_SEC << " seconds" <<
endl;

/// Applying the Selection Sorting Again on Already Sorted Array for checking
execution time

//cout<<"\n\n\n Now we are making a new Array and sort it with Insertion
Sorting Method"<<endl;
time_req = clock();
//for(int i=0;i<n;i++){
//a[i]=rand()%100;}
//cout<<"New Orignal array of elements are:";
// for(int i=0;i<n;i++)
//{
// cout<<"["<<a[i]<<"]";
//}

DSA Assignment PAGE 6


for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}

temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"\n Again Selection Sorted Arraay is as follows\n";
for(i=0;i<n;i++)
{
cout<<"["<<a[i]<<"] ";
}

time_req = clock() - time_req;


cout << "\n again sorting took " << (float)time_req/CLOCKS_PER_SEC << "
seconds" << endl;

//Now performing the Insertion Sorting

cout<<"\n\n\n Now we are making a new Array and sort it with Insertion
Sorting Method\n\n"<<endl;
time_req = clock();
for(int x=0;x<n;x++){
b[x]=rand()%100;}
cout<<"New Orignal array of elements aren \n:";
for(int x=0;x<n;x++)
{
cout<<"["<<b[x]<<"] ";
}

for(int k=1; k<n; k++)

DSA Assignment PAGE 7


{
int temp1 = b[k];
int r= k-1;
while(r>=0 && temp1 <= b[r])
{
b[r+1] = b[r];
r = r-1;
}
b[r+1] = temp1;
}
cout<<"\n the Insertion Sorted Array is \n";
for(int y=0;y<n;y++)
{
cout <<"["<<b[y]<<"] ";
}
time_req = clock() - time_req;
cout << "\n Seletion sorting took " << (float)time_req/CLOCKS_PER_SEC << "
seconds time" << endl;

cout<<"\n\nNow perorming the Selecion Sortig Again to check the execution


time\n\n";

for(int k=1; k<n; k++)


{
int temp1 = b[k];
int r= k-1;
while(r>=0 && temp1 <= b[r])
{
b[r+1] = b[r];
r = r-1;
}
b[r+1] = temp1;
}
cout<<"\n the Insertion Sorted Array is \n";
for(int y=0;y<n;y++)
{
cout <<"["<<b[y]<<"] ";
}
time_req = clock() - time_req;
cout << "\n AGain Seletion sorting took " << (float)time_req/CLOCKS_PER_SEC
<< " seconds time" << endl;

DSA Assignment PAGE 8


return 0;
}

Q#3
Selection sort
i. Swap inside loop & check time
ii. Swap outside loop & check time
Ans:
#include <iostream>
#include<stdlib.h>
#include<chrono>
#include<time.h>
#include<ctime>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

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


for(int i=0;i<size;i++){
array[i]=rand()%100;}
cout<<"Orignal array elements are:";
for(int i=0;i<size;i++)
{
cout<<"["<<array[i]<<"]";
}

cout << endl;


}

void selectionSortOuter(int array[], int size, clock_t time_req) {


time_req = clock();
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
if (array[i] < array[min_idx])
min_idx = i;

DSA Assignment PAGE 9


}
swap(&array[min_idx], &array[step]);
}
time_req = clock() - time_req;
cout << "\n it took " << (float)time_req/CLOCKS_PER_SEC << " seconds" <<
endl;
}

void selectionSortInner(int array[], int size,clock_t time_req) {


time_req = clock();
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
if (array[i] < array[min_idx])
{
int temp=array[i];
array[i]=array[min_idx];
array[min_idx]=temp;
}
}
}
time_req = clock() - time_req;
cout << "\n it took " << (float)time_req/CLOCKS_PER_SEC << " seconds" <<
endl;
}
int main() {
clock_t time_req;
int sz;
cout<<"Enter size of Array"<<endl;
cin>>sz;

int data[sz];
int size = sizeof(data) / sizeof(data[0]);
selectionSortOuter(data, size, time_req);
cout << "Sorted array by outer:\n";
printArray(data, size);
selectionSortInner(data, size, time_req);
cout << "Sorted array by inner:\n";
printArray(data, size);
}

DSA Assignment PAGE 10

You might also like