Searching and Sorting
Learn about some of the most common searching and sorting algorithms!
StartKey Concepts
Review core concepts you need to learn to master this subject
Searching for smallest or largest value using linear search
Linear Search best case
Linear Search Complexity
Linear Search expressed as a Function
Return value of a linear search
Modification of linear search function
Linear search
Linear search as a part of complex searching problems
Searching for smallest or largest value using linear search
Searching for smallest or largest value using linear search
Linear search can be used to search for the smallest or largest value in an unsorted list rather than searching for a match. It can do so by keeping track of the largest (or smallest) value and updating as necessary as the algorithm iterates through the dataset.
Create a variable called max_value_index
Set max_value_index to the index of the first element of the search list
For each element in the search list
if element is greater than the element at max_value_index
Set max_value_index equal to the index of the element
return max_value_index
- 1Imagine that you are a DJ at a party. The diagram on the right shows your playlist for the event. A party guest wants to know if “Uptown Funk” by Bruno Mars is a song on your playlist. You would …
- 2Linear search can be used to search for a desired value in a list. It achieves this by examining each of the elements and comparing it with the search element starting with the first element to the…
- 3Linear search is not considered the most efficient search algorithm, especially for lists of large magnitudes. However, linear search is a great choice if you expect to find the target value at the…
- 4There are two worst cases for linear search. Case 1: when the target value at the end of the list.
…
- 5If this search was used 1000 times on 1000 different lists, some of them would be the best case, some the worst. For most searches, it would be somewhere in between. On average it would be in the…
- 6Linear search runs in linear time. Its efficiency can be expressed as a linear function, with the number of comparisons to find a target increasing linearly as the size of the list, N, increases. …
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory