-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearBinary.c
194 lines (167 loc) · 4.49 KB
/
LinearBinary.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "omp.h"
clock_t t, t2;
double cpu_time_used, cpu_time_used2;
int linearSearch(int* A, int n, int tos);
int main(){
int number, iter =0, find;
int* Arr;
int s[number];
printf("Done by Maitreyee\n\n");
Arr = (int *)malloc( number * sizeof(int));
scanf("%d", &number);
for(; iter<number; iter++){
scanf("%d", &Arr[iter]);
}
scanf("%d", &find);
printf("\nTo find: %d\n", find);
t = clock();
int indexx = linearSearch(Arr, number, find);
t = clock()-t;
printf("\nLinear Search :\n");
if(indexx == -1){
printf("Not found");
}
else
printf("Found at %d\n", indexx);
cpu_time_used = ((double)t)/CLOCKS_PER_SEC;
printf("\nTime taken for linear search: %f", cpu_time_used);
t2 = clock();
int index2 = binarysearch(Arr, number, find);
t2 = clock()-t;
printf("\nBinary Search: \n");
if(index2 == -1){
printf("Not found");
}
else
printf("Found at %d\n", index2);
cpu_time_used2 = ((double)t2)/CLOCKS_PER_SEC;
printf("\nTime taken for binary search: %f", cpu_time_used2);
return 0;
}
int linearSearch(int* A, int n, int tos){
int mid = n/2;
int foundat = -1;
int one_quart = mid/2;
int three_quart = 3*one_quart;
double start_time;
start_time = omp_get_wtime();
int loc[4];
double end_time;
#pragma omp parallel sections
{
#pragma omp section
{
start_time = omp_get_wtime();
loc[0] = linear(A, t, 0, one_quart);
end_time = omp_get_wtime()-start_time;
printf("Time for section %d: \t %f \n",1, end_time);
}
#pragma omp section
{
start_time = omp_get_wtime();
loc[1] = linear(A, t, one_quart+1, mid);
end_time = omp_get_wtime()-start_time;
printf("Time for section %d: \t %f \n",1, end_time);
}
#pragma omp section
{
start_time = omp_get_wtime();
loc[2] = linear(A, t, mid+1, three_quart);
end_time = omp_get_wtime()-start_time;
printf("Time for section %d: \t %f \n",3, end_time);
}
#pragma omp section
{
start_time = omp_get_wtime();
loc[3] = linear(A, t, three_quart+1, n-1);
end_time = omp_get_wtime()-start_time;
printf("Time for section %d: \t %f \n",4, end_time);
}
}
int i;
for(int i = 0; i<4; i++)
{
if(loc[i]!=-1)
{
return loc[i];
}
}
}
int linear(int A[], int x, int low, int high)
{
int i, foundat;
for(int i =low ; i< high; i++){
if(A[i] == x)
foundat = i+1;
return foundat;
}
return -1;
}
int binarysearch(int* A, int n, int k)
{
int mid = n/2;
double start_time;
int low = 0;
int high = n -1;
int one_quart = mid/2;
int three_quart = 3*one_quart;
start_time = omp_get_wtime();
double end_time;
int loc[4];
#pragma omp parallel sections
{
#pragma omp section
{
start_time = omp_get_wtime();
loc[0] = binary(A, t, 0, one_quart);
end_time = omp_get_wtime()-start_time;
printf("Time for section %d: \t %f \n",1, end_time);
}
#pragma omp section
{
start_time = omp_get_wtime();
loc[1] = binary(A, t, one_quart+1, mid);
end_time = omp_get_wtime()-start_time;
printf("Time for section %d: \t %f \n",1, end_time);
}
#pragma omp section
{
start_time = omp_get_wtime();
loc[2] = binary(A, t, mid+1, three_quart);
end_time = omp_get_wtime()-start_time;
printf("Time for section %d: \t %f \n",3, end_time);
}
#pragma omp section
{
start_time = omp_get_wtime();
loc[3] = binary(A, t, three_quart+1, n-1);
end_time = omp_get_wtime()-start_time;
printf("Time for section %d: \t %f \n",4, end_time);
}
}
int i;
for(int i = 0; i<4; i++)
{
if(loc[i]!=-1)
{
return loc[i]+1;
}
}
}
int binary(int arr[], int x, int low, int high)
{
while(low <= high)
{
int mid = low + (high-low)/2;
if(arr[mid] == x)
return mid;
if(arr[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}