0% found this document useful (0 votes)
9 views12 pages

Linear Search Algorithm

The document explains the Linear Search Algorithm, which is a simple method for finding an element in an unordered list by sequentially checking each element until a match is found or the list is exhausted. It details the algorithm's steps, time complexity (O(n) in the worst case), and space complexity (O(1)). Additionally, it briefly introduces the Binary Search Algorithm, which is more efficient for sorted lists, with a time complexity of O(log n).

Uploaded by

mahesh9705842810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Linear Search Algorithm

The document explains the Linear Search Algorithm, which is a simple method for finding an element in an unordered list by sequentially checking each element until a match is found or the list is exhausted. It details the algorithm's steps, time complexity (O(n) in the worst case), and space complexity (O(1)). Additionally, it briefly introduces the Binary Search Algorithm, which is more efficient for sorted lists, with a time complexity of O(log n).

Uploaded by

mahesh9705842810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Linear Search Algorithm

In this article, we will discuss the Linear Search Algorithm. Searching is the process of
finding some particular element in the list. If the element is present in the list, then the
process is called successful, and the process returns the location of that element;
otherwise, the search is called unsuccessful.

Two popular search methods are Linear Search and Binary Search. So, here we will
discuss the popular searching technique, i.e., Linear Search Algorithm.

Linear search is also called as sequential search algorithm. It is the simplest searching
algorithm. In Linear search, we simply traverse the list completely and match each
element of the list with the item whose location is to be found. If the match is found,
then the location of the item is returned; otherwise, the algorithm returns NULL.

It is widely used to search an element from the unordered list, i.e., the list in which
items are not sorted. The worst-case time complexity of linear search is O(n).

Algorithm
1. Linear_Search(a, n, val) // 'a' is the given array, 'n' is the size of given array, 'val' is the value to
search
2. Step 1: set pos = -1
3. Step 2: set i = 1
4. Step 3: repeat step 4 while i <= n
5. Step 4: if a[i] == val
6. set pos = i
7. print pos
8. go to step 6
9. [end of if]
10. set ii = i + 1
11. [end of loop]
12. Step 5: if pos = -1
13. print "value is not present in the array "
14. [end of if]
15. Step 6: exit

Working of Linear search


Now, let's see the working of the linear search Algorithm.
To understand the working of linear search algorithm, let's take an unsorted array. It
will be easy to understand the working of linear search with an example.

Let the elements of array are -

Let the element to be searched is K = 41

Now, start from the first element and compare K with each element of the array.

The value of K, i.e., 41, is not matched with the first element of the array. So, move to
the next element. And follow the same process until the respective element is found.
Now, the element to be searched is found. So algorithm will return the index of the
element matched.

Linear Search complexity


Now, let's see the time complexity of linear search in the best case, average case, and
worst case. We will also see the space complexity of linear search.

1. Time Complexity
Case Time Complexity

Best Case O(1)

Average Case O(n)


Worst Case O(n)

o Best Case Complexity - In Linear search, best case occurs when the element we are
finding is at the first position of the array. The best-case time complexity of linear
search is O(1).
o Average Case Complexity - The average case time complexity of linear search is O(n).
o Worst Case Complexity - In Linear search, the worst case occurs when the element
we are looking is present at the end of the array. The worst-case in linear search could
be when the target element is not present in the given array, and we have to traverse
the entire array. The worst-case time complexity of linear search is O(n).

The time complexity of linear search is O(n) because every element in the array is
compared only once.

2. Space Complexity
Space Complexity O(1)

o The space complexity of linear search is O(1).


o // Linear Search in C
o
o #include <stdio.h>
o
o int search(int array[], int n, int x) {
o
o // Going through array sequencially
o for (int i = 0; i < n; i++)
o if (array[i] == x)
o return i;
o return -1;
o }
o
o int main() {
o int array[] = {2, 4, 0, 1, 9};
o int x = 1;
o int n = sizeof(array) / sizeof(array[0]);
o
o int result = search(array, n, x);
o
o (result == -1) ? printf("Element not found") : printf("Element found
at index: %d", result);
o }
o #include <stdio.h>
o int linearSearch(int a[], int n, int val) {
o // Going through array sequencially
o for (int i = 0; i < n; i++)
o {
o if (a[i] == val)
o return i+1;
o }
o return -1;
o }
o int main() {
o int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52}; // given array
o int val = 41; // value to be searched
o int n = sizeof(a) / sizeof(a[0]); // size of array
o int res = linearSearch(a, n, val); // Store result
o printf("The elements of the array are - ");
o for (int i = 0; i < n; i++)
o printf("%d ", a[i]);
o printf("\nElement to be searched is - %d", val);
o if (res == -1)
o printf("\nElement is not present in the array");
o else
o printf("\nElement is present at %d position of array", res);
o return 0;
o }

Binary Search Algorithm


In this article, we will discuss the Binary Search Algorithm. Searching is the process of
finding some particular element in the list. If the element is present in the list, then the
process is called successful, and the process returns the location of that element. Otherwise,
the search is called unsuccessful.

Linear Search and Binary Search are the two popular searching techniques. Here we will
discuss the Binary Search Algorithm.

Binary search is the search technique that works efficiently on sorted lists. Hence, to search
an element into some list using the binary search technique, we must ensure that the list is
sorted.
Binary search follows the divide and conquer approach in which the list is divided into two
halves, and the item is compared with the middle element of the list. If the match is found
then, the location of the middle element is returned. Otherwise, we search into either of the
halves depending upon the result produced through the match.

NOTE: Binary search can be implemented on sorted array elements. If the list
elements are not arranged in a sorted manner, we have first to sort them.
1. Binary_Search(a, lower_bound, upper_bound, val) // 'a' is the given array, 'lower_boun
d' is the index of the first array element, 'upper_bound' is the index of the last array el
ement, 'val' is the value to search
2. Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
3. Step 2: repeat steps 3 and 4 while beg <=end
4. Step 3: set mid = (beg + end)/2
5. Step 4: if a[mid] = val
6. set pos = mid
7. print pos
8. go to step 6
9. else if a[mid] > val
10. set end = mid - 1
11. else
12. set beg = mid + 1
13. [end of if]
14. [end of loop]
15. Step 5: if pos = -1
16. print "value is not present in the array"
17. [end of if]
18. Step 6: exit

Working of Binary search


Now, let's see the working of the Binary Search Algorithm.

To understand the working of the Binary search algorithm, let's take a sorted array. It
will be easy to understand the working of Binary search with an example.

There are two methods to implement the binary search algorithm -

o Iterative method
o Recursive method
The recursive method of binary search follows the divide and conquer approach.

Let the elements of array are -

Let the element to search is, K = 56

We have to use the below formula to calculate the mid of the array -

1. mid = (beg + end)/2

So, in the given array -

beg = 0

end = 8

mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.


Now, the element to search is found. So algorithm will return the index of the element
matched.

Binary Search complexity


Now, let's see the time complexity of Binary search in the best case, average case, and
worst case. We will also see the space complexity of Binary search.

1. Time Complexity
Case Time Complexity

Best Case O(1)


Average Case O(logn)

Worst Case O(logn)

o Best Case Complexity - In Binary search, best case occurs when the element to search
is found in first comparison, i.e., when the first middle element itself is the element to
be searched. The best-case time complexity of Binary search is O(1).
o Average Case Complexity - The average case time complexity of Binary search
is O(logn).
o Worst Case Complexity - In Binary search, the worst case occurs, when we have to
keep reducing the search space till it has only one element. The worst-case time
complexity of Binary search is O(logn).

2. Space Complexity
Space Complexity O(1)

o The space complexity of binary search is O(1).


o #include <stdio.h>
o int binarySearch(int a[], int beg, int end, int val)
o {
o int mid;
o if(end >= beg)
o { mid = (beg + end)/2;
o /* if the item to be searched is present at middle */
o if(a[mid] == val)
o {
o return mid+1;
o }
o /* if the item to be searched is smaller than middle, then it can only be
in left subarray */
o else if(a[mid] < val)
o {
o return binarySearch(a, mid+1, end, val);
o }
o /* if the item to be searched is greater than middle, then it can only be
in right subarray */
o else
o {
o return binarySearch(a, beg, mid-1, val);
o }
o }
o return -1;
o }
o int main() {
o int a[] = {11, 14, 25, 30, 40, 41, 52, 57, 70}; // given array
o int val = 40; // value to be searched
o int n = sizeof(a) / sizeof(a[0]); // size of array
o int res = binarySearch(a, 0, n-1, val); // Store result
o printf("The elements of the array are - ");
o for (int i = 0; i < n; i++)
o printf("%d ", a[i]);
o printf("\nElement to be searched is - %d", val);
o if (res == -1)
o printf("\nElement is not present in the array");
o else
o printf("\nElement is present at %d position of array", res);
o return 0;
o }

Iteration Method

do until the pointers low and high meet each other.

mid = (low + high)/2

if (x == arr[mid])

return mid

else if (x > arr[mid]) // x is on the right side

low = mid + 1

else // x is on the left side

high = mid - 1
Recursive Method

binarySearch(arr, x, low, high)

if low > high

return False

else

mid = (low + high) / 2

if x == arr[mid]

return mid

else if x > arr[mid] // x is on the right side

return binarySearch(arr, x, mid + 1, high)

else // x is on the left side

return binarySearch(arr, x, low, mid - 1)

/ Binary Search in C

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {


// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;

if (array[mid] == x)
return mid;

if (array[mid] < x)
low = mid + 1;

else
high = mid - 1;
}

return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}

You might also like