Skip to main content

Python Program for Linear Search

 Python Program for Linear Search

  • Difficulty Level : Medium
  • Last Updated : 11 JUNE, 2021

Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[].


Examples :

Input : arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170}
          x = 110;
Output : 6
Element x is present at index 6

Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170}
           x = 175;
Output : -1
Element x is not present in arr[].

A simple approach is to do linear search, i.e

  • Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
  • If x matches with an element, return the index.
  • If x doesn’t match with any of elements, return -1.


Example:
linear-search1

# Searching an element in a list/array in python
# can be simply done using \'in\' operator
# Example:
# if x in arr:
#   print arr.index(x)
  
# If you want to implement Linear Search in python
  
# Linearly search x in arr[]
# If x is present then return its location
# else return -1
  
def search(arr, x):
  
    for i in range(len(arr)):
  
        if arr[i] == x:
            return i
  
    return -1

The time complexity of above algorithm is O(n).

Please refer complete article on Linear Search for more details!

Comments

Popular posts from this blog

Data Structure Multiple Choice Questions and Answers

  Data Structure Multiple Choice Questions and Answers Our 1000+ multiple choice questions and answers (MCQs) on "Data Structure - I" (along with 1000+ MCQs on "Data Structure - II (Algorithms)") focus on all areas of Data Structure covering 200+ topics. One can read MCQs on Data Structure - II (Algorithms)                 Array and Array Operations This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Array and Array Operations”. 1. Which of these best describes an array? a) A data structure that shows a hierarchical behavior b) Container of objects of similar types c) Arrays are immutable once initialised d) Array is not a data structure View Answer Answer: b Explanation: Array contains elements only of the same type. 2. How do you initialize an array in C? a) int arr[3] = (1,2,3); b) int arr(3) = {1,2,3}; c) int arr[3] = {1,2,3}; d) int arr(3) = (1,2,3); View Answer Answer: c Explanation: This is the syntax ...

Write a C program to find number is Abundant number or not

  Write a C program to find the number is an Abundant number or not:- In this program to find a number is an Abundant number or not. A number n is said to be an Abundant Number to follow these condition the sum of its proper di visors is greater than the number itself. And the difference between these two values is called abundance. Ex:-  Abundant number  12 having a proper divisor is 1,2,3,4,6 the sum of these factors is 16 it is greater than 12 so it is an Abundant number. Some other abundant numbers     18, 20, 24, 30, 36, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100, 102, 104, 108, 112, 114, 120.. ALGORITHMS:- Step 1 - Enter the number, to find the Abundant number. Step 2 - Initialize the loop with c=1 to c<=number and follow the following calculation      (i) check if whether the number is divisible with c and c got a result zero.      (ii) now sum=sum+c, add a digit into a sum and store it in the sum. Step 3 . then the...

sWAP cASE

  sWAP cASE: You are given a string and your task is to  swap cases . In other words, convert all lowercase letters to uppercase letters and vice versa. For Example: Www.HackerRank.com → wWW.hACKERrANK.COM Pythonist 2 → pYTHONIST 2 Function Description Complete the  swap_case  function in the editor below. swap_case  has the following parameters: string s:  the string to modify Returns string:  the modified string Input Format A single line containing a string  . Sample Input 0 HackerRank.com presents "Pythonist 2". Sample Output 0 hACKERrANK.COM PRESENTS "pYTHONIST 2". def   swap_case ( s ):      result =  s . swapcase ()      return ( result ) if  __ name__  ==  '__main__' :      s  =  input ()      result  =  swap_case ( s )      print ( result )