If the target value is not found, the method will return -1. If the element is equal to the target value, the index of the element is returned. The method iterates through the array using a for loop, and compares each element to the target value. The LinearSearch method takes two parameters, an array of integers and the target value. This example creates an array of integers and searches for the number 5. WriteLine ( "The number 5 is at index " + index ) In the first, the matching doesn’t happen.Console. Step 1: The algorithm begins from the left-hand side, and the element to be searched is matched with every element. Now, suppose we want to search 92 in the above-mentioned array, the linear search algorithm shall follow the steps mentioned below. Let’s consider the following array to understand the working of the algorithm. 102 is not present in the array, and the program gave correct output saying that the number doesn’t exist in the array. After this, we specified the number to be searched, which is 102. As shown below, we decided to have eight elements in the array, and then specified the eight elements. However, the program should work correctly if the element is not present. Till now, we saw the program correctly returning the position of the element present in the array. As the number 245 is present in the list, so, the program correctly returned its position in the array. Then we specified number to be searched in the array, which is 245. Here, we passed eight three-digit numbers into the array. Go through the following output and see how the correct result has been obtained. 29 is present in the array, and the program successfully gave its position, which is 14. Once done with the array, specify the requisite number to be searched. This is especially important when the number of elements in the array is high. After specifying the number of elements in the array, while passing the elements, ensure that the required number of elements are only passed. Follow the steps and pass the inputs properly. In this case, we passed twenty-one elements into the array. As 98 is present in the array, its position has been returned correctly by the program. Once, we pass the entire array correctly next, we will specify the number that we intend to search in the array. Also, while passing elements, they must be separated by space. Passing input of different data types may give incorrect results. Next, we passed ten different numeric elements in the array. In this case, we decided to have ten elements in the array, and so, specified 10 when asked to specify the number of elements in the array. The inputs passed and the respective results obtained have been discussed in the below section. We validate the program by passing multiple inputs. For this, the program should be checked by passing multiple parameters to it. We must verify and validate the correctness of the implemented program. If the element is found in the array, then the function linear_search() returns the element’s position, and if the element is not found in the array, then -1 is returned. For this task, a function linear_search() has been used as seen in the code. The program using loop sequentially searches for the desired element. Once the array is specified, the user is asked to specify the element that needs to be searched in the array in the next step. Printf("\n%d is present at position %d in the array.", x, res) Ĭode Explanation: The above program first asks the user to specify the number of elements in the array along with the elements. Printf("\n%d does not exist in the array.", x) Printf("\nEnter the number to be searched: ") Printf("Enter the number of elements in array: ") Function linear_search(integer array, integer n, integer x)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |