0% found this document useful (0 votes)
10 views15 pages

Lec 1

The document compares linear and binary search algorithms. Linear search checks each item sequentially, while binary search uses a divide and conquer approach on sorted arrays, significantly reducing time complexity to O(log n). It includes pseudocode and Java implementations for both algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

Lec 1

The document compares linear and binary search algorithms. Linear search checks each item sequentially, while binary search uses a divide and conquer approach on sorted arrays, significantly reducing time complexity to O(log n). It includes pseudocode and Java implementations for both algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SEARCHING

ALGORITHMS
LINEAR VS. BINARY ALGORITHM
Linear Search

 Linear search is a very simple search algorithm. In this type of


search, a sequential search is made over all items one by one.
Every item is checked and if a match is found then that particular
item is returned, otherwise the search continues till the end of the
data collection.
Algorithm Vs. Pseudocode
Algorithm Pseudocode
Linear Search ( Array A, Value x)
 procedure linear_search (list, value)
Step 1: Set i to 1
 Step 2: if i > n then go to step 7
 Step 3: if A[i] = x then go to step 6 for each item in the list
 Step 4: Set i to i + 1 if match item == value
 Step 5: Go to Step 2 return the item's location
 Step 6: Print Element x Found at index i end if
and go to step 8 end for
 Step 7: Print element not found
end procedure
 Step 8: Exit
Java Implementation

1. public class LinearSearchExample{


2. public static int linearSearch(int[] arr, int key){
3. for(int i=0;i<arr.length;i++){
4. if(arr[i] == key){
5. return i;
6. }
7. }
8. return -1;
9. }
10. public static void main(String a[]){
11. int[] a1= {10,20,30,50,70,90};
12. int key = 60;
13. int index = linearSearch(a1, key) ;
14. System.out.println(key+" is found at index: "+(index>-1 ?index:"not found") );
15. }
16. }
Binary Search

 Binary search is a fast search algorithm with run-time complexity of


Ο(log n). This search algorithm works on the principle of divide and
conquer. For this algorithm to work properly, the data collection
should be in the sorted form.
 Binary Search is defined as a searching algorithm used in a sorted
array by repeatedly dividing the search interval in half. The idea of
binary search is to use the information that the array is sorted and
reduce the time complexity to O(log N).
How Binary Search Works?

 For a binary search to work, it is mandatory for the target array to be


sorted. We shall learn the process of binary search with a pictorial
example. The following is our sorted array and let us assume that we
need to search the location of value 31 using binary search.
Algorithm

Determine half of the array by using this formula

mid = low + (high - low) / 2

Compare the value stored at location 4


Algorithm

The algorithm follows the procedure below −


Step 1 − Select the middle item in the array and compare it with the key
value to be searched. If it is matched, return the position of the median.
Step 2 − If it does not match the key value, check if the key value is either
greater than or less than the median value.
Step 3 − If the key is greater, perform the search in the right sub-array; but
if the key is lower than the median value, perform the search in the left sub-
array.
Step 4 − Repeat Steps 1, 2 and 3 iteratively, until the size of sub-array
becomes 1.
Step 5 − If the key value does not exist in the array, then the algorithm
returns an unsuccessful search.
Pseudocode

Procedure binary_search set midPoint = lowerBound + ( upperBound -


lowerBound ) / 2
A ← sorted array
if A[midPoint] < x
n ← size of array
set lowerBound = midPoint + 1
x ← value to be searched
if A[midPoint] > x
Set lowerBound = 1
set upperBound = midPoint - 1
Set upperBound = n
if A[midPoint] = x
while x not found
EXIT: x found at location midPoint
if upperBound < lowerBound
end while
EXIT: x does not exists.
end procedure
Java Implementation01

// Java implementation of iterative Binary Search


class BinarySearch {
// Returns index of x if it is present in arr[],
// else return -1
// Driver method to test above
int binarySearch(int arr[], int x) public static void main(String args[])
{ {
int l = 0, r = arr.length - 1; BinarySearch ob = new BinarySearch();
while (l <= r) {
int m = l + (r - l) / 2; int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
// Check if x is present at mid int x = 10;
if (arr[m] == x)
return m; int result = ob.binarySearch(arr, x);
if (result == -1)
// If x greater, ignore left half System.out.println("Element not
if (arr[m] < x)
l = m + 1;
present");
else
// If x is smaller, ignore right half System.out.println("Element found at "
else
+ "index " + result);
r = m - 1;
} }
}
// if we reach here, then element was
// not present
return -1;
Java Implementation02

// Java program to demonstrate working of Arrays.


// binarySearch() in a sorted array.
import java.util.Arrays;

public class GFG


{
public static void main(String[] args)
{ {‘c',‘g',‘i',‘p',‘q'}
char charArr[] = {'g','p','q','c','i'};
int intArr[] = {10,20,15,22,35};

Arrays.sort(charArr);
Arrays.sort(intArr); {10,15,20,22,35};
char charKey = 'g';
int intKey = 22;
System.out.println(charKey + " found at index = "
+Arrays.binarySearch(charArr,charKey));
g found at index = 1
System.out.println(intKey + " found at index = "
+Arrays.binarySearch(intArr,intKey));
} 22 found at index = 3
}

You might also like