0% found this document useful (0 votes)
32 views

Searching Algorithms: Compiled by JN Masi

The document summarizes several common searching algorithms. It describes linear search as sequentially checking each element of an unsorted list to find a target value. Binary search is described as locating the middle element of a sorted list, eliminating half the remaining search space based on whether the target is higher or lower. The key advantages and disadvantages of linear and binary search are provided. Linear search is simple but inefficient for large lists, while binary search requires a sorted list but has logarithmic time complexity.

Uploaded by

Clement Phiri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Searching Algorithms: Compiled by JN Masi

The document summarizes several common searching algorithms. It describes linear search as sequentially checking each element of an unsorted list to find a target value. Binary search is described as locating the middle element of a sorted list, eliminating half the remaining search space based on whether the target is higher or lower. The key advantages and disadvantages of linear and binary search are provided. Linear search is simple but inefficient for large lists, while binary search requires a sorted list but has logarithmic time complexity.

Uploaded by

Clement Phiri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

SEARCHING

ALGORITHMS
COMPILED BY JN MASI
Searching

• The process of locating target data is known as


searching

• A search algorithm is a method of locating a specific


item in a collection of data.
Examples of searching algorithms

• Sequential or linear search


• Binary search
• Hashing
• Jump search
• Sublist search
• Fibonacci search
Linear Search
• It uses a loop to sequentially step through an array starting
with the first element.
• It compares each element with the value being searched for
and stops when either the value is found or the end of the
array is encountered.
• If the value being searched for is not in the array, the
algorithm will search to the end of the array.
• Sequential search is used when the list is not sorted
Linear search pseudocode
Step 1: Set flag to indicate “Element not found”

Step 2: Take the first element on the list

Step 3: If the element in the list is equal to the desired element


- Set flag to “element found”
- Display the message “element found in the list”
- Go to step 6

Step 4: If it is not end of the list


- Take the next element in the list
- Go to step 3

Step 5: If end of the list


- Display “element not found ”
- Go to step 6

Step 6:End of algorithm


Linear search code

//Where 0 means not found and 1 means found.


for(int a=0;a<ArraySize;a++)
{
if(Target==x[a]){
return 1;
break; }
} return 0;
Linear Search advantages

• It is a simple algorithm
• Easy to understand and implement
• Does not need a sorted array
Linear search disadvantages

• This method is insufficient when large number of


elements is present in list.
• It consumes more time and reduces the retrieval rate of
the system.

Note: When it can be avoided the linear search should


not be used on large arrays if speed is important.
Linear search best,worst and average cases

• In the average and worst cases


• T(n) = n
• Asymptotic notation Θ(n)
Binary Search
• A Binary search algorithm only requires that the values in the
array be in order/sorted.
• Instead of testing the array’s first element, this algorithm starts
with the element in the middle.
• If that element happens to contain the desired value, then the
search is over.
• If the desired value wasn’t found in the middle element, the
procedure is repeated for the half of the array that potentially
contains the value
Binary Search
int bin_search(int arr[], int n, int val)
{
int low = 0, high = n-1; // initially pick the whole range
while (low <= high) {
int mid = (low+high)/2; // average value...
if (val == arr[mid]) return mid; // found the key
if (val < arr[mid]) high = mid-1; // pick the first half
else low = mid+1; // pick the second half
} return -1; // did not find the key
}
Binary Search
Step 1: Set lowest

Step 2: Set highest

Step 3: Set target

Step 4: Do while lowest <=highest

Set middle = (lowest + highest) / 2

If target= array(middle) then

Found=true

Exit

Else if target<array(middle) then

Highest=middle-1

Else

Low =middle + 1

End if

Loop
Binary Search advantages

• The binary search is much more efficient and quicker than the
linear search. Every time it makes a comparison and fails to
find the desired item, it eliminates half of the remaining portion
of the array that must be searched
• Efficient for large lists
• Suitable for storage structure that supports direct access to data
Binary search disadvantages

Not suitable for unsorted data


• Not suitable for storage structure that do not
support direct access to data, for example,
magnetic tape and linked list
• Inefficient for small lists
Binary search Best, average and worst cases

• Average and worst case


• T(n) = log n
• Θ(log n)
Example 100 elements in the array, the algorithm
should run T(n) = log2(100), which is 6.
• Meaning the algorithm should run approx 6 times
QUESTIONS???

You might also like