Breaking Posts

6/trending/recent

Hot Widget

Type Here to Get Search Results !

Searching Algorithms for Array.

Searching Algorithms for Array in Data Structure

  • An algorithm is a step-by-step approach or method for a computer to solve a problem in a specific number of steps. 
  • Depending on the problem for which the algorithm is being designed, the steps of an algorithm may entail repetition. 
  • The algorithm is written in a way that is easy to read and understand. 
  • There are two methods for finding an element in an array: linear search and binary search.



Linear Search

  • The simplest and most basic search algorithm is a linear search. 
  • A linear search examines an array for an element or value until the requested element or value is not found, and it does so in order. 
  • It compares the element to all of the other items in the list and returns the value index if the element is matched, otherwise it returns -1. 
  • When there are fewer elements in a list, Linear Search is used on the unsorted or unordered list.

Example with Implementation

  • To search element 5 it will go 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([ 8 , 2 , 6 , 3 , 5 ] , 5) ;

Binary Search

  • On a sorted array or list, Binary Search is used. In binary search, we compare the value to the elements in the array's middle place. 
  • We return the value if the value is matched. If the value is less than or equal to the center element, it must be in the bottom half of the array; if it is more than or equal to the element, it must be in the upper half. 
  • On the bottom (or upper) half of the array, we repeat the procedure. 
  • When an array contains a huge number of elements, Binary Search comes in handy.

Example with Implementation

  • To search an element 13 from the sorted array or list.

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, 7, 9, 13, 15], 13);
  • We compare the middle number of the list to the target in the previous programme logic, and if they match, we return. If it doesn't, we check to see if the middle value is larger or smaller than the target.
  • If the Middle number is bigger than the Target, the binary search is restarted, but this time on the left half of the list, from the beginning to the middle, not beyond.
  • If the Middle number is less than the Target, we repeat the binary search, but this time on the right half of the list, from the middle to the finish.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.