-
Notifications
You must be signed in to change notification settings - Fork 96
/
recursiveLinearSearch.c
57 lines (46 loc) · 1.21 KB
/
recursiveLinearSearch.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
#include <stdio.h>
#include <stdbool.h>
/**
* This function contains the main logic of recursive linear search algorithm
*
* @param array
* @param leftIndex
* @param rightIndex
*
* @return boolean if key element found or not
**/
bool linearSearch(int array[], int leftIndex, int rightIndex, int key)
{
if( array[leftIndex] == key ){ // if element is found at this index
return true;
}
if(leftIndex == rightIndex){ // if array traversed upto end
return false;
}
bool ans = linearSearch(array, leftIndex+1 , rightIndex, key);
if(ans){
return true;
}
return false;
}
int main()
{
int size;
printf("Enter the size of the array\n");
scanf("%d", &size);
int array[size];
printf("Enter the elements of the array\n");
for(register int i=0; i<size; i++)
{
scanf("%d",&array[i]);
}
int key;
printf("Enter the element to find\n");
scanf("%d",&key);
bool result = linearSearch(array, 0, size, key);
if(result){
printf("Element found!!!\n");
}else{
printf("Element not found\n");
}
}