General

Computer Science

  1. 1. Introduction to Computer Science
  2. Legacy Course

  3. Introduction to Computer Science
  4. History of Computer Science
  5. Fundamentals of Computer Science
  6. Algorithms
  7. Data Structures
  8. Programming Concepts
  9. Web Development
  10. Databases and SQL
  11. Networking and Security
  12. Artificial Intelligence and Machine Learning
  13. Mobile App Development
  14. Game Development
  15. Future of Computer Science
  16. Careers in Computer Science

Searching Algorithms

Module Progress
0 / 52 Lessons
0%
Learning

Searching algorithms are methods used to locate a specific piece of data within a larger collection of data. Two common searching algorithms are linear search and binary search.

Linear search, also known as sequential search, is a simple searching algorithm that starts at the first element of a collection and compares each element to the target item. If the element matches the target, the search is complete. If not, the search moves to the next element until the target is found or the end of the collection is reached.

The time complexity of linear search is O(n), where n is the number of elements in the collection. This means that, on average, the search will take n/2 comparisons to find the target item. The space complexity of linear search is O(1), as it only requires a single variable to store the current element being compared.

Binary search, on the other hand, is a more efficient searching algorithm that can only be used on sorted collections. The algorithm starts by finding the middle element of the collection and comparing it to the target item. If the middle element is the target, the search is complete. If the target is smaller than the middle element, the search continues on the left half of the collection. If the target is larger than the middle element, the search continues on the right half of the collection. This process is repeated until the target is found or the search reaches an empty collection.

The time complexity of binary search is O(log n), where n is the number of elements in the collection. This means that, on average, the search will take log(n) comparisons to find the target item. The space complexity of binary search is also O(1), as it only requires a single variable to store the current middle element.

Continue learning with Knowness

Sign up to access the full lesson, predicted grades, revision tools, progress tracking, and more.

Create a free account