
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Front and Back Search in Unsorted Array
Unsorted Array ? An array is a data structure consisting of a collection of elements of the same type. An unsorted array is such a structure where the order of elements is random, i.e. on insertion, the element is added to the last irrespective of the order of previous elements and searching in such an array is not helped by any search algorithm because of lack of a pattern of the positioning of elements.
Searching ? Searching in an array means finding a particular element in the array which can be either returning the position of a desired element or returning a bool statement specifying if the element is present in the array or not.
Front Search ? Front searching an array means starting the linear search traversal on the array from the 0th index i.e. the first element.
Back Search ? Back searching an array means starting the liner search traversal on the array from the (n-1)th index i.e. the last element.
Problem Statement
Given a search element x, find if x is present in the following cases ?
An array having elements of the same size, an array of integers.
An array having elements of different sizes, an array of strings.
Sample Example 1
Input: x = 4, [6, 1, 4, 10, 2]
Output: TRUE
Explanation ? In the given array, 4 is present at the 2nd index.
Sample Example 2
Input: x = "high", ["goat", "ice", "hgh"]
Output: False
Explanation ? In the given array, "high" is not present.
Solution
As discussed above, the front search starts from the first element and the back search starts from the last element. Combining these two approaches together, the time for searching for an element in an array can be reduced by two as checking in the front half and back half of the array take place at the same time.
For finding if an element occurs in an array, define first and last to the first and last element of the array. If any of the first or last elements is the desired element, return true else increment first by one and decrement last by one and continue until the element is found. If upon complete traversal when first and last are both equal, the element is not found then return false.
Pseudocode
procedure frontBack (arr[], x) first = 0 last = n - 1 while first <= last If arr[first] == x or arr[last] == x ans = true end if front = front + 1 last = last - 1 ans = false end procedure
Example: C++ Implementation
In the following program, we take the first case of an integer array. Taking a first and a back variable, check the first and last element simultaneously to find the desired element. If the element is found, return true else move on to the next elements and check again.
#include <bits/stdc++.h> using namespace std; // Function to front back search an element in the array bool frontBack(int arr[], int x){ int first = 0, last = 9; // loop execute till the element is found or traversal completes while (first <= last){ if (arr[first] == x || arr[last] == x){ return true; } first++; // Incrementing first last--; // Decrementing last } return false; } int main(){ int arr[10] = {21, 43, 10, 19, 3, 56, 91, 20, 5, 79}; int x = 55; cout << "In the array : "; for (int i = 0; i < 10; i++){ cout << arr[i] << " "; } cout << "\nElement " << x; if (frontBack(arr, x)){ cout << " is present."; } else{ cout << " is not present."; } return 0; }
Output
In the array : 21 43 10 19 3 56 91 20 5 79 Element 55 is not present.
Time Complexity ? O(n/2) as searching from both sides reduces time by half.
Space Complexity ? O(1)
Example
In the following program, we take the second case of a string array. Taking a first and a back variable, check the first and last element simultaneously to find the desired element. If the element is found, return true else move on to the next elements and check again.
#include <bits/stdc++.h> using namespace std; // Function to front back search an element in the array bool frontBack(string arr[], string x){ int first = 0, last = 9; // loop execute till the element is found or traversal completes while (first <= last) { if (arr[first] == x || arr[last] == x) { return true; } first++; // Incrementing first last--; // Decrementing last } return false; } int main(){ string arr[4] = {"hi", "high", "goat", "goa"}; string x = "goat"; cout << "In the array : "; for (int i = 0; i < 4; i++) { cout << arr[i] << ", "; } cout << "\nElement " << x; if (frontBack(arr, x)) { cout << " is present."; } else { cout << " is not present."; } return 0; }
Output
In the array : hi, high, goat, goa, Element goat is present.
Time Complexity ? O(n/2) as searching from both sides reduces time by half.
Space Complexity ? O(1)
Conclusion
In conclusion, the front and back search of an array is similar to a usual liner search except for the fact that it reduces the time consumption by half as it checks two elements simultaneously. Thus converting the worst-case time complexity of search in an unsorted array from O(n) to O(n/2).