Your algorithm for the search must meet certain requirements:

  1. correctness - it must return the correct results that are being sought, every time

  2. applicability - it must be appropriate for the source type and data needs

    1. 因为搜索的对象可能是 Excel、XML、JSON 或关系型数据库等
  3. speed - it must execute in a timely manner based on your requirements for searching

Linear Search

The basic through process in a linear search is this:

  1. set a counter equal to 0

  2. check the item at position (counter) and if it equals search term, end the algorithm

  3. if search term is not found in step 2, increment counter and repeat step 2

Linear search.mp4

  1. static void LineSearchDemo()
  2. {
  3. // declare an array of integers that will be used for searching
  4. int[] searchArray = { 5, 10, 3, 2, 4 };
  5. int term = 2;
  6. int index = LinearSearch(searchArray, term);
  7. if (index != -1)
  8. {
  9. Console.WriteLine($"Your search term was found at position {index}");
  10. }
  11. else
  12. {
  13. Console.WriteLine("Your search term was not found.");
  14. }
  15. }
  16. public static int LinearSearch(int[] arr, int term)
  17. {
  18. // set up a loop to iterate over the elements in the array
  19. // because this is a linear search, we start at the beginning
  20. // of the array and work through the elements looking for the term
  21. for (var i = 0; i < arr.Length; i++)
  22. {
  23. // if we found a match, we can return the index in the array where the term is
  24. if (arr[i] == term)
  25. {
  26. return i;
  27. }
  28. }
  29. // if the term is not found, we reach the end of the array
  30. // and as a result, we return -1 indicating that the term wasn't found
  31. // at a valid index in the array.
  32. return -1;
  33. }

Binary Search

只能对有序列表使用 Binary Search。

The logic behind this search goes something like this:

  1. Get the list to search

  2. Find the mid-point and get an index to mark that mid-point

  3. Compare the value at the mid-point and the search term

  4. If the two match, the search is done. If not, proceed to the next step

  5. Is the term greater or less than the mid-point value?

    1. if greater, get index for mid-point between current mid-point and end of list. Repeat steps 3 to 5 and i. or ii. until term is found or not

    2. if less than, get the index for mid-point between current mid-point and beginning of list. Repeat steps 3 to 5 and i. or ii. until term is found or not

  6. If term is found above, end and return index of term

  7. If term is not found above, end and return search failed result

Binary Search.mp4

  1. static void BinarySearchDemo()
  2. {
  3. // declare an array of integers that will be used for searching
  4. int[] arrLinearSearch = { 2, 3, 4, 5, 8, 10 };
  5. var searchTerm = 8;
  6. var result = BinarySearch(arrLinearSearch, searchTerm, 0, arrLinearSearch.Length - 1);
  7. if (result != -1)
  8. {
  9. Console.WriteLine($"Search term found at index {result}.");
  10. }
  11. else
  12. {
  13. Console.WriteLine("Search term not found in the array.");
  14. }
  15. }
  16. public static int BinarySearch(int[] arr, int key, int left, int right)
  17. {
  18. while (left <= right)
  19. {
  20. var mid = (left + right) / 2;
  21. if (key == arr[mid])return mid;
  22. if (key < arr[mid])
  23. {
  24. right = mid - 1;
  25. }
  26. else
  27. {
  28. left = mid + 1;
  29. }
  30. }
  31. return -1;
  32. }