0% completed
Overview
Linear search, in its basic form, identifies the position of a target element in a given array or list. When extended to find all occurrences, it retrieves every index where the target element appears. Given large arrays, this can be time-consuming. Therefore, utilizing the capabilities of modern multicore processors with multithreading can significantly expedite the search by examining multiple parts of the list concurrently.
Multithreading in Linear Search for All Occurrences
Using multithreading for searching all occurrences of an element introduces unique challenges. As multiple threads can find multiple occurrences simultaneously, we need a way to collect these indices efficiently.
- Divide the Work: As before, split the array into smaller segments or "chunks." Each thread will have its distinct portion of the array to search through.
- Concurrent Execution: Each thread will independently search its assigned chunk for the target element. Due to simultaneous searches, the overall search time often reduces for large arrays.
- Collecting Results: Rather than a single shared variable, we'll use a shared collection (like a list) where threads can append indices of found occurrences. Synchronization mechanisms are essential to ensure thread-safe updates to this shared collection.
Step-by-step Algorithm
- Make a list of numbers (let's call it arr) and decide on the number you want to find (let's call it key).
- Make a shared list (let's call it found_places) where threads will add the places where the number shows up.
- Start several threads, each one assigned to look through a specific part of the list.
- When a thread finds the number, it adds the place to the found_places list.
- Wait for all the threads to finish.
- Check the found_places list:
- If it’s empty, the number was not found.
- If it has places in it, the number was found, and the list shows where.
Algorithm Walkthrough
Let’s imagine we have a list [4, 15, 12, 3, 12, 9, 12]
, and we want to find all the places of the number 12
.
- We decide to use 3 threads to speed things up. We also have a shared list (
found_places
) to store where we find12
. At first, it's empty. - We create a function
FindAllPlaces
for the threads to use:- a. The function knows which part of the list to check based on the thread's number.
- b. It looks through its assigned numbers. If it finds
12
, it adds the place tofound_places
.
- We start our 3 threads. The first thread checks
[4, 15]
, the second checks[12, 3]
, and the third checks[12, 9, 12]
. - The threads work at the same time. The second and third threads find
12
. - They add the places of
12
(2, 4, and 6) tofound_places
. They use locks to make sure they don’t add to the list at the same time. - All threads are done. We check
found_places
and see2, 4, 6
. We print “Number found at places: 2, 4, 6”. - We’re done, and we found all places of
12
quicker by looking in three parts simultaneously.
Below are various implementations:
1 of 6
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible