diff --git a/guide/english/algorithms/search-algorithms/linear-search/index.md b/guide/english/algorithms/search-algorithms/linear-search/index.md index 1849361409..af8d8d43ec 100644 --- a/guide/english/algorithms/search-algorithms/linear-search/index.md +++ b/guide/english/algorithms/search-algorithms/linear-search/index.md @@ -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 - -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