🔷 C#

C# (.NET) 中的搜索算法:详细指南

What is it and why? (a real-life analogy)

Imagine you walk into a huge library with thousands of books on the shelves. You need to find the book "The Adventures of Tom Sawyer". How would you search for it?

The first way (silly, but possible): You walk along the shelves and look through every book in a row until you find the right one. This will take a lot of time, especially if the book is at the very end.

The second way (smart): You go to the catalog, where all the books are arranged alphabetically. You open the "T" section, find the author's last name, and then the exact title. In a minute, the book is in your hands.

The same thing happens in programming. A search algorithm is a clear instruction that helps a computer find the needed data among many others. Without search algorithms, search engines (Google, Yandex) wouldn't work, files on your computer wouldn't open, and social networks couldn't quickly show your friends.

💡 Important: Search algorithms are not just "find a number in a list". They are the foundation of databases, file systems, search engines, and even artificial intelligence.

In this article, we will analyze the two most popular search algorithms: linear (sequential) and binary. You will learn how they work, when to apply which, and learn to write them in C#.

Theory: how it works from the inside

Linear Search

This is the simplest algorithm. Imagine you have an array of numbers and you are looking for the number 7. Linear search works like this:

  1. Take the first element of the array.
  2. Compare it with the target number.
  3. If it matches — great, we found it!
  4. If not — move to the next element.
  5. Repeat steps 2-4 until we find the target number or check the entire array.

Pros:

  • Works with any data (even unsorted).
  • Very simple to write and understand.

Cons:

  • Very slow on large amounts of data. If the array has a million elements and the target is the last one, you'll have to check a million times.

Binary Search

This algorithm is similar to searching for a word in a thick dictionary. You don't read every page in a row. You open the book roughly in the middle, see which half contains the target word, and discard the other half. Then you repeat the same with the remaining part.

How it works on an array of numbers:

  1. The array must be sorted in ascending order.
  2. Find the middle of the array.
  3. Compare the middle element with the target number.
  4. If the middle element equals the target — hooray, found it!
  5. If the target number is less than the middle — search in the left half.
  6. If the target number is greater than the middle — search in the right half.
  7. Repeat steps 2-6 until we find the number or the range becomes empty.

Pros:

  • Very fast. With each step, the search area is halved.
  • For an array of 1 million elements, only ~20 checks are needed (instead of a million).

Cons:

  • Works only with sorted data.
  • More complex to implement (but we'll break it down step by step).

Speed comparison (Big O notation)

Algorithm Best case Average case Worst case
Linear Search O(1) — element is first O(n) — somewhere in the middle O(n) — element is last
Binary Search O(1) — element is in the center O(log n) — logarithmic complexity O(log n) — element is absent
⚠️ Remember: Binary search only works on sorted data. If the data is not sorted — sort it first, then search. Or use linear search.

Syntax in C# (.NET): all forms and variants

In C#, there are several ways to implement search. We will look at:

  • Manual implementation (to understand how everything works internally).
  • Built-in .NET methods (for real work).
  • Generic methods for working with any data types.

Built-in .NET methods for search

Method Description Example
Array.IndexOf() Linear search in an array. Returns the index of the first occurrence or -1. int index = Array.IndexOf(numbers, 7);
Array.BinarySearch() Binary search in a sorted array. Returns the index or a negative number. int index = Array.BinarySearch(sortedNumbers, 7);
List<T>.IndexOf() Linear search in a list. int index = myList.IndexOf(7);
List<T>.BinarySearch() Binary search in a sorted list. int index = myList.BinarySearch(7);
Enumerable.Contains() Checks if an element exists in a collection (linear search). bool exists = numbers.Contains(7);
Enumerable.FirstOrDefault() Finds the first element matching a condition. var result = numbers.FirstOrDefault(x => x == 7);