The Wayback Machine - https://web.archive.org/web/20240812000243/https://www.geeksforgeeks.org/searching-in-array/
Open In App

Searching in Array

Last Updated : 22 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

There are several searching algorithms. The most commonly used among them are:

  • Linear Search
  • Binary Search
  • Ternary 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.

Ternary Search:

Ternary search is a divide and conquer algorithm that can be used to find an element in an array. It is similar to binary search where we divide the array into two parts but in this algorithm, we divide the given array into three parts and determine which has the key (searched element). This algorithm also has the constraint that the array must be sorted. The time complexity for this algorithm is O(log 3 N) where ‘N’ is the size of the array.

Easy Problems on Array Searching:

Medium Problems on Array Searching:

Hard Problems on Array Searching:

Quick Links:



Previous Article
Next Article

Similar Reads

Searching Elements in an Array | Array Operations
In this post, we will look into search operation in an Array, i.e., how to search an element in an Array, such as: Searching in an Unsorted Array using Linear SearchSearching in a Sorted Array using Linear SearchSearching in a Sorted Array using Binary SearchSearching in an Sorted Array using Fibonacci SearchSearching operations in an Unsorted Arra
15+ min read
Array range queries for searching an element
Given an array of N elements and Q queries of the form L R X. For each query, you have to output if the element X exists in the array between the indices L and R(included). Prerequisite : Mo's Algorithms Examples : Input : N = 5 arr = [1, 1, 5, 4, 5] Q = 3 1 3 2 2 5 1 3 5 5 Output : No Yes Yes Explanation : For the first query, 2 does not exist bet
15+ min read
Searching in an array where adjacent differ by at most k
A step array is an array of integers where each element has a difference of at most k with its neighbor. Given a key x, we need to find the index value of x if multiple-element exist to return the first occurrence of the key.Examples: Input : arr[] = {4, 5, 6, 7, 6} k = 1 x = 6 Output : 2 The first index of 6 is 2. Input : arr[] = {20, 40, 50, 70,
7 min read
Algorithms | Searching | Question 1
Linear search is also called------ (A) Random Search (B) Sequential search (C) Perfect search (D) None Answer: (B) Explanation: 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 is found, otherwise, the search continues till the end of the data set. Hen
1 min read
Algorithms | Searching | Question 2
Which of the following is correct recurrence for worst case of Binary Search? (A) T(n) = 2T(n/2) + O(1) and T(1) = T(0) = O(1) (B) T(n) = T(n-1) + O(1) and T(1) = T(0) = O(1) (C) T(n) = T(n/2) + O(1) and T(1) = T(0) = O(1) (D) T(n) = T(n-2) + O(1) and T(1) = T(0) = O(1) Answer: (C) Explanation: Following is a typical implementation of Binary Search
2 min read
Algorithms | Searching | Question 3
Given a sorted array of integers, what can be the minimum worst-case time complexity to find ceiling of a number x in given array? The ceiling of an element x is the smallest element present in array which is greater than or equal to x. Ceiling is not present if x is greater than the maximum element present in array. For example, if the given array
2 min read
Algorithms | Searching | Question 4
Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous. (GATE CS 2008) C/C++ Code 1. f(int Y[10], int x) { 2. int i, j, k; 3. i = 0; j = 9; 4. do { 5. k = (i + j) /2; 6. if( Y[k] On which of the following contents of Y and x does the program fail? (A) Y is [1 2 3 4 5 6 7 8
2 min read
Algorithms | Searching | Question 6
C/C++ Code , int x) { 2. int i, j, k; 3. i = 0; j = 9; 4. do { 5. k = (i + j) /2; 6. if( Y[k] In the above question, the correction needed in the program to make it work properly is (GATE CS 2008) (A) Change line 6 to: if (Y[k] < x) i = k + 1; else j = k-1; (B) Change line 6 to: if (Y[k] < x) i = k - 1; else j = k+1; (C) Change line 6 to: if
2 min read
Algorithms | Searching | Question 6
Which of the following is the correct recurrence for the worst case of Ternary Search? (A) T(n) = T(n/3) + 4, T(1) = 1 (B) T(n) = T(n/2) + 2, T(1) = 1 (C) T(n) = T(n + 2) + 2, T(1) = 1 (D) T(n) = T(n - 2) + 2, T(1) = 1 Answer: (A) Explanation: Following is a typical implementation of Binary Search. // A recursive ternary search function. It returns
2 min read
Pattern Searching using C++ library
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function that prints all occurrences of pat[] in txt[]. You may assume that n > m.Examples: Input : txt[] = "geeks for geeks" pat[] = "geeks" Output : Pattern found at index 0 Pattern found at index 10 Input : txt[] = "aaaa" pat[] = "aa" Output : Pattern found at index 0 Pattern found a
3 min read
Practice Tags :
three90RightbarBannerImg