100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4,6 TrustPilot
logo-home
Other

Array Search Algorithms: Techniques and Examples

Rating
-
Sold
-
Pages
6
Uploaded on
28-01-2025
Written in
2024/2025

This document covers array search algorithms, including linear search, binary search, and techniques to optimize search operations with examples.

Institution
Course

Content preview

Array Search Algorithms
Array search algorithms are essential for finding specific elements in an array.
These algorithms can be categorized based on their approach and efficiency. Let's
explore some of the most common search algorithms:



1. Linear Search
The simplest search algorithm that sequentially checks each element of the array
until the target is found.

 Time Complexity: O(n)O(n)O(n)
 Space Complexity: O(1)O(1)O(1)
 Algorithm:
1. Start from the first element.
2. Compare each element with the target.
3. Return the index if the element matches, or return -1 if not found.
 Python Implementation:

def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1

 JavaScript Implementation:

function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return i;
}
return -1;
}

, 2. Binary Search
A much faster search algorithm for sorted arrays, which repeatedly divides the
search range in half.

 Time Complexity: O(log⁡n)O(\log n)O(logn)
 Space Complexity: O(1)O(1)O(1) (iterative) or O(log⁡n)O(\log n)O(logn)
(recursive)
 Algorithm:
1. Initialize two pointers, low and high, to the start and end of the
array.
2. Compute the middle index and compare the middle element with the
target.
3. Narrow the search range based on whether the target is smaller or
larger.
4. Repeat until the range is empty or the target is found.
 Python Implementation:

def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1

 JavaScript Implementation:

function binarySearch(arr, target) {
let low = 0, high = arr.length - 1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) low = mid + 1;

Written for

Institution
Course

Document information

Uploaded on
January 28, 2025
Number of pages
6
Written in
2024/2025
Type
Other
Person
Unknown

Subjects

R118,32
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
rileyclover179

Document also available in package deal

Get to know the seller

Seller avatar
rileyclover179 US
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
252
Last sold
-

0,0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions