Code Monkey home page Code Monkey logo

search-algorithm's Introduction

Linear Search and Binary search

Aim:

To write a program to perform linear search and binary search using python programming.

Equipment’s required:

  1. Hardware – PCs
  2. Anaconda – Python 3.7 Installation / Moodle-Code Runner

Linear Search Algorithm:

  1. Start from the leftmost element of array[] and compare k with each element of array[] one by one.
  2. If k matches with an element in array[] , return the index.
  3. If k doesn’t match with any of elements in array[], return -1 or element not found.

Binary Search Algorithm:

  1. Set two pointers low and high at the lowest and the highest positions respectively.
  2. Find the middle element mid of the array ie. arr[(low + high)/2].
  3. If x == mid, then return mid.Else, compare the element to be searched with m.
  4. If x > mid, compare x with the middle element of the elements on the right side of mid. This is done by setting low to low = mid + 1.
  5. Else, compare x with the middle element of the elements on the left side of mid. This is done by setting high to high = mid - 1.
  6. Repeat steps 2 to 5 until low meets high

Program:

i) #Use a linear search method to match the item in a list.

''' 
Program for linear search method to match the item in a list
Developed by: Paarkavy.B
RegisterNumber: 21500424
'''
def linearSearch(array,n,k):
    for i in range(0,n):
        if array[i]==k:
            return i
    return -1       
array = eval(input())
array.sort()
k = eval(input()) 
n=len(array)
result = linearSearch(array,n,k)
if result >=0:
    print(array)
    print('Element found at index: ',result)
else:
    print(array)
    print('Element not found')

ii) # Find the element in a list using Binary Search(Iterative Method).

''' 
Program to find the element in a list using Binary Search(Iterative Method)..
Developed by: Paarkavy.B
RegisterNumber: 21500424
'''
def binarySearchIter(array, k, low, high):
    while(low<=high):
        mid=low+(high-low)//2
        if array[mid]==k:
            return mid
        elif array[mid]<k:
            low=mid+1
        else:
            high=mid+1
    return -1  
array=eval(input())
array.sort()
k = eval(input())
low=0
high=len(array)-1
result=binarySearchIter(array,k,low,high)
if result>=0: 
   print(array)
   print("Element found at index: ",result)
else:
    print(array)
    print("Element not found")

iii) # Find the element in a list using Binary Search (recursive Method).

''' 
Program to find the element in a list using Binary Search (recursive Method).
Developed by: Paarkavy.B
RegisterNumber: 21500424
'''
def BinarySearch(arr, k, low, high):
    if high>=low:
        mid=low+(high-low)//2
        if arr[mid]==k:
            return mid
        elif arr[mid]<k:
            return BinarySearch(arr,k,mid+1,high)
        else:
            return BinarySearch(arr,k,low,mid+1)
    return -1
arr = eval(input())
arr.sort()
print(arr)
k = eval(input()) 
low=0
high=len(arr)-1
result=BinarySearch(arr, k, low, high)
if result>=0:
    print("Element found at index: ",result)
else:
    print("Element not found")

Output:

output output output

Result:

Thus the linear search and binary search algorithm is implemented using python programming.

search-algorithm's People

Contributors

paarkavyb avatar etjabajasphin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.