forked from MakeContributions/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(C): add linear search (MakeContributions#293)
- Loading branch information
1 parent
3340691
commit d2b175a
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// linear search | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
//function prototype | ||
int linearSearch(int a[], int query, int size); | ||
|
||
int main() | ||
{ | ||
int *data,i,temp,searchQuery,size; | ||
printf("Enter size:"); | ||
scanf("%d",&size); | ||
data=(int*)malloc(sizeof(int)*size); | ||
|
||
//generate some data | ||
for (i = 0; i < size; ++i) | ||
{ | ||
scanf("%d",&data[i]); | ||
} | ||
|
||
printf("Enter integer to search for: "); | ||
scanf("%d", &searchQuery); | ||
|
||
//finding searchQuery in array data | ||
temp = linearSearch(data, searchQuery, size); | ||
|
||
//condition for displaying result | ||
if ( temp != -1) | ||
printf("Value found at index no:%d", temp); | ||
else | ||
printf("Value not found"); | ||
|
||
return 0; | ||
} | ||
|
||
//Linear search function | ||
|
||
int linearSearch(int a[], int query, int size) | ||
{ | ||
int j; //counter variable | ||
|
||
//loop through array | ||
for (j = 0; j < size; ++j) | ||
{ | ||
if ( a[ j ] == query) | ||
{ | ||
return j; //return location of value | ||
} | ||
} | ||
|
||
return -1; //value not found | ||
} | ||
/* | ||
Case 1: | ||
Input: Enter size: 10 | ||
1,2,3,4,5,6,7,8,50,10 | ||
Enter the data to search: 11 | ||
Output: Data not Found | ||
Case 2: | ||
Input: Enter size: 10 | ||
1,2,3,4,5,6,7,8,9,10 | ||
Enter the data to search: 7 | ||
Output: Data Found at index: 6 | ||
Time complexity: O(n) //where n is the size of array | ||
*/ |