Search in an almost sorted array
Last Updated :
07 Mar, 2023
Given a sorted array arr[] of size N, some elements of array are moved to either of the adjacent positions, i.e., arr[i] may be present at arr[i+1] or arr[i-1] i.e. arr[i] can only be swapped with either arr[i+1] or arr[i-1]. The task is to search for an element in this array.
Examples :
Input: arr[] = {10, 3, 40, 20, 50, 80, 70}, key = 40
Output: 2
Explanation: Output is index of 40 in given array i.e. 2
Input: arr[] = {10, 3, 40, 20, 50, 80, 70}, key = 90
Output: -1
Explanation: -1 is returned to indicate the element is not present
Naive Approach:
A simple solution is to linearly search the given key in array arr[].
Below is implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int linearSearch( int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
int main( void )
{
int arr[] = { 3, 2, 10, 4, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 4;
int result = linearSearch(arr, n, x);
(result == -1)
? printf ( "Element is not present in array" )
: printf ( "Element is present at index %d" , result);
return 0;
}
|
Java
class Geeksforgeeks{
public static int linearSearch( int arr[], int x)
{
int n = arr.length;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == x)
return i;
}
return - 1 ;
}
public static void main(String args[])
{
int arr[] = { 3 , 2 , 10 , 4 , 40 };
int x = 4 ;
int result = linearSearch(arr, x);
if (result == - 1 )
System.out.print(
"Element is not present in array" );
else
System.out.print( "Element is present at index "
+ result);
}
}
|
Python3
class Geeksforgeeks:
@staticmethod
def linearSearch(arr, x):
n = len (arr)
for i in range (n):
if arr[i] = = x:
return i
return - 1
if __name__ = = '__main__' :
arr = [ 3 , 2 , 10 , 4 , 40 ]
x = 4
result = Geeksforgeeks.linearSearch(arr, x)
if result = = - 1 :
print ( "Element is not present in array" )
else :
print ( "Element is present at index" , result)
|
C#
using System;
class Geeksforgeeks {
public static int LinearSearch( int [] arr, int x)
{
int n = arr.Length;
for ( int i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
static void Main( string [] args)
{
int [] arr = { 3, 2, 10, 4, 40 };
int x = 4;
int result = LinearSearch(arr, x);
if (result == -1)
Console.WriteLine(
"Element is not present in array" );
else
Console.WriteLine( "Element is present at index "
+ result);
}
}
|
Javascript
function linearSearch(arr, x) {
let n = arr.length;
for (let i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
let arr = [3, 2, 10, 4, 40];
let x = 4;
let result = linearSearch(arr, x);
if (result == -1)
console.log( "Element is not present in array" );
else
console.log( "Element is present at index " + result);
|
Output
Element is present at index 3
Time complexity: O(N).
Auxiliary Space: O(1)
We can modify binary search to do it in O(Logn) time.
Search in an almost sorted array using Binary search:
The idea is to compare the key with middle 3 elements, if present then return the index. If not present, then compare the key with middle element to decide whether to go in left half or right half. Comparing with middle element is enough as all the elements after mid+2 must be greater than element mid and all elements before mid-2 must be smaller than mid element.
Follow the steps below to implement the idea:
- Construct a recursive function to search for x that takes array arr[], left pointer l and right pointer r as input and returns the index of x in array.
- Initialize a variable mid with l+(r-l)/2.
- If arr[mid] is equal to x return mid
- Else if arr[mid-1] is equal to x return mid-1
- Else if arr[mid+1] is equal to x return mid+1
- If arr[mid] > x recur for search space l to mid-2 else recur for search space mid+2 to r.
Below is the implementation of this approach.
C++
#include <bits/stdc++.h>
using namespace std;
int binarySearch( int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (mid > l && arr[mid - 1] == x)
return (mid - 1);
if (mid < r && arr[mid + 1] == x)
return (mid + 1);
if (arr[mid] > x)
return binarySearch(arr, l, mid - 2, x);
return binarySearch(arr, mid + 2, r, x);
}
return -1;
}
int main( void )
{
int arr[] = { 3, 2, 10, 4, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 4;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? printf ( "Element is not present in array" )
: printf ( "Element is present at index %d" , result);
return 0;
}
|
Java
import java.io.*;
class GFG {
int binarySearch( int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2 ;
if (arr[mid] == x)
return mid;
if (mid > l && arr[mid - 1 ] == x)
return (mid - 1 );
if (mid < r && arr[mid + 1 ] == x)
return (mid + 1 );
if (arr[mid] > x)
return binarySearch(arr, l, mid - 2 , x);
return binarySearch(arr, mid + 2 , r, x);
}
return - 1 ;
}
public static void main(String args[])
{
GFG ob = new GFG();
int arr[] = { 3 , 2 , 10 , 4 , 40 };
int n = arr.length;
int x = 4 ;
int result = ob.binarySearch(arr, 0 , n - 1 , x);
if (result == - 1 )
System.out.println(
"Element is not present in array" );
else
System.out.println(
"Element is present at index " + result);
}
}
|
Python3
def binarySearch(arr, l, r, x):
if (r > = l):
mid = int (l + (r - l) / 2 )
if (arr[mid] = = x):
return mid
if (mid > l and arr[mid - 1 ] = = x):
return (mid - 1 )
if (mid < r and arr[mid + 1 ] = = x):
return (mid + 1 )
if (arr[mid] > x):
return binarySearch(arr, l, mid - 2 , x)
return binarySearch(arr, mid + 2 , r, x)
return - 1
arr = [ 3 , 2 , 10 , 4 , 40 ]
n = len (arr)
x = 4
result = binarySearch(arr, 0 , n - 1 , x)
if (result = = - 1 ):
print ( "Element is not present in array" )
else :
print ( "Element is present at index" , result)
|
C#
using System;
class GFG {
int binarySearch( int [] arr, int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (mid > l && arr[mid - 1] == x)
return (mid - 1);
if (mid < r && arr[mid + 1] == x)
return (mid + 1);
if (arr[mid] > x)
return binarySearch(arr, l, mid - 2, x);
return binarySearch(arr, mid + 2, r, x);
}
return -1;
}
public static void Main()
{
GFG ob = new GFG();
int [] arr = { 3, 2, 10, 4, 40 };
int n = arr.Length;
int x = 4;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
Console.Write(
"Element is not present in array" );
else
Console.Write( "Element is present at index "
+ result);
}
}
|
PHP
<?php
function binarySearch( $arr , $l , $r , $x )
{
if ( $r >= $l )
{
$mid = $l + ( $r - $l ) / 2;
if ( $arr [ $mid ] == $x )
return $mid ;
if ( $mid > $l && $arr [ $mid - 1] == $x )
return ( $mid - 1);
if ( $mid < $r && $arr [ $mid + 1] == $x )
return ( $mid + 1);
if ( $arr [ $mid ] > $x )
return binarySearch( $arr , $l ,
$mid - 2, $x );
return binarySearch( $arr , $mid + 2,
$r , $x );
}
return -1;
}
$arr = array (3, 2, 10, 4, 40);
$n = sizeof( $arr );
$x = 4;
$result = binarySearch( $arr , 0, $n - 1, $x );
if ( $result == -1)
echo ( "Element is not present in array" );
else
echo ( "Element is present at index $result" );
?>
|
Javascript
<script>
function binarySearch(arr,l,r,x)
{
if (r >= l)
{
let mid = l + Math.floor((r - l) / 2);
if (arr[mid] == x)
return mid;
if (mid > l && arr[mid - 1] == x)
return (mid - 1);
if (mid < r && arr[mid + 1] == x)
return (mid + 1);
if (arr[mid] > x)
return binarySearch(arr, l, mid - 2, x);
return binarySearch(arr, mid + 2, r, x);
}
return -1;
}
let arr=[3, 2, 10, 4, 40];
let n = arr.length;
let x = 4;
let result = binarySearch(arr, 0, n - 1, x);
if (result == -1)
document.write( "Element is not present in array<br>" );
else
document.write( "Element is present at index " +
result+ "<br>" );
</script>
|
Output
Element is present at index 3
Time complexity: O(Logn).
Auxiliary Space: O(1)
Please Login to comment...