Searching is the process of determining whether a given element exists in an array and, if required, returning its index.

The choice of searching technique depends entirely on whether the array is sorted or unsorted. Selecting the correct approach directly impacts performance and scalability.


1. Searching in an Unsorted Array

Characteristics

  • Elements are not arranged in any specific order
  • No assumptions can be made about element positions
  • Linear search is the primary technique used

Concept

Each element of the array is checked one by one until the target element is found or the array ends.

Python Example (Text Only)

The algorithm starts from the first element and compares each element with the target value.
If a match is found, the index is returned.
If the loop finishes without a match, -1 is returned.

Example logic:
Traverse the array from index 0 to the last index.
Compare each element with the target value.
Stop when the element is found.


Time Complexity

  • Best case: O(1)
  • Average case: O(n)
  • Worst case: O(n)

  • The array is unsorted
  • The array size is small
  • Only a few searches are required
  • Simplicity is more important than performance

2. Searching in a Sorted Array

Characteristics

  • Elements are arranged in ascending or descending order
  • Efficient searching techniques can be applied
  • Binary search is commonly used

Concept

Binary search repeatedly divides the search space into halves by comparing the target value with the middle element.

If the target is smaller than the middle element, the search continues in the left half.
If the target is larger, the search continues in the right half.


Python Example (Text Only)

The algorithm maintains two pointers: low and high.
The middle element is calculated.
The target is compared with the middle element.
Half of the array is discarded in each step.

This process continues until the element is found or the search space becomes empty.


Time Complexity

  • Best case: O(1)
  • Average case: O(log n)
  • Worst case: O(log n)

  • The array is already sorted
  • The dataset is large
  • Multiple searches are required
  • Faster performance is needed

3. Comparison: Sorted vs Unsorted Searching

Aspect: Searching Method
Unsorted Array: Linear search
Sorted Array: Binary search

Aspect: Time Complexity
Unsorted Array: O(n)
Sorted Array: O(log n)

Aspect: Precondition
Unsorted Array: No requirement
Sorted Array: Array must be sorted

Aspect: Speed
Unsorted Array: Slower
Sorted Array: Faster

Aspect: Implementation
Unsorted Array: Simple
Sorted Array: Moderate


4. Important Interview Insight

Sorting an unsorted array takes O(n log n) time.

For a single search operation, sorting first and then applying binary search is inefficient.

However, if multiple searches are required, sorting the array once and then using binary search becomes beneficial.

This tradeoff is commonly tested in interviews.


5. Real-World Examples

Unsorted Array Searching

  • User-entered data
  • Log files
  • Sensor readings

Sorted Array Searching

  • Contact lists
  • Dictionaries
  • Database indexes
  • Ranking systems

6. Common Mistakes to Avoid

  • Applying binary search on an unsorted array
  • Ignoring edge cases such as empty arrays
  • Not analyzing time complexity
  • Using inefficient searching methods for large datasets

Conclusion

Linear search is suitable for unsorted arrays and small datasets.
Binary search is ideal for sorted arrays and large datasets.

Choosing the correct searching technique is fundamental to writing efficient and scalable algorithms.