Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Searching - DSA | Part 3 |
Madhuban Khatri
Madhuban Khatri

Posted on • Edited on

     

Searching - DSA | Part 3 |

Searching is the process to find the element and its position in the given array/list.

We'll discuss two searching algorithms which areLinear Search &Binary Search and finding which one is better.


Linear Search

Linear Search is a basic searching algorithm. It searches an element from the entire list.

Linear Search

It is very time consuming algorithm because if we search the last index of element , then this algorithm will start searching from beginning and it traverses the entire list until the element is found.

For example, if we have a list which contains 100 elements and we want to find 100th element of the list, then Linear search will compare each element one by one and it will do 100 comparison which is very time consuming.

Algorithm

LinearSearch(array, key)  for each item in the array    if item == value      return index
Enter fullscreen modeExit fullscreen mode

Implimentation of Linear Search

# Linear SearchdeflinearSearch(array,size,key):foriinrange(0,size):if(array[i]==key):returnireturn-1array=[2,4,0,1,9]key=1size=len(array)result=linearSearch(array,size,key)if(result==-1):print("Element not found")else:print("Element found at index:",result)
Enter fullscreen modeExit fullscreen mode

Time Complexity

Time complexity of Linear Search isO(n) because in the algorithm loop will run 'n' times to search the element.

Space Complexity

Space Complexity of Linear Search isO(1) because it does not take extra space/memory while searching.


Binary Search

Binary Search is a searching algorithm for finding an element's position in asorted array.

In this approach, the element is always searched in the middle of a portion of an array.

Binary Search

Algorithm

do until the pointers low and high meet each other.    mid = (low + high)/2    if (x == arr[mid])        return mid    else if (x > arr[mid]) // x is on the right side        low = mid + 1    else                       // x is on the left side        high = mid - 1
Enter fullscreen modeExit fullscreen mode

Implimentation of Binary Search

# Binary SearchdefbinarySearch(array,key,low,high):whilelow<=high:mid=low+(high-low)//2ifarray[mid]==x:returnmidelifarray[mid]<x:low=mid+1else:high=mid-1return-1array=[3,4,5,6,7,8,9]key=4result=binarySearch(array,key,0,len(array)-1)ifresult!=-1:print("Element is present at index"+str(result))else:print("Not found")
Enter fullscreen modeExit fullscreen mode

Time Complexity of Binary Search

  • Best Case - O(1)
  • Average Case - O(log n)
  • Worst Case - O(log n)

Space Complexity of Binary Search

Space Complexity of Binary Search is O(1) because it does not take extra space/memory while searching.


So that's it for today. I hope you like this post. Share this post to your friends/relatives to spread the knowledge.

Thank you.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

PYTHON | DJANGO | JAVASCRIPT | ReactJs | PHP |work at ReliableSoft pvt. ltd. , Jodhpur
  • Location
    Jaisalmer, Rajasthan
  • Education
    Graduate in BSc. Mathemetics in Science Stream
  • Pronouns
    Mohit
  • Work
    Fresher | Self-Taught Programmer | YouTuber
  • Joined

More fromMadhuban Khatri

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp