-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSearch1.cpp
116 lines (91 loc) · 2.56 KB
/
Search1.cpp
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
#include<bits/stdc++.h>
using namespace std;
class Search{
private:
int array[100];
public:
int linearSearch(int array[], int x, int arrayLength);
int binarySearch(int array[], int x, int high, int low);
int insertionSort(int array[], int n);
};
int Search::linearSearch(int array[], int x, int arrayLength){
int count=0;
int found=0;
for(int i=0; i<arrayLength; i++){
count++;
if(array[i]==x){
found=1;
break;
}
}
return count;
}
int Search::binarySearch(int array[], int x, int low, int high){
int bCount = 0;
while(high >= low){
bCount++;
int mid = (high + low)/2;
//cout<<"The value of mid is : "<<mid<<endl;
if(array[mid]==x){
break;
}
else if(x < array[mid]){
high = mid -1;
}
else{
low = mid + 1;
}
}
return bCount;
}
int Search::insertionSort(int array[], int n){
for(int i=1; i<n; i++){
for(int j=i; j>0; j--){
if(array[j]<array[j-1]){
int temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
}
}
}
}
int main(){
srand(time(NULL));
Search searchAnalysis;
int a[100];
int lc[1000];
int bc[1000];
cout<<"------------------------------------------------------------------------------"<<endl;
cout<<"Input Size" <<" "<<"Linear "<<"Binary"<<endl;
cout<<"------------------------------------------------------------------------------";
for(int j=0; j<1000; j++){
//cout<<"Entering the value in array"<<endl;
for(int i=0; i<100; i++){
a[i]=rand()%100 + 1;
}
int searchFor = rand()%100 +1;
//cout<<"The value seaching for : "<<searchFor<<endl;
//cout<<"Running linear search"<<endl;
int linearCount = searchAnalysis.linearSearch(a, searchFor, 100);
//cout<<"Running insertion sort"<<endl;
int compInsertion = searchAnalysis.insertionSort(a, 100);
//cout<<"Printing the sorted Array"<<endl;
//for(int i=0; i<100; i++){
// cout<<a[i]<<" ";
// }
// cout<<endl;
//cout<<"Running binary search"<<endl;
int binaryCount = searchAnalysis.binarySearch(a,searchFor, 0, 100);
cout<<" 100 " <<" "<<linearCount<<" "<<binaryCount<<endl;
lc[j] = linearCount;
bc[j] = binaryCount;
}
float lsum = 0.0; float bsum = 0.0;
for (int i=0; i<1000; i++){
lsum+=(float)lc[i];
bsum+=(float)bc[i];
}
cout<<"------------------------------------------------------------------------------";
cout<<" Average " <<" "<<lsum/1000<<" "<<bsum/1000<<endl;
cout<<"------------------------------------------------------------------------------"<<endl;
}