What is Linear Search?
Linear Search, also known as sequential search, is a searching algorithm that finds an element in an array by checking each element sequentially until a match is found. Many beginning programmers might use this search method without thinking about it, as it can be represented by only a for loop and is very intuitive.
Assumptions
- Knowledge of <a href="https://www.devmaking.com/learn/algorithms/big-o-notation/" target="_blank" style="color:inherit">Big-O notation</a>
Linear Search Complexity
Best | Worst | Average |
---|---|---|
O(1) | O(n) | O(n) |
When to use linear search.
Linear Search may often be seen as a "last resort" to more experienced programmers when no other searching algorithm is able to be utilized. In general, consider using linear sort when:
- The list of elements is unordered.
- It is not worth sorting the array first; especially if the array will not be needed to be searched again.
> Optimal sorting complexity is O(n log n), while worst case linear search is O(n).
Example
Consider an array with the elements { 5, 12, 3, 9, 1, 7, 4 }
. This is an excellent candidate for using linear search as the list is unordered.
To find out if 4
is in the array, we can loop through each item, checking the values.
// Array to iterate over.
int [] array = { 5, 12, 3, 9, 1, 7, 4 };
// Searching the array with linear search.
function contains(int value) {
// Iterate the array.
for ( int i = 0; i < array.length; i++ ) {
// Check the value.
if ( array[i] == value ) {
return true;
}
}
// No value was matched, so we'll return false.
return false;
}
Using this framework, the method can be edited to retrieve the index of the item being searched for as well as other interesting methods like finding the smallest item in the array.
> If we wanted to continue finding elements in the array, it might be worth sorting it to utilize binary search in further searches.
Further Resources
- <a href="https://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X/ref=as_li_ss_tl?_encoding=UTF8&pd_rd_i=032157351X&pd_rd_r=37c135e7-c5c4-4ed1-94ab-c2aaec60d692&pd_rd_w=PEuac&pd_rd_wg=xSJkC&pf_rd_p=1c11b7ff-9ffb-4ba6-8036-be1b0afa79bb&pf_rd_r=H3XK6W265MFMNYZ7PV3Z&psc=1&refRID=H3XK6W265MFMNYZ7PV3Z&linkCode=ll1&tag=devmaking-20&linkId=9f9600161b287607f80038c5fd85b020&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Algorithms (Sedgwick)</a> An excellent resource and reference for learning algorithms in computer science.
- <a href="https://www.amazon.com/Algorithms-Sanjoy-Dasgupta-ebook-dp-B006Z0QR3I/dp/B006Z0QR3I/ref=as_li_ss_tl?_encoding=UTF8&me=&qid=1566246918&linkCode=ll1&tag=devmaking-20&linkId=26b190aacb9c02d1a0815d4066f9d80e&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Algorithms (Dasgupta)</a> Concise and clear overview on various types and designs of algorithms in a digestible format.
- <a href="https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=devmaking-20&linkId=1dd5be6066b180c12926dc03e2659215&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Introduction to Algorithms (CLRS)</a> An in depth and comprehensively detailed explanation of algorithms and how to design them.
- <a href="https://www.amazon.com/Introduction-Graph-Theory-Dover-Mathematics/dp/0486678709/ref=as_li_ss_tl?keywords=graph+theory&qid=1566248143&s=books&sr=1-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUExUVdSUTU4TjVPRzdIJmVuY3J5cHRlZElkPUEwNzM2MDIyMkIxUUdNVENLMVJFUiZlbmNyeXB0ZWRBZElkPUEwNzc2NTcyNDBEMlFGWDJRRU5XJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==&linkCode=ll1&tag=devmaking-20&linkId=3a3d02f5e5b338587f4e4e4be75fe2f2&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Introduction to Graph Theory</a> An accessible, mathematic-centered approach to graph theory for new programmers and mathematicians.