Professional Documents
Culture Documents
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;
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
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;
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++)
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]<<"]";
//}
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]<<"] ";
}
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]<<"] ";
}
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;
}
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);
}