Posts

Showing posts with the label Data-structure

Binary Search - Algorithm

Image
 Binary search is a fastest search algorithm with o(log n) time complexity.this algorithm is based on divide and conquer.the searching technique required collection in sorted meaner. this is biggest disadvantage of binary search. suppose we need to search on array then first we need to sort array and the perform search operation on that array. the solution in this case is first sort array and the use Binary search on the array. Algorithm : Step 1: initialize low = 0, high = size of collection step 2 : while low <= high then calculate the mid i.e mid = low + high/2 step 3 : if(searchValue == array[mid]){print Element found} step 4 : if(searchValue > array[mid]){low = mid + 1} otherwise high = mid -1 step 5 : Exit Programmatic Representation : while(low <= high){     mid = low+high/2;     if(x== a[mid]){          Print : Element Found.          break;      }  ...

Liner Search - Algorithm

Image
Liner search is an simple and  Sequential searching technique. this search, a sequential search where every item is checked and if match is found then it return the element other wise the search continues till the end of the collection i.e size of element.it check all the item one by one.the complexity of liner search is O(n). Algorithm : Linear Search (A,x,i,n) Step 1: Set i to 0 Step 2: if i > n then Print Element Not found and Go to step 6 Step 3: if A[i] = x then Print Element is not found and Go to step 6 Step 4: Set i to i + 1 Step 5: Repeat 2,3,4 till n or element not found Step 6: Exit Example : public class LinerSrarchExample { private static void search(int[] numberArray,int searchNumber) { int n= numberArray.length; for(int i=0;i<n;i++) { if(i>n) { System.out.println("Element not found."); } if(numberArray[i]==searchNumber) { System.out.println("Element found at "+ i +" Position"); break; } } }...