Posts

Showing posts from May, 2022

Searching and sorting- Linear Search

bool search(int arr[],int size,int key) { for(int i=0;i<n;i++) {       if(arr[i]==key)        {           return 1;        } } return 0; } Time complexity = O(n) space complexity=O(1) Approach: In Linear search, we have given an array, the size of the array, and the key element that we have to find in the array whether the key element is present in the array or not. If the key element is present in an array then return true otherwise return false. Step-1:-Now, make a function for searching the key element and pass the array, size of the array, and key element you want to find. Also return type of function is bool because we just want that key element to be present or not. So, basically, we want if present then True otherwise False and for this, we have to use Bool return type.       bool search(int arr[],int size,int key) {     //code } Step-2: For finding key elements in an array we have to traverse the array from start to its size i.e from 0-->size. So we have to take an int va