-
Notifications
You must be signed in to change notification settings - Fork 2
/
search_ele_array.cpp
109 lines (87 loc) · 2.26 KB
/
search_ele_array.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
/****************************************************************************
File name: search_ele_array.cpp
Author: babajr
*****************************************************************************/
/*
Program to search the element in an array using recursion.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
API to search element in an array in iterative way.
*/
int search_element_iterative(int arr[], int size, int ele)
{
for(int i = 0; i < size; i++)
{
if(arr[i] == ele)
return i;
}
// if element not found return -1.
return -1;
}
// /*
// API to search element in an array in recursive way.
// */
// int search_element_recursive(int arr[], int size, int ele)
// {
// int res;
// size--;
// if(size >= 0)
// {
// if(arr[size] == ele)
// return size;
// else
// res = search_element_recursive(arr, size, ele);
// }
// else // element is not present in the array
// return -1;
// }
/*
API to search element in an array in recursive way.
*/
int search_element_recursive(int arr[], int size, int ele)
{
// base case
if(size < 0)
return -1;
if(arr[size] == ele)
return size;
else
return search_element_recursive(arr, size - 1, ele);
}
/*
API to search element in an array in recursive way.
If element is present in the array, return 1 (true) else return 0 (false).
*/
bool search_element_recursive_bool(int arr[], int size, int ele, int index)
{
// base case
if(index == size)
return false;
return (arr[index] == ele) || (search_element_recursive_bool(arr, size, ele, index + 1));
}
/*
API to return index of element if found in the array. otherwise return -1.
*/
int find_index(int arr[], int size, int ele, int index)
{
// base case
if(index == size)
return -1;
if(arr[index] == ele)
return index;
else
return find_index(arr, size, ele, index + 1);
}
int main(void)
{
int arr[] = {1,2,3,4,5};
int ele = 5;
int size = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", search_element_recursive(arr, size - 1, ele));
printf("%d\n", search_element_recursive_bool(arr, size, ele, 0));
printf("%d\n", find_index(arr, size, ele, 0));
return 0;
}