From 410b54f8c1440b23033067cf9c9aa6d883a89ef7 Mon Sep 17 00:00:00 2001 From: Achint Srivastava <32853258+achint769muj@users.noreply.github.com> Date: Mon, 3 Dec 2018 23:56:19 +0530 Subject: [PATCH] Added the code of Linear Search in C Language (#23405) * Added the code of Linear Search in C Language * fix: C example formatting and code --- .../search-algorithms/linear-search/index.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/guide/english/algorithms/search-algorithms/linear-search/index.md b/guide/english/algorithms/search-algorithms/linear-search/index.md index 5bafd6b548..be8c984224 100644 --- a/guide/english/algorithms/search-algorithms/linear-search/index.md +++ b/guide/english/algorithms/search-algorithms/linear-search/index.md @@ -41,6 +41,37 @@ 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; +} +``` ### Example in Javascript ```javascript function linearSearch(arr, item) {