-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcom_alg_time
122 lines (121 loc) · 3.24 KB
/
com_alg_time
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <chrono>
#include<stdlib.h>
using namespace std;
//swaping array
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
//partation code for deviding array based on pevot element
int partition(int array[], int low, int high) {
int pivot = array[high];
int i = (low);
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
swap(&array[i], &array[j]);
i++;
}
}
swap(&array[i], &array[high]);
return (i );
}
//iterative program for quickshorting
void iterativequickSort(int arr[], int l, int h)
{
int stack[h + 1];
int top = -1;
stack[++top] = l;
stack[++top] = h;
while (top >= 0) {
h = stack[top--];
l = stack[top--];
int i = partition(arr, l, h);
if (i - 1 > l) {
stack[++top] = l;
stack[++top] = i - 1;
}
if (i + 1 < h) {
stack[++top] = i + 1;
stack[++top] = h;
}
}
}
void bubbleshort(int array[],int n){
int i,j; auto begin = std::chrono::high_resolution_clock::now();
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
{
if(array[j]>array[j+1])
swap(&array[j],&array[j+1]);
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured for bubble short: %.5f seconds.\n", elapsed.count() * 1e-9);
}
void binarysearch(int array[],int length)
{
int first=0,key,flag=0;
cout<<" which element you want to find out";
cin>>key;
auto begin = std::chrono::high_resolution_clock::now();
while(first<=length)
{
int mid=(first+length)/2;
if(array[mid]==key)
{ cout<<"your element is at "<<mid+1<<"th place";
flag=1;
break;
}
else if(array[mid]>key)
length=mid-1;
else if(array[mid]<key)
first=mid+1;
}
if(flag==0)
cout<<"your element is not there in this array \n";
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured for binary search: %.5f seconds.\n", elapsed.count() * 1e-9);
}
void linearsearch(int array[],int length){
int key,flag=0,i;
cout<<"enter the element you want to find";
cin>>key;
auto begin = std::chrono::high_resolution_clock::now();
for(i=0;i<length;i++)
{
if(array[i]==key)
{
cout<<"your element found at arr["<<i<<"]";
flag=1;
}
}
if(flag==0)
cout<<"your element not found \n";
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured for linearsearch: %.5f seconds.\n", elapsed.count() * 1e-9);
}
int main() {
int i,max=1000;
int arr[max];
for(i=0;i<max;i++)
{
arr[i]=rand()%1000;
}
linearsearch(arr,max-1);
auto begin = std::chrono::high_resolution_clock::now();
iterativequickSort(arr, 0, max - 1);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured for quickshorting: %.5f seconds.\n", elapsed.count() * 1e-9);
for(i=0;i<max;i++)
{
arr[i]=rand()%1000;
}
bubbleshort(arr,max);
binarysearch(arr,max-1);
return 0;
}