Linear Search Flashcards

1
Q

Gives two reasons why searching important in computing?

A
  1. It is a fundamental operation in computing that involves finding a specific item or value from a collection of data
  2. It plays a crucial role in various applications and is essential for efficient data retrieval and information processing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is searching?

A

Data come in large amounts, typically unsorted or random

We often need to locate an item (strings, numbers, lists…) in a dataset

Searching is the process of locating a particular element or record within a given dataset

It involves examining the data systematically to determine if the desired item exists and, if so, its location or other relevant information

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

What are the two types of search algorithms?

A
  1. Linear Search: Sequentially checks each element until a match is found or the end of the collection is reached
  2. Binary Search: Efficiently searches sorted collections by repeatedly dividing the search space in half
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is linear (sequential) search?

A

It is a simple searching algorithm that sequentially checks each element in a list or array until a match is found or the end of the list is reached

It starts at the beginning of the list and checks each element in order until the target value is found OR the end of the list is reached

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

Why is linear search also called a “sequential search”?

A

Because it examines elements in a linear order, one by one

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

What is the algorithm for it? (5 steps)

A
  1. Start at the first element of the list
  2. Compare the current element with the target element
  3. If a match is found, return the index of the element
  4. If the end of the list is reached without finding a match, return a special value (e.g., -1) to indicate the element was not found
  5. If a match is not found, move to the next element and repeat steps 2-5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does this mean?

Linear_search(arr, target)

A

This takes an array (list) and a target element as parameters

It iterates through each element of the array using a for loop and compares each element with the target

If a match is found,
= the function returns the index of the element

If the loop **completes without finding a match*
= the function returns -1 to indicate that the element was not found

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

Given the list:

[0, 11, 4, 7, 5, 1, 8, 3, 6]

How many loop iterations is does the code need to complete to find?

Value 0?
Value 11?
Value 4?
Value 2?

NOTE: you do this the same way but backwards for reverse order searching

A

Value 0 = 1

Value 11 = 2

Value 4 = 3

Value 2 = 9

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

What would be the benefit of searching backwards in the list (starting from the last element and moving towards the first element)

A
  1. Reverse order requirement:
    - When the desired order of search results is from
    the last occurrence to the first occurrence

    Ex. identifying the most recent entries in a log.
  2. Optimized search:
    - If you have prior knowledge or an expectation that the target value is more likely to be found towards the end of the list
    = searching backwards can potentially lead to faster results

Ex. With “for” loop:

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

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

What happens when multiple occurrences occur?

A

You can use this to identify all the indexes where
a specific item occurs in a list

Return a list of indexes where multiple occurrences of an item are found

This algorithm provides a useful tool for tasks such as counting occurrences, filtering data, or identifying patterns

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

What is the algorithm involved in multiple occurrences? (4 steps)

A
  1. Initialize an empty list called “occurrences” to store the indexes of the found occurrences
  2. Iterate through each element in the list
  3. If the current element matches the target item:
    = Append the index of the current element to the “occurrences” list.
  4. After iterating through the entire list, return the “occurrences” list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the algorithm involved in multiple occurrences? (4 steps)

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

The given list is [2, 4, 7, 4, 2, 9, 4, 1, 8, 3, 4].

We are searching for occurrences of the target item 4

What would be the result?

NOTE: this would also look the same for if you found multiple occurrences backwards
= it would just be in reverse order (from last element to the first element)

A

The algorithm iterates through the list and finds four occurrences of 4
= at indexes 1, 3, 6, and 10

The algorithm returns the list [1, 3, 6, 10] as the indexes where the target item 4 occurred

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

Linear search may not be efficient for “______” or “_______” lists

A

Large; sorted