0% found this document useful (0 votes)
1 views9 pages

Searching in Array

The document discusses two primary searching algorithms for arrays: Linear Search and Binary Search. Linear Search is a sequential method with a time complexity of O(N), suitable for unsorted arrays and small datasets, while Binary Search is an efficient method for sorted arrays with a time complexity of O(log N). It also includes implementation details and examples for both algorithms, along with their respective advantages and disadvantages.

Uploaded by

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

Searching in Array

The document discusses two primary searching algorithms for arrays: Linear Search and Binary Search. Linear Search is a sequential method with a time complexity of O(N), suitable for unsorted arrays and small datasets, while Binary Search is an efficient method for sorted arrays with a time complexity of O(log N). It also includes implementation details and examples for both algorithms, along with their respective advantages and disadvantages.

Uploaded by

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

Searching in Array

Searching is one of the most common operations performed in an array. Array


searching can be defined as the operation of finding a particular element or a group
of elements in the array.

 Linear Search
 Binary Search

Linear Search:
Linear Search is defined as a sequential search algorithm that starts at one end and goes
through each element of a list until the desired element or group of elements is found.
Otherwise, the search continues till the end of the data set. This has a time complexity
of O(N) where 'N' is the length of the array.

Binary Search:
Binary Search is a searching algorithm used in a sorted array. In this algorithm, the
element is found by repeatedly dividing the search interval in half and deciding the next
interval to find the element. This searching algorithm has a time complexity
of O(log 2 N) where 'N' is the length of the array. The only thing to note is that the
array must be sorted in increasing or decreasing order.

Linear Search

Linear_Search(a, n, val) // 'a' is the given array, 'n' is the size of given array,
'val' is the value to search
Step 1: set pos = -1
Step 2: set i = 1
Step 3: repeat step 4 while i <= n
Step 4: if a[i] == val
set pos = i
print pos
go to step 6
[end of if]
set ii = i + 1
[end of loop]
Step 5: if pos = -1
print "value is not present in the array "
[end of if]
Step 6: exit
#include <stdio.h>

int search(int arr[], int N, int x)

for (int i = 0; i < N; i++)

if (arr[i] == x)

return i;

return -1;

int main(void)

int arr[] = { 2, 3, 4, 10, 40 };

int x = 10;

int N = sizeof(arr) / sizeof(arr[0]);

// Function call

int result = search(arr, N, x);

(result == -1)

? printf("Element is not present in array")

: printf("Element is present at index %d", result);

return 0;

Output
Present at Index 3

Time and Space Complexity of Linear Search Algorithm:


Time Complexity:

 Best Case: In the best case, the key might be present at


the first index. So the best case complexity is O(1)
 Worst Case: In the worst case, the key might be present at the last index i.e.,
opposite to the end from which the search has started in the list. So the worst-case
complexity is O(N) where N is the size of the list.
 Average Case: O(N)

Auxiliary Space: O(1) as except for the variable to iterate through the list, no other
variable is used.

Applications of Linear Search Algorithm:

 Unsorted Lists: When we have an unsorted array or list, linear search is most
commonly used to find any element in the collection.
 Small Data Sets: Linear Search is preferred over binary search when we have small
data sets with
 Searching Linked Lists: In linked list implementations, linear search is commonly
used to find elements within the list. Each node is checked sequentially until the
desired element is found.
 Simple Implementation: Linear Search is much easier to understand and
implement as compared to Binary Search or Ternary Search.

Advantages of Linear Search Algorithm:


 Linear search can be used irrespective of whether the
array is sorted or not. It can be used on arrays of any data
type.
 Does not require any additional memory.
 It is a well-suited algorithm for small datasets.
Disadvantages of Linear
Search Algorithm:
 Linear search has a time complexity of O(N), which in turn
makes it slow for large datasets.
 Not suitable for large arrays.
When to use Linear Search Algorithm?
 When we are dealing with a small dataset.
 When you are searching for a dataset stored in contiguous
memory.

Binary Search Algorithm – Iterative and


Recursive Implementation
Binary Search Algorithm is 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).
Binary search is a search algorithm used to find the position
of a target value within a sorted array. It works by
repeatedly dividing the search interval in half until the target
value is found or the interval is empty. The search interval is
halved by comparing the target element with the middle
value of the search space.
Conditions to apply Binary Search
Algorithm in a Data Structure
To apply Binary Search algorithm:
 The data structure must be sorted.
 Access to any element of the data structure should take
constant time.
Binary Search Algorithm
Below is the step-by-step algorithm for Binary Search:
 Divide the search space into two halves by finding the
middle index “mid”.
 Compare the middle element of the search space with
the key.
 If the key is found at middle element, the process is
terminated.
 If the key is not found at middle element, choose which
half will be used as the next search space.
o If the key is smaller than the middle element, then
the left side is used for next search.
o If the key is larger than the middle element, then
the right side is used for next search.
 This process is continued until the key is found or the total
search space is exhausted.
Binary Search Visualizer
How does Binary Search Algorithm work?
To understand the working of binary search, consider the
following illustration:
Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72,
91}, and the target = 23.

1/4
How to Implement Binary Search
Algorithm?
The Binary Search Algorithm can be implemented in the
following two ways
 Iterative Binary Search Algorithm
 Recursive Binary Search Algorithm
Given below are the pseudocodes for the approaches.
Iterative Binary Search Algorithm:
Here we use a while loop to continue the process of
comparing the key and splitting the search space in two
halves.

Try it on GfG Practice

// C program to implement iterative Binary Search


2

#include <stdio.h>
3

// An iterative binary search function.


5

int binarySearch(int arr[], int low, int high, int x)


6

{
7

while (low <= high) {


8

int mid = low + (high - low) / 2;


9

10
// Check if x is present at mid
11

if (arr[mid] == x)
12

return mid;
13

14

// If x greater, ignore left half


15

if (arr[mid] < x)
16

low = mid + 1;
17

18

// If x is smaller, ignore right half


19

else
20

high = mid - 1;
21

}
22

23

// If we reach here, then element was not present


24

return -1;
25

}
26

27

// Driver code
28

int main(void)
29
{
30

int arr[] = { 2, 3, 4, 10, 40 };


31

int n = sizeof(arr) / sizeof(arr[0]);


32

int x = 10;
33

int result = binarySearch(arr, 0, n - 1, x);


34

if(result == -1) printf("Element is not present in array");


35

else printf("Element is present at index %d",result);


36

37

Output
Element is present at index 3
Time Complexity: O(log N)
Auxiliary Space: O(1)

Largest element in an Array


Last Updated : 27 Dec, 2024



Given an array arr. The task is to find the largest element in


the given array.
Examples:
Input: arr[] = [10, 20, 4]
Output: 20
Explanation: Among 10, 20 and 4, 20 is the largest.
Input: arr[] = [20, 10, 20, 4, 100]
Output: 100
Iterative Approach – O(n) Time and O(1) Space
The approach to solve this problem is to traverse the whole
array and find the maximum among them.
1

#include <stdio.h>
int largest(int arr[], int n) {
4

int i;
5

int max = arr[0];


6

// Traverse array elements from second and


8

// compare every element with current max


9

for (i = 1; i < n; i++)


10

if (arr[i] > max)


11

max = arr[i];
12

13

return max;
14

}
15

16

int main() {
17

int arr[] = { 10, 324, 45, 90, 9808 };


18

int n = sizeof(arr) / sizeof(arr[0]);


19

20
printf("%d", largest(arr, n));
21

return 0;
22

Output
9808

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy