0% found this document useful (0 votes)
8 views8 pages

Linear Search

Uploaded by

y2vkgk8ww4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Linear Search

Uploaded by

y2vkgk8ww4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Department of Computer Science

Northern Frontier College


Tarfh Saad
Introduction:

Linear search, or sequential search, is a basic algorithm in computer


science that checks each element in a list sequentially to find a target
value. Its simplicity and ease of implementation make it a valuable
introductory concept. Although its time complexity is O(n), making it
less efficient for large datasets than algorithms like binary search, it
effectively handles small or unsorted datasets and serves as a
foundation for more complex searching techniques.
Linear Search :

❑ Definition: The Process of Linear Search in


• Linear Search is an0020algorithm that iteratively checks each element in an Computer Science:
array, starting from the first element, in a linear or sequential manner until
it finds a match with the target value. If the algorithm does not find a • Start from the first element in the array.
match, it denotes that the target value is not present in the array. • Compare the target value with the current
array element.
Mechanism of Linear Search Algorithm : • If they match, return the current index.
• A linear search algorithm works by sequentially checking each element of
• If they do not, move to the next element
the array from start to end until the desired element is found or all elements
and repeat the process.
have been checked.
• If the end of the array is reached without
• It starts at the first element of an array and moves element to element in a
linear fashion.
finding a match, the search has failed and -
1 is returned.
• If the desired element is found, the search ends and returns the index of the
element. • In terms of time complexity, a Linear
Search requires on
• If the element is not found after checking the entire array, the algorithm
average n/2 comparisons, where n is the
returns a message indicating that the element is not in the array
number of items in the array.
Linear Search :

❑ Efficiency in the Application of Linear Search


Uses of the linear search algorithm
Algorithm
• Linear Search is usually preferred when the array has a small
number of elements or when performing a single search in an • The Linear Search algorithm is a straightforward and
unsorted array. effective method for array searching in computer
science. Its efficiency is characterized by the
• For larger or sorted arrays, more efficient algorithms like Binary following points:
Search or Hashing should be considered.
• Time Complexity: The algorithm has a time
complexity is O(n), indicating that execution time
increases linearly with the number of elements in the
:Advantages of Linear Search
array.
• Implementation ease - Linear Search is straightforward to understand
• No Sorting Required: A significant advantage of
and code.
Linear Search is that it does not require the array to
• Space efficiency - Unlike other searching algorithms, Linear Search be sorted beforehand, unlike algorithms such as
does not require any extra space since it works on the existing data Binary Search.
structure.
• Multiple Occurrences: Linear Search can locate
• Unordered data - It is effective on both sorted and unsorted arrays, multiple occurrences of the target value, returning all
irrespective of the order of the elements. relevant indices instead of identifying the first
instance and ending the search operation.
Linear Search Example:

Practical Linear Search Example


Let's consider a practical example of a Linear Search algorithm. The goal is to
determine if the number '5' exists in the following array:
Array: [2, 3, 4, 5, 6] Now, we will run the linear search algorithm:
❖ Starting from First Index: The algorithm begins by checking the first element
(index 0). Linear Search algorithm
Check if '2' equals '5': They do not match, so we move to the next element.
Find ‘5’
❖ Continue Checking Elements:
Check if '3' equals '5': They do not match, so proceed to the next element.
Check if '4' equals '5': They do not match, so proceed to the next element. 2 3 4 5 6
❖ Finding the Target:
Check if '5' equals '5': They match, so the algorithm stops here.
❖ Return the Index:
In this case, the number '5' was found at the 4th index, hence index '3' (0-based
index) is returned.
Scenarios Where Linear Search Holds an Advantage:

Linear search, while simple, excels in certain scenarios:


1. Small Datasets: It is efficient for small data sizes due to its straightforward nature, eliminating the
overhead associated with more complex algorithms.

2. Unsorted Datasets: Linear search is suitable for unsorted arrays, as it does not require prior sorting,
unlike algorithms such as binary search.

3. Sequential Memory: The algorithm performs well with data stored sequentially in memory, aligning with
the structure of arrays and lists.

4. Searching for Multiple Instances: Linear search can continue to find all occurrences of a target value,
returning multiple indices as needed.
Ajoutez le formatage nécessaire en utilisant CSS:

Building a Linear Search Algorithm: Step-by-Step


Let’s say an array containing integer numbers. The task is to find a given number in the array. Flowchart for Linear Search Algorithm:
If the number is located in the array, we need to return the index of that number.•

If the given number is not found, then it will return -1.•


In the flowchart, “Data” is the integer array, “N” is the size of the array, and the “item” is the
number we want to search in the array.
Here are the steps of the flowchart:
o Step 1) Read the search item, “item.”
o Step 2) Initiate i=0 and index=-1
o Step 3) If i<N, go to step 4. Else, go to step 8.
o Step 4) If Data[i] equals to “item,” then go to step 5. Else go to step 6.
o Step 5) Index = i (As the item is found at index no i). Go to step 8.
o Step 6) i = i +1.
o Step 7) Go to step 3.
o Step 8) Stop.
Conclusion:

The Linear Search algorithm is a fundamental technique in computer science, defined by its straightforward
approach to searching through arrays. The mechanism involves sequentially examining each element, making
it particularly useful for small or unsorted datasets.

Its primary advantages include ease of implementation and the ability to locate multiple occurrences of a
target value without requiring prior sorting. While the algorithm has a time complexity of O(n), making it less
efficient for larger datasets compared to alternatives like Binary Search, it remains effective in specific
scenarios.

Practical examples demonstrate its functionality, reinforcing its value in array exploration. Overall, Linear
Search excels in contexts where simplicity and directness are crucial, highlighting its importance in the study
of search algorithms.

You might also like