File tree Expand file tree Collapse file tree 4 files changed +200
-0
lines changed Expand file tree Collapse file tree 4 files changed +200
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < algorithm>
3
+ using namespace std ;
4
+ int binary_search (int *arr,int size,int element);
5
+
6
+ int main ()
7
+ {
8
+ int size,element,position;
9
+ cout<<" Enter size of array : " ;
10
+ cin>>size;
11
+ int *arr=new int [size];
12
+ cout<<" Enter all the element of array : " ;
13
+ for (int i=0 ;i<size;i++)
14
+ {
15
+ cin>>arr[i];
16
+ }
17
+ cout<<" Now enter element to search : " ;
18
+ cin>>element;
19
+ sort (arr,arr+size);
20
+ position=binary_search (arr,size,element);
21
+ if (position==-1 )
22
+ cout<<" element not found in array \n " ;
23
+ else
24
+ cout<<" element in array at " <<position<<" th place in array\n " ;
25
+
26
+ }
27
+
28
+ int binary_search (int *arr,int size,int element)
29
+ {
30
+ int low=0 ;
31
+ int high=size-1 ;
32
+ while (high>=low && arr[low]<=element && arr[high]>=element)
33
+ {
34
+ if (low==high)
35
+ {
36
+ if (arr[low]==element)
37
+ return low+1 ;
38
+ return -1 ;
39
+ }
40
+ int position=(low+high)/2 ;
41
+ if (arr[position]==element)
42
+ return position+1 ;
43
+ if (arr[position]<element)
44
+ low=position+1 ;
45
+ else
46
+ high=position-1 ;
47
+ }
48
+ return -1 ;
49
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < algorithm>
3
+ using namespace std ;
4
+ int interpolation_serach (int *arr,int size,int element);
5
+
6
+ int main ()
7
+ {
8
+ int size,element,position;
9
+ cout<<" Enter size of array : " ;
10
+ cin>>size;
11
+ int *arr=new int [size];
12
+ cout<<" Enter all the element of array : " ;
13
+ for (int i=0 ;i<size;i++)
14
+ {
15
+ cin>>arr[i];
16
+ }
17
+ cout<<" Now enter element to search : " ;
18
+ cin>>element;
19
+ sort (arr,arr+size);
20
+ position=interpolation_serach (arr,size,element);
21
+ if (position==-1 )
22
+ cout<<" element not found in array \n " ;
23
+ else
24
+ cout<<" element found at " <<position+1 <<" in array \n " ;
25
+ }
26
+
27
+ int interpolation_search (int *arr,int size,int element)
28
+ {
29
+ int low=0 ;
30
+ int high=size-1 ;
31
+ while (low<=high && element>=arr[low] && element<=arr[high])
32
+ {
33
+ if (low==high)
34
+ {
35
+ if (arr[low]==element)
36
+ return low;
37
+ return -1 ;
38
+ }
39
+ int position=low+(((high - low)/(arr[high]-arr[low]))*(element - arr[low]));
40
+ if (arr[position]==element)
41
+ return position;
42
+ if (arr[position]<element)
43
+ low=position+1 ;
44
+ else
45
+ high=position-1 ;
46
+ }
47
+ return -1 ;
48
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+ int linear_search (int *arr,int size,int element);
4
+
5
+ int main ()
6
+ {
7
+ int size,element,result;
8
+ cout<<" Enter size of array : " ;
9
+ cin>>size;
10
+ int *arr=new int [size];
11
+ cout<<" Enter all the element of array : " ;
12
+ for (int i=0 ;i<size;i++)
13
+ {
14
+ cin>>arr[i];
15
+ }
16
+ cout<<" Now enter element to search : " ;
17
+ cin>>element;
18
+ result=linear_search (arr,size,element);
19
+ if (result!=-1 )
20
+ cout<<" element found at " <<result+1 <<" th position in array " <<endl;
21
+ else
22
+ cout<<" element not found in array" <<endl;
23
+ }
24
+
25
+ int linear_search (int *arr,int size,int element)
26
+ {
27
+ for (int i=0 ;i<size;i++)
28
+ {
29
+ if (arr[i]==element)
30
+ {
31
+ return i;
32
+ }
33
+ }
34
+ return -1 ;
35
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < algorithm>
3
+ using namespace std ;
4
+
5
+ class searching_class
6
+ {
7
+ // linear search
8
+ public:
9
+ int linear_search (int *arr,int size,int element)
10
+ {
11
+ for (int i=0 ;i<size;i++)
12
+ {
13
+ if (arr[i]==element)
14
+ {
15
+ return i+1 ;
16
+ }
17
+ }
18
+ return -1 ;
19
+ }
20
+
21
+ // interpolation search
22
+ int interpolation_search (int *arr,int size,int element)
23
+ {
24
+ int low=0 ;
25
+ int high=size-1 ;
26
+ while (low<=high && element>=arr[low] && element<=arr[high])
27
+ {
28
+ if (low==high)
29
+ {
30
+ if (arr[low]==element)
31
+ return low+1 ;
32
+ return -1 ;
33
+ }
34
+ int position=low+(((high - low)/(arr[high]-arr[low]))*(element - arr[low]));
35
+ if (arr[position]==element)
36
+ return position+1 ;
37
+ if (arr[position]<element)
38
+ low=position+1 ;
39
+ else
40
+ high=position-1 ;
41
+ }
42
+ return -1 ;
43
+ }
44
+
45
+ // binary search
46
+ int binary_search (int *arr,int size,int element)
47
+ {
48
+ int low=0 ;
49
+ int high=size-1 ;
50
+ while (high>=low && arr[low]<=element && arr[high]>=element)
51
+ {
52
+ if (low==high)
53
+ {
54
+ if (arr[low]==element)
55
+ return low+1 ;
56
+ return -1 ;
57
+ }
58
+ int position=(low+high)/2 ;
59
+ if (arr[position]==element)
60
+ return position+1 ;
61
+ if (arr[position]<element)
62
+ low=position+1 ;
63
+ else
64
+ high=position-1 ;
65
+ }
66
+ return -1 ;
67
+ }
68
+ };
You can’t perform that action at this time.
0 commit comments