C implementation of linear search (#26629)
* C implementation of linear search C implementation of linear search using pointers vs array indexing * fix: replaced existing c solution
This commit is contained in:
committed by
Randell Dawson
parent
87bbbeee8d
commit
e0d97ea3e1
@ -41,37 +41,20 @@ If the element to be searched presides on the the first memory block then the co
|
||||
|
||||
The code for a linear search function in JavaScript is shown below. This function returns the position of the item we are looking for in the array. If the item is not present in the array, the function would return null.
|
||||
|
||||
### Example in C
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
int LinearSearch(int array[], int l,int n);
|
||||
|
||||
int main(void) {
|
||||
int arr[] = {10, 2, 3, 1, 4, 5, 8, 9, 7, 6};
|
||||
LinearSearch(arr, 10, 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LinearSearch(int array[], int l,int n) // l is length of array, n is the number to be searched
|
||||
{
|
||||
int i, flag = 0;
|
||||
for (i = 0 ; i < l - 1; i++)
|
||||
{
|
||||
if(array[i] == n)
|
||||
{
|
||||
flag = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag == 1)
|
||||
printf("Number found");
|
||||
else
|
||||
printf("Number not found");
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
int linearSearch(int arr[], int num)
|
||||
{
|
||||
int len = (int)( sizeof(arr) / sizeof(arr[0]);
|
||||
int *a = arr;
|
||||
for(int i = 0; i < len; i++)
|
||||
{
|
||||
if(*(a+i) == num) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Example in Javascript
|
||||
```javascript
|
||||
function linearSearch(arr, item) {
|
||||
@ -116,6 +99,7 @@ int linear_search(int arr[],int n,int num)
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
### Example in C
|
||||
|
||||
### Example in Python
|
||||
```python
|
||||
|
Reference in New Issue
Block a user