50 likes | 69 Views
An algorithm is a method or procedure to solve a problem step by step by a computer. The different steps of the algorithm can include repetition. It depends on the problem for which the algorithm is made. The algorithm is written in a form that can be understood and read by humans. If you want to make Searching algorithms in the data structure (array) then there are two ways to do this. These are linear search and Binary search. To know more visit https://medium.com/@helpmestudyrbo/searching-algorithms-in-data-structure-d78bf0b65862
E N D
Searching algorithms in data structure An algorithm is a method or procedure to solve a problem step by step by a computer. The different steps of the algorithm can include repetition. It depends on the problem for which the algorithm is made. The algorithm is written in a form that can be understood and read by humans. If you want to make Searching algorithms in the data structure (array) then there are two ways to do this. These are linear search and Binary search. Linear search Linear search is the most simple and basic searching algorithms in data structure In a linear search, an element or a value is searched in an array by searching elements one by one, till the desired element is found. In linear search, search happens in sequential order. It compares the desired elements with all elements of the array and if the value is matched it returns the index of value else it returns -1. Linear search can be applied on an unordered and unsorted list with fewer elements.
Example: To search an element 9 in the given list, it will search step by step in sequence order. function findIndex(values, target) { for(var i = 0; i < values.length; ++i) { if (values[i] == target) { return I; } } return -1; } //call the function findIndex with array and number to be searched findIndex([ 2 , 4 , 6 , 8 , 9 ] , 9) ;
Binary Search A binary search algorithm can be implemented for the sorted list or array. In binary search, we compare the desired elements with the values that present in the middle of the array. If the desired element is matched, then the binary search returns the value. If the desired value is less than the middle value, then continue the search in the lower half of the list. If the value is greater than the middle element then continue the search in the upper half of the list or array. In binary search, we repeat this process of searching in the lower and upper half, until the element is found. Example Search an element 9 from a sorted list or array. function findIndex(values, target) { return binarySearch(values, target, 0, values.length - 1); };
function binarySearch(values, target, start, end) { if (start > end) { return -1; } //does not exist var middle = Math.floor((start + end) / 2); var value = values[middle]; if (value > target) { return binarySearch(values, target, start, middle-1); } if (value < target) { return binarySearch(values, target, middle+1, end); } return middle; //found! } findIndex([2, 4, 6, 8, 9, 10], 9); In the above programming of binary search, at first, we are comparing the desired element with the middle element. If the value matches, we return. If not, we see if the middle element is is greater or smaller than the desired element. If the middle element is greater than the desired element, we start our search again, this time on the left half of the array. Means start searching from the start to the middle of the list. If the middle value is smaller than the desired value, we start our search again, this time on the right half of the array. It means start searching from the middle of the array to the end.
If you want to learn more about searching algorithms in the data structure, visit the e-learning website Help me study bro. Help me study bro is one of the tops and most trusted e-learning platforms for the data structure. On this platform, you will get all the notes, problem-solving questions, numerical and other study materials. Learn data structure and algorithm in deep at Help me study bro and become master of programming. A-109, 3rd floor, kaushambi, Pin code – 201010 +91-98737 95550 https://www.helpmestudybro.com/