Search Algorithms Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Linear Search

A

Checks each element in the list sequentially until it finds the target value or reaches the end of the list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Time Complexity Linear Search

A

Best: O(1) - if target is in first position
AVG: O(n)
Worst: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Space Complexity Linear Search

A

Always: O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When to use Linear Search

A

List is small or unsorted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When not to use Linear Search

A

For large datasets or if speed is important

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Strengths Linear Search

A

Simple to implement and works on unsorted data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Weaknesses Linear Search

A

Inefficient for large datasets (O(n) time)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Binary Search

A

Binary Search requires a sorted list. It repeatedly divides the list in half, comparing the middle element to the target. If the target is smaller or larger than the middle, it continues searching in the respective half

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Binary Search Time Complexity

A

Best: O(1)
AVG: O(log n)
Worst: O(log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Binary Search Space Complexity

A

Iterative Version: O(1)
Recursive Version: O(log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When to use Binary Search

A

When the list is sorted and you need efficient search

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When not to use Binary Search

A

On unsorted data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Strengths of Binary Search

A

Very efficient for large, sorted datasets, O(log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Weaknesses of Binary Search

A

Only works on sorted data and requires extra time for sorting

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When is it worth sorting first for a large dataset before using search function?

A

It’s worth sorting first if the dataset is going to be repeatedly searched for values.

If you only search once, just use Linear Search. It is time complexity O(n), while sorting first and then binary is O(n log n) * O(log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly