![]() 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: ") This speed disadvantage is why other search methods have been developed.Function linear_search(integer array, integer n, integer x) For example, when searching through a database of everyone in the Northern Ireland to find a particular name, it might be necessary to search through 1.8 million names before you found the one you wanted. As other searching algorithms may have to reorder the list after insertions or deletions, this may sometimes mean a linear search will be more efficient. As the linear search does not require the list to be sorted, additional elements can be added and deleted.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |