You are on page 1of 43

Name : Muhammad Mahmood

Roll no: 19011598-080


Section : B
Submitted to Mam Reema
Q1:

#include<iostream>

#include<time.h>

#include<stdlib.h>

using namespace std;

class Selection

private:

int arr[1000];

public:
void inputRandom()

{
cout<<"UNSORTED ARRAY VALUES :
"<<endl;

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

int random = rand();

arr[i]= random;

void inputSorted()

cout<<"early sorted” :
"<<endl;

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

{
int random = rand();

arr[i]= i+1;

}
}

void LinearSearch(int x)

int index=0;

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

if(arr[i]==x)

cout<<"value found:
"<<index<<endl;

break;

else
{

index++;

}
}

void BinarySearch(int x)

int middle = 1000/2;

int index=middle;

if(arr[middle] < x)

for(int i=middle; i<1000;i++)

if(arr[i]==x)
{

cout<<"VALUE FOUND AT INDEX


: "<<index<<endl;

break;

}
else

index++;

else

for(int i=middle;i>=0;i--)

if(arr[i]==x)

{
cout<<"VALUE FOUND AT INDEX
: "<<x<<endl;

break;

}
else

index++;

void show()

{
cout<<"elements of array: "<<endl<<endl;

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

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

};

int main()

//LINEAR SEARCH

clock_t start1 = clock();

Selection s;

s.inputSorted();

s.show();

cout<<endl<<endl;

s.LinearSearch(57);
clock_t end1 = clock();

clock_t result1 = end1-start1;

cout<<endl<<endl;

cout<<"TIME TAKEN BY LINEAR SEARCHING ON SORTED ARRAY IN


MILLISECONDS : "<<result1<<endl<<endl<<endl;
clock_t start2 = clock();

s.inputRandom();

s.show();

cout<<endl<<endl;

s.LinearSearch(19923);

clock_t end2 = clock();

clock_t result2 = end2-start2;

cout<<endl<<endl;

cout<<"TIME TAKEN BY LINEAR SEARCHING ON UNSORTED ARRAY:


"<<result2<<endl<<endl<<endl;

// BINARY SEARCH

clock_t start3 = clock();


s.inputSorted();

s.show();

cout<<endl<<endl;

s.BinarySearch(57);
clock_t end3 = clock();

clock_t result3 = end3-start3;

cout<<endl<<endl;

cout<<"TIME TAKEN BY BINARY SEARCHING SEARCHING ON


SORTED ARRAY IN MILLISECONDS : "<<result3<<endl<<endl<<endl;

clock_t start4 = clock();

s.inputRandom();

s.show();

cout<<endl<<endl;

s.BinarySearch(4000);

clock_t end4 = clock();

clock_t result4= end4-start4;


cout<<endl<<endl;

cout<<"TIME TAKEN BY BINARY SEARCHING ON UNSORTED


ARRAY IN MILLISECONDS : "<<result4<<endl;
Q2;
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
class Sort

private:

int arr[1000];

int start;

int end;

public:

void inputRandom()

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

int random = rand();

arr[i]=random;

}
}

void inputSorted()

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

arr[i]=i+1;

void insertionSort()
{

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

{
int key =arr[i];

int j=i-1;

while(j>=0 && arr[j]>key)

arr[j+1] =arr[j];

j=j-1;

arr[j+1] = key;

void selectionSort()
{

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

int min = i;

for(int j=i+1 ; j<1000; j++)


{

if(arr[j] < arr[min])

min=j;

swap(&arr[i] , &arr[min]) ;

void swap(int *a , int *b)


{
int temp = *a ;

*a = *b ;

*b = temp;

}
void show()

cout<<"ARRAY VALUES ARE : "<<endl;


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

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

};

int main()

{
//LINEAR SEARCH

clock_t start1 = clock();

Sort s;

s.inputSorted();
cout<<endl<<endl;

s.selectionSort();

cout<<"AFTER APPLYING SELECTION SORT ON SORTED ARRAY :


"<<endl;

s.show();

clock_t end1 = clock();

clock_t result1 = end1-start1;

cout<<endl<<endl;

cout<<"TIME TAKEN BY SELECTION SORT ON SORTED ARRAY IN


MILLISECONDS : "<<result1<<endl<<endl<<endl;

clock_t start2 = clock();

s.inputRandom();

cout<<endl<<endl;

s.selectionSort();
cout<<"AFTER APPLYING SELECTION SORT ON UNSORTED ARRAY :
"<<endl;

s.show();

clock_t end2 = clock();


clock_t result2 = end2-start2;

cout<<endl<<endl;

cout<<"TIME TAKEN BY SELECTION SORT ON UNSORTED ARRAY :


"<<result2<<endl<<endl<<endl;

// INSERTION SORT

clock_t start3 = clock();

s.inputSorted();

cout<<endl<<endl;

s.insertionSort();

cout<<"AFTER APPLYING INSERTION SORT ON SORTED


ARRAY : "<<endl;

s.show();

clock_t end3 = clock();


clock_t result3 = end3-start3;

cout<<endl<<endl;

cout<<"TIME TAKEN BY INSERTION SORT ON SORTED ARRAY:


"<<result3<<endl<<endl<<endl;
clock_t start4 = clock();

s.inputRandom();

cout<<endl<<endl;

s.insertionSort();

cout<<"AFTER APPLYING INSERTION SORT ON UNSORTED


ARRAY : "<<endl;

s.show();

clock_t end4 = clock();

clock_t result4= end4-start4;

cout<<endl<<endl;

cout<<"TIME TAKEN BY INSERTION SORT ON UNSORTED ARRAY:


"<<result4<<endl;
}
Q3:
#include <iostream>

#include<stdlib.h>

#include<chrono>
#include<time.h>

#include<ctime>

using namespace std;

void swap(int *x, int *y) {

int temp = *x;

*x = *y;

*y= 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;

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);

You might also like