课程: C# Algorithms

Linear search arrays in C# - C#教程

课程: C# Algorithms

Linear search arrays in C#

- [Narrator] As a software engineer, you'll need to search through data structures, using algorithms in order to retrieve relevant data. With a search algorithm, you often input the data you need to search and the item you're trying to find. Sometimes the algorithm will return true or false depending on whether the item exists or it will return the item itself with more data, let's take a look at how we can search for data in the array data structure. One option is to do a linear search, also called a sequential search, where we look at each item in the array. One at a time, in order, we'll do this using a for each loop. For each integer in the array, we check if it matches the thing we're looking for, N. If so, we return true. If we search through the entire array and still cannot find N then it must not exist in the array and we can return false. Another option is to return the data itself, instead of a Boolean. If we find the data we'll return N, if we don't find the data, we can return a Sentinel value. This is a value that will never be the value of N and it can serve as a not found value. Both of these will be ENTs. So we'll need to change the return type to ENT. Instead of returning a Sentinel value, we could also return null, but we need to add a question mark to the return type. This lets null be a proper value to return from the function. Let's try running it. Four does exist in the array and eight does not exist. Let's execute our program. In the first case, we get the item back. In the second case, eight is not in the array, so we just get nothing. If we change this back to a Boolean, execute the program again, we'll see true and false in the output. Now this type of linear search is actually pretty common. There are a few built-in functions that we can use for this behavior. One is called find and it lives in the array class. The find function takes in an array and a predicate or a function that returns true or false. That's what it'll use to find the appropriate data. So we'll pass in the array and we'll have it find the number equal to three. The function will take in an element and it will return true for elements equal to three. Since it's finding the data, it will actually return the value three. So we'll create a variable called item. If three exists in the array, item will get the value three. If three does not exist in the array, the item variable will get the value zero. Now this works for more than just a quality. We can check whether the array has an item greater than five or any type of conditional where we want to find an element that meets a certain condition. The find function returns only the first item that meets this condition, but we can use, find all, to return all the data that meets the condition. It lives in array, we'll pass in the array and then the condition, whether the element is greater than or equal to five. With find all, we gather all the elements in the array that are greater than or equal to five. They're returned to us in an array. If no items meet the condition, the array will be empty, to display the items or print the contents of the items array, we can use the built-in for each function. For each item, we'll print it to the console. Let's comment out these lines. And we'll also display whether we find the item three, let's run it. In the output, we get three because it does exist in the array. Then we see all the items that are greater than or equal to five. There are some alternatives to find and find all that work with array indices. However, I find these two functions to be most useful when searching for data within arrays.

内容