Day 40: Searching algorithms with Python

Day 40: Searching algorithms with Python

Navigating Data: Exploring Searching Algorithms in Python

ยท

2 min read

Today marks the fortieth day of my Python learning series. In this blog post, I am excited to share my latest discoveries on essential searching algorithms. These algorithms play a pivotal role in virtually every project, making them a fundamental aspect of programming.

Searching Algorithms

  1. Linear Search: In linear search, we traverse elements one by one, systematically checking each element until we find the desired item.

     list = [ 5 , 10 , 15, 20, 25 ]
     target = 200
     def findInList(l: list):
         for e in l:
             if(e == target):
                 return True;
         return False
     print(findInList(list))
    
  2. Binary Search: Binary search is a divide-and-conquer algorithm. It involves dividing the sorted list into two halves and then efficiently determining which half the target element resides in, narrowing down the search space until the element is found.

     def binarySearch(list, target):
         start = 0;
         end = len(list)
         while(start < end):
             mid = (start + end) / 2
             mid = int(mid)
             print(mid)
             if list[mid] > target:
                 end = mid - 1
             elif list[mid] < target:
                 start = mid + 1
             elif list[mid] == target:
                 print("index: ",mid)
                 return;
    
     list = [ 5 , 10 , 15 , 20 ]
     binarySearch(list, 15)
    

These are two very important search algorithms that are very used in developing any app.

Conclusion

Conclusion: This blog post explored two critical searching algorithms: Linear Search and Binary Search. Here's a concise summary:

  1. Linear Search: This algorithm checks each element sequentially until finding the target. It's simple but less efficient for large datasets due to its linear time complexity.

  2. Binary Search: Operating on sorted lists, Binary Search divides the search space in half with each iteration, resulting in logarithmic time complexity. It's highly efficient for large datasets.

Mastering these algorithms is crucial for optimizing data retrieval and enhancing application performance. Continued practice and exploration of algorithms are essential for skill development in programming.

Thank you๐Ÿ’•๐Ÿ’•

ย