~ Searching Algorithms in Python

Sorting Algorithms - Python Code Sorted

An introduction to Searching Algorithms

In this series we provide a

  • video,
  • explanation,
  • flow chart and
  • python code implementation 
  • suggested learning activities (download worksheet with ideas for classroom activities here)

for the various searching algorithms.

Searching is integral to life - we search for things all the time - or at least I do! As often is the case, just as it is in life, so it is in computer science and searching plays a very important role when it comes to working with data.  a search algorithm is any algorithm which solves the Search problem, namely, to retrieve information stored within some data structure, or calculated in the search space of a problem domain. Examples of such structures include but are not limited to a Linked List, an Array data structure, or a Search tree. The appropriate search algorithm often depends on the data structure being searched, but also on any a priori knowledge about the data. Searching also encompasses algorithms that query the data structure, such as the SQL SELECT command.

This series will primarily look at python code for the various searching algorithms, along with an explanation (video or presentation) for each. If you're really interested however, there is plenty of additional information on this topic below:

From http://www.geeksforgeeks.org/searching-algorithms/#algo and BBC Bitesize

Why Searching Algorithms?

We often need to find one particular item of data amongst many hundreds, thousands, millions or more. For example, you might need to find someone’s phone number on your phone, or a particular business’s address in the UK.

This is why searching algorithms are important. Without them you would have to look at each item of data – each phone number or business address – individually, to see whether it is what you are looking for. In a large set of data, it will take a long time to do this. Instead, a searching algorithm can be used to help find the item of data you are looking for.

Searching Algorithms :

  1. Linear Search
  2. Binary Search
  3. Jump Search
  4. Interpolation Search
  5. Exponential Search
  6. Sublist Search (Search a linked list in another list)
  7. Fibonacci Search
  8. The Ubiquitous Binary Search
  9. Recursive program to linearly search an element in a given array
  10. Recursive function to do substring search
  11. Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time)

Comparisons :

  1. Linear Search vs Binary Search
  2. Interpolation search vs Binary search
  3. Why is Binary Search preferred over Ternary Search?

Library Implementations of Searching Algorithms :

  1. Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
  2. Arrays.binarySearch() in Java with examples | Set 1
  3. Arrays.binarySearch() in Java with examples | Set 2 (Search in subarray)
  4. Collections.binarySearch() in Java with Examples

Coding Problems :

  1. Find the Missing Number
  2. Search an element in a sorted and rotated array
  3. Median of two sorted arrays
  4. Two elements whose sum is closest to zero
  5. Find the smallest and second smallest element in an array
  6. Maximum and minimum of an array using minimum number of comparisons
  7. k largest(or smallest) elements in an array | added Min Heap method
  8. Ceiling in a sorted array
  9. Count number of occurrences (or frequency) in a sorted array
  10. Find the repeating and the missing | Added 3 new methods
  11. Find a Fixed Point in a given array
  12. Find the maximum element in an array which is first increasing and then decreasing
  13. Find a pair with the given difference
  14. Find the k most frequent words from a file
  15. Median of two sorted arrays of different sizes
  16. Find a peak element
  17. Given an array of of size n and a number k, find all elements that appear more than n/k times
  18. Find the minimum element in a sorted and rotated array
  19. Kth smallest element in a row-wise and column-wise sorted 2D array | Set 1
  20. Find k closest elements to a given value
  21. Search in an almost sorted array
  22. A Problem in Many Binary Search Implementations
  23. Find the first repeating element in an array of integers
  24. Find common elements in three sorted arrays
  25. Count 1’s in a sorted binary array
  26. Given a sorted array and a number x, find the pair in array whose sum is closest to x
  27. Find the closest pair from two sorted arrays
  28. K’th Smallest/Largest Element in Unsorted Array | Set 1
  29. K’th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
  30. K’th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
  31. Find position of an element in a sorted array of infinite numbers
  32. Given a sorted and rotated array, find if there is a pair with a given sum
  33. Find the largest pair sum in an unsorted array
  34. Find the nearest smaller numbers on left side in an array
  35. K’th largest element in a stream
  36. Find a pair with maximum product in array of Integers
  37. Find the element that appears once in a sorted array
  38. Find the odd appearing element in O(Log n) time
  39. Find the largest three elements in an array
  40. Search an element in an array where difference between adjacent elements is 1
  41. Find three closest elements from given three sorted arrays
  42. Find the element before which all the elements are smaller than it, and after which all are greater
  43. Binary Search for Rational Numbers without using floating point arithmetic
  44. Floor in a Sorted Array
  45. Third largest element in an array of distinct elements
  46. Second minimum element using minimum comparisons
  47. Queries for greater than and not less than
  48. Efficient search in an array where difference between adjacent is 1
  49. Print all possible sums of consecutive numbers with sum N
  50. Minimum time required to produce m items
  51. Make all array elements equal with minimum cost
  52. Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
  53. Check if reversing a sub array make the array sorted
  54. Find all triplets with zero sum
  55. Search, insert and delete in an unsorted array
  56. Search, insert and delete in a sorted array
  57. Move all occurrences of an element to end in a linked list
  58. Search in an array of strings where non-empty strings are sorted
  59. Smallest Difference Triplet from Three arrays
  60. Best First Search (Informed Search)

Quick Links :

  1. ‘Practice Problems’ on Searching
  2. ‘Quizzes’ on Searching
  3. Ask a Question on Searching