Linear search is a very commonly used searching method used by most programmers in order to search through a small list of elements. This kind of searching technique is also known as the Sequential Searching algorithm. It is an extremely simple searching algorithm, where a sequential search is conducted over all items in a list or array, one at a time. Every element is checked for equality with the being searched element and if a match is found then that particular element is displayed or returned. However, until the search element is spotted, the searching goes on till the pointer reaches the end of the list (Deitel and Deitel 2008).
LinearSearch (array myArray, value searchElement)
Step 1: Set index i to 0
Step 2: if i >= length(myArray) then go to step-7, else go to next step
Step 3: if myArray [i] = searchElement, then go to step-6, else go to next step
Step 4: Increment i by 1
Step 5: Go to Step-2
Step 6: Print searchElement ‘Found at index i and go to step-8’
Step 7: Print ‘element not found’
Step 8: Exit
public class Search{
public static int LinearSearch(int[] myArray, int searchElement){
int flag=0;
for(int i=0;i<myArray.length;i++){
if(myArray[i[==searchElement){
System.out.println(searchElement+" found at index "+i);
return i;
}
}
System.out.println(searchElement+" not found.”);
return -1;
}
}
It is mathematically proven that the efficiency of a Linear Search algorithm is O(n), ‘n’ being the length of the array. This means that the loop counter has to traverse through the entire loop in order to search for a particular element that is probably present at the last index or may be is absent completely (Lafore 2017).
To analyse any algorithm, it is always necessary to consider the best case, the average case and the worst case scenario for that particular algorithm.
Considering the example below, the iteration comparisons can be easily made for the respective case scenarios.
Suppose, for an array of length 10 with the following elements:
[ 15 , 12 , 16 , 20 , 45 , 33 , 19 , 25 , 30 , 10 ] and the element being searched for is 15.
Therefore, the element can be found with the linear search making the fewest comparisons at index 0 itself. Therefore, for best case scenario, the loop exits with only one iteration with a complexity of O(1).
Suppose, for an array of length 10 with the following elements:
[ 15 , 12 , 16 , 20 , 45 , 33 , 19 , 25 , 30 , 10 ] and the element being searched for is 10.
The element being present at the very end of the loop, in this case, the counter will have to iterate through the entire loop in order to spot the element being searched for. This gives the algorithm a complexity of O(10) as the loop has to go through 10 iterations. This would be the same if the item being searched for was absent from the array.
Therefore, it can be concluded that the Average Case iterations for the algorithm is N/2.
All values present in the array at a lower index than the present loop iterator index within the array, is not the value being searched (present searching index <= value index). Therefore, if an element is present multiple times, then the loop counter is surely expected to hit the first occurrence as index <= index of searched number.
Deitel, P.J. and Deitel, H.M., 2008. C++ how to Program. PearsonPrentice Hall.
Fletcher, R. and Reeves, C.M., 1964. Function minimization by conjugate gradients. The computer journal, 7(2), pp.149-154.
Glass, H. and Cooper, L., 1965. Sequential search: A method for solving constrained optimization problems. Journal of the ACM (JACM), 12(1), pp.71-82.
Lafore, R., 2017. Data structures and algorithms in Java. Sams Publishing.
Ravishankar, M., 1997, February. Some results on search complexity vs accuracy. In DARPA Speech Recognition Workshop (pp. 104-107). Morgan Kaufmann Pub.
Weiss, M.A. and Hartman, S., 1998. Data structures and problem solving using Java (Vol. 204). Boston, MA: Addison-Wesley.
To export a reference to this article please select a referencing stye below:
My Assignment Help. (2020). Data Structures And Algorithms I. Retrieved from https://myassignmenthelp.com/free-samples/comp20010-data-structures-and-algorithms-i/linear-search-algorithm-analysis.html.
"Data Structures And Algorithms I." My Assignment Help, 2020, https://myassignmenthelp.com/free-samples/comp20010-data-structures-and-algorithms-i/linear-search-algorithm-analysis.html.
My Assignment Help (2020) Data Structures And Algorithms I [Online]. Available from: https://myassignmenthelp.com/free-samples/comp20010-data-structures-and-algorithms-i/linear-search-algorithm-analysis.html
[Accessed 27 January 2021].
My Assignment Help. 'Data Structures And Algorithms I' (My Assignment Help, 2020) <https://myassignmenthelp.com/free-samples/comp20010-data-structures-and-algorithms-i/linear-search-algorithm-analysis.html> accessed 27 January 2021.
My Assignment Help. Data Structures And Algorithms I [Internet]. My Assignment Help. 2020 [cited 27 January 2021]. Available from: https://myassignmenthelp.com/free-samples/comp20010-data-structures-and-algorithms-i/linear-search-algorithm-analysis.html.
It is time that you end your struggle with the referencing styles. You just have to visit our website to get authentic results regarding Harvard referencing, APA referencing etc. The service is free of cost and provides immediate result. You simply type in the required author name, book name and other essential data, and the Harvard reference generator will provide you with the desired result. Thus, you do not have to waste time searching for Harvard or APA referencing guide on the Internet. The service is tailor-made to serve your purpose.
Answer: For improving the business there are many parts need to upgrade. Human errors leads to huge losses to the organization. Errors are common for everyone but sometimes minimal errors leads to big problem. This topic also states the human error in the ice cream store that leads to business loss. In this assignment the management of the ice cream shop wants implement the automation system for the entire billing system. Because this process ...
Read MoreAnswer: Manual systems and operations have often placed a lot of pressure on employees to be extra careful and do tasks in the right way every time, but the problem is that human is to error and they are prone to do them many times. Productivity, performance, and quality of services in manual system is solely depended on the person doing it, therefore, this requires that the business carries out regular training for its employees so as to enab...
Read MoreAnswer: The current problem is a case of testing of hypothesis regarding mean lifetime ( ) of light bulbs. The standard deviation of light bulb has been provided as hours. Assumption: Let the random variable X denotes the lifetime of the light bulbs, where it is assumed that X follows Normal Distribution, (Park, 2015). Step 1: The null hypothesis is constructed as, Step 2: The alternate hypothesis is taken as, {two-tailed} Step 3: The leve...
Read MoreAnswer: Cryptography In cryptography, a hash value is a numeric value which is used to uniquely identify data and it has a fixed length in string values. Since hash values represent data of large amounts as well as smaller numeric values, they apply digital signatures in their operations (Lefebvre, Czyz & Macq, 2003, p. 496). Hash values are mostly used to store passwords in a database. 1. In order to have a successful message authentic...
Read MoreAnswer: Paperback Land is a small organization for selling and buying book. This book shop is run by three different employees. Andrew and Bhavesh are the semi-retired co-owners of the bookshop, and do not take a salary. Chad is a part time employee and its total employment cost is $22 per hour. In task 1, the total annual cost is calculating for this bookshop in a excel sheet. This sheet includes different type of working rate, working hours,...
Read MoreJust share requirement and get customized Solution.
Orders
Overall Rating
Experts
Our writers make sure that all orders are submitted, prior to the deadline.
Using reliable plagiarism detection software, Turnitin.com.We only provide customized 100 percent original papers.
Feel free to contact our assignment writing services any time via phone, email or live chat. If you are unable to calculate word count online, ask our customer executives.
Our writers can provide you professional writing assistance on any subject at any level.
Our best price guarantee ensures that the features we offer cannot be matched by any of the competitors.
Get all your documents checked for plagiarism or duplicacy with us.
Get different kinds of essays typed in minutes with clicks.
Calculate your semester grades and cumulative GPa with our GPA Calculator.
Balance any chemical equation in minutes just by entering the formula.
Calculate the number of words and number of pages of all your academic documents.
Our Mission Client Satisfaction
Thanks for doing it on time...great work...fingers crossed for the grades...i hope i get good grades...its a great website to do assignments and all the exams
Australia
Satisfied with the writing style. The writer responded immediately for the revisions which were requested.
Australia
did not have time to start on my assignment and randomly googled for people to do it for me. To my surprise, i found myassignmenthelp and they did it for me just on time for my submission with even time to spare. My first time, but definitely not my ...
Australia
Thank you, thank you, thank you very much. It is one of the most beautiful and highest levels of the solution that I have seen in my life. In all honesty, the expert is very distinguished and he was at the heart of the solution to answer all the poin...
Australia