COURSES
MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Law
Job Linked
Bootcamps
Study Abroad
Master of Business Administration (90 ECTS)Master of Business Administration (60 ECTS)Master in Computer Science (120 ECTS)Master in International Management (120 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS)MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMaster of Science in AccountancyMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business AdministrationMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science MBA Business Technologies MBA Leading Business Transformation Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMS in Project ManagementMaster in Logistics and Supply Chain ManagementMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMS in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMBA in Strategic Data Driven ManagementMaster of Business AdministrationMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc Digital MarketingMSc in Sustainable Luxury and Creative IndustriesMSc in Sustainable Global Supply Chain ManagementMSc in International Corporate FinanceMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSBachelors in International Management
For College Students

What is Linear Search Algorithm in Data Structure

$$/$$

In this segment, we will discuss this naive search technique, which is also the most intuitive one. If you are told to search for something in a list, you will most likely do so this way. So, let’s see what linear search is and how efficient it is. 

$$/$$

So, in this video, you learned how linear search algorithm works. It basically compares the element you are searching for with every element in the array. It keeps on comparing till the algorithm finds the required element or it reaches the end of the array to conclude that the element does not exist in the array.

 

Video Transcript

 

So now we'll talk about linear search. So suppose you are given a collection of objects and you are supposed to find whether an element exists in that collection or not. So what is the most basic way to do it? Well, the most basic way would be to compare each and every element from that collection and see if it equals to the element which I want to find. If I find it, then we are done. And if I iterate over all the objects in my collection and still don't find that element, then this would imply that the element does not exist. So let us take a simple example and see how this works. Suppose I'm given an array of numbers like this.

 

Let me call this array array A. Now suppose I want to find an element x and check if it exists inside this array or no. Let me take x equals to seven. So the idea behind linear search is a very basic one. What I'll do is that I will take this array and I will compare each and every object in that array with the element which I want to find. So I will start with number six and check if six equals to seven. Since they are not equal, I say no, I haven't found this element and I move on to my next element. My next element here is three. Again I'll compare it with seven and find that since they are unequal, again my search has been unsuccessful. So now I'll move on to the next element here, which is eight. On comparing it again with seven, I find that since they both are not equal, so I must move on to my next element, which is seven in this case. So in this step, when I compare this seven with the seven which I want to find, I see that they both are equal. So I say that I have finally found my element seven inside this array and it is located at index number 0123. So I'll say that I found the element number seven at index number three. So how many steps did it take for me to find it? Step one, step two, step three and step four. So in a total number of four steps I was able to successfully find number seven here. Now let us take another simple example with the same array, suppose I have to find whether element x exists inside this array or no. And in this case I'll take my x equal to one. So repeating the same procedure.

 

Repeating the same procedure. Now what I have to do is compare this one with each and every element in this array. So in step one, I compare it with six and I find that they are unequal. So I move on to the next element which is three. I again compare it with one and find that they are unequal. Now I move to my elements number three which is eight. On comparing it with one I find again unequal and similarly, on comparing it with seven, again I'll see that since they both are unequal my search has been unsuccessful. Now, lastly, I'll check the last element of my array which is nine and which is also not equal to one. But do I have to check anything beyond this? No. I reached the end of my array and on iterating through each and every element of this array I could not find the element which I wanted to seek which was one in this case. So what will I say? I'll say that my overall search over this array for finding element one was unsuccessful. And how many steps did it take for me to conclude that? Step 1 2 3 4 and 5. So it took me a total number of five steps to see that one does not exist inside this array. And also five was the total number of elements in this array. And I had to do five comparisons and check step by step whether it equals to the element which I wanted to find.

 

Video Recap

 

 

  • Linear search in java and other languages is used to find an element in a collection of objects

  • The most basic way is to compare each element with the element to be found

  • If found, the search is successful, else it implies that the element does not exist

  • Linear search in java and other languages involves iterating through each element of the collection

  • Example: searching for element 7 in an array of numbers

  • Compare each element with 7 until it is found at index 3 after 4 steps

  • Another example: searching for element 1 in the same array

  • Compare each element with 1 until the end of the array, which takes 5 steps

  • If the element is not found after iterating through all elements, the search is unsuccessful

  • Total number of steps taken is equal to the number of elements in the array

  • Linear search in java and other languages is a simple but inefficient search algorithm.

 

Brute-force searching :
Brute-force searching is also known as exhaustive searching and simply means to check all possible configurations for a given problem. It is easy to implement and will most definitely find the solution but it is quite time-consuming.

For example :

If you have a problem set in a countable space (chess moves are countable, passwords are countable, continuous stuff is uncountable) brute force will explore this space considering all solutions equally. In the chess example, you want to checkmate your opponent. This is done via a sequence of moves, which is countable. Brute force will go through all sequence of moves, however unlikely they may be. The word unlikely is important because it means that if you have knowledge about your problem (you know what is unlikely to be the solution, like sacrificing your queen), you can do much better than brute force.

For more details, please click on the link below.

https://stackoverflow.com/questions/8103050/what-exactly-is-the-brute-force-algorithm

$$/$$

Great! So now that you have seen how linear search works, it's time to move on to the next segment where you will learn about the Java implementation of Linear Search.